Note that there are some explanatory texts on larger screens.

plurals
  1. POshow user location on a map and stop location
    text
    copied!<p>Here is my goal: I have to show the user location with his address on a map every time he open the view that has the MapView and then stop location to save battery. So far i can do it, but if the user moves to another area and enter the view (after leave it) the location does not change.</p> <p>Here is my code:</p> <pre><code>- (void)viewDidLoad { mapView.mapType = MKMapTypeStandard; self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; [super viewDidLoad]; } - (void)viewDidAppear:(BOOL)animated{ if(!self.locationManager){ self.locationManager = [[CLLocationManager alloc] init]; } [self.locationManager startUpdatingLocation]; } -(void)viewDidDisappear:(BOOL)animated{ self.locationManager = nil; } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 2000, 2000); MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion]; [mapView setRegion:adjustedRegion animated:YES]; [manager stopUpdatingLocation]; MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate]; geocoder.delegate = self; [geocoder start]; } - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark { MapLocation *annotation = [[MapLocation alloc] init]; annotation.streetAddress = placemark.thoroughfare; annotation.city = placemark.locality; annotation.state = placemark.administrativeArea; annotation.zip = placemark.postalCode; annotation.coordinate = geocoder.coordinate; [mapView addAnnotation:annotation]; geocoder.delegate = nil; } - (void)openCallout:(id&lt;MKAnnotation&gt;)annotation { [mapView selectAnnotation:annotation animated:YES]; } - (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id &lt;MKAnnotation&gt;) annotation { static NSString *placemarkIdentifier = @"Map Location Identifier"; if ([annotation isKindOfClass:[MapLocation class]]) { MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[theMapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifier]; if (annotationView == nil) { annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:placemarkIdentifier]; } else{ annotationView.annotation = annotation; } annotationView.enabled = YES; annotationView.canShowCallout = YES; annotationView.image = [UIImage imageNamed:@"rescue"]; [self performSelector:@selector(openCallout:) withObject:annotation afterDelay:0.5]; return annotationView; } return nil; } </code></pre> <p>I have a CLLocationManager on another view that get user location without a map (here the location is working just fine). If a start the location on that view and go back to the view with the map it's is updated. Why that?</p> <p>Any ideias?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload