Velmurugan
Velmurugan

Reputation: 2303

Read current location using Map View in iPhone applications

I want to read my current location using Map view in my iPhone application.

My current location may be changed depends upon timing.

Now I will be one location, after one hour I will have travelled some kilometers. But my aim is, whenever I will be there, that current location, I will be want to read.

If there is any way to possible to read like this.

Upvotes: 0

Views: 2825

Answers (4)

Trup
Trup

Reputation: 635

I think http://www.roseindia.net/tutorial/iphone/examples/iPhoneMapViewCurrentLocation.html will help you. It worked for me.

Upvotes: 2

chandrika
chandrika

Reputation: 394

   mapView.delegate=self;
enter code here

MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.2;
span.longitudeDelta = 0.2;
region.span = span;
[mapView setRegion:region animated:YES];

NSMutableArray* annotations=[[NSMutableArray alloc] init];
CLLocationCoordinate2D theCoordinate1;


CLLocationManager *manager = [[CLLocationManager alloc] init];
double lat=manager.location.coordinate.latitude;
double lon=manager.location.coordinate.longitude;


NSString *lat_str1=[NSString stringWithFormat:@"%f",lat];
NSString * long_str1=[NSString stringWithFormat:@"%f",lon];

theCoordinate1.latitude = [lat_str1 floatValue];
theCoordinate1.longitude = [long_str1 floatValue];


MyAnnotation *myAnnotation1=[[MyAnnotation alloc] init];
myAnnotation1.coordinate=theCoordinate1;
myAnnotation1.title=@"Current Address";
myAnnotation1.subtitle=[arr objectAtIndex:0];
[annotations addObject:myAnnotation1];
[myAnnotation1 release];

for(int i=0; i<[annotations count];i++)
{
    [mapView addAnnotation:[annotations objectAtIndex:i]];
}

MKMapRect flyTo = MKMapRectNull;
for (id <MKAnnotation> annotation in annotations) {


    MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
    MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
    if (MKMapRectIsNull(flyTo)) {
        flyTo = pointRect;
    } else {
        flyTo = MKMapRectUnion(flyTo, pointRect);
    }
}

    mapView.visibleMapRect = flyTo;

}

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{


    CLLocationManager *manager = [[CLLocationManager alloc] init];
    double lat=manager.location.coordinate.latitude;
    double lon=manager.location.coordinate.longitude;

    SBJSON *parser = [[SBJSON alloc] init];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://maps.google.com/maps/geo?output=json&oe=utf-8&ll=%@,%@&key=API_KEY",lat_str1,long_str1]]];
      NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSDictionary *statuses = [parser objectWithString:json_string error:nil];


    NSArray *resultsnew1=[statuses objectForKey:@"Placemark"];



arr=[[NSMutableArray alloc]init];


for(NSDictionary *sub333 in resultsnew1)
{

    [arr addObject:[sub333 objectForKey:@"address"]];

    NSString*add_str=[NSString stringWithFormat:@"%@",arr];
    atm_address.text=add_str;

}



address.text=[arr objectAtIndex:0];

}

Upvotes: 2

Dan Ray
Dan Ray

Reputation: 21893

If you have a MKMapView object called mapView, you'd do:

mapView.showsUserLocation = YES;

That'll turn on MKMapView's built in location services. You don't need to deal with CLLocation Manager necessarily, because MKMapView will handle the work of locating the device for you.

Now, receiving the updates that MKMapView produces is a touch trickier if you've never worked with delegates before.

In your .h, you want to adopt the <MKMapViewDelegate> protocol.

Then, right next the above line, say:

mapView.delegate = self;

Then, implement the following method:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    // inside here, userLocation is the MKUserLocation object representing your
    // current position. 

    CLLocation *whereIAm = userLocation.location;
    NSLog(@"I'm at %@", whereIAm.description);

    float latitude = whereAmI.coordinate.latitude;
    float longitude = whereAmI.coordinate.longitude;

    // etc.

}

That method will get called every time MKMapView updates its location, probably every few seconds at least.

Upvotes: 2

dannywartnaby
dannywartnaby

Reputation: 5542

Take a look at the Core location manager. http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManager_Class/

It's fairly straight forward:

  • Create a location manager and register a delegate.
  • Start the location manager.
  • When the location manager detects a change in location, it'll notify your delegate via locationManager:didUpdateToLocation:fromLocation:

The Map View is a presentational component - if you want to get control over location awareness use the location manager.

Upvotes: 2

Related Questions