0

In my MapView in a button tap I run the following action:

- (IBAction)locate:(id)sender event:(UIEvent*)event {
    DLog(@"");
    if ([self.mapView respondsToSelector:@selector(userTrackingMode)]) {
        [self.mapView setUserTrackingMode:MKUserTrackingModeNone];
    }
    [self.mapView setShowsUserLocation:NO];
    [self.mapView setShowsUserLocation:YES];
}

I see the userLocation pin and call the following method to change the map position:

- (void) resizeRegionToFitAllPins:(BOOL)includeUserLocation animated:(BOOL)animated {
    if ([self.annotations count] == 1) {
        NSObject<MKAnnotation> *annotation = [self.annotations objectAtIndex:0];
        BOOL isUserLocation = [annotation isKindOfClass:MKUserLocation.class];
        if ((includeUserLocation && isUserLocation) ||
            isUserLocation == NO) {
            CLLocationCoordinate2D coordinate;
            coordinate.latitude = annotation.coordinate.latitude;
            coordinate.longitude = annotation.coordinate.longitude;
            [self setRegion:MKCoordinateRegionMake(coordinate, MKCoordinateSpanMake(0.001f, 0.001f)) animated:animated];
        }
    }
}

My problem is that the map is updating the user's position forever (in loop). It is impossible to use the map after activating the user's location. What is necessary to make it stop updating the user's position and allow user to use map?

The method mapView:didUpdateUserLocation: is called over and over again. I Put the MKMapViewDelegate on the interface, set the self.mapView.delegate to self and set the mapViewWillStartLocatingUser: and mapViewDidStopLocatingUser: with only a Dlog(@"") call; The mapViewDidStopLocatingUser: is not called ever.

- (void)mapView:(MKMapView *)map didUpdateUserLocation:(MKUserLocation *)userLocation {
    DLog(@"Resizing...");
    [self.mapView resizeRegionToFitAllPins:YES animated:YES];
    }
}

- (void)mapViewWillStartLocatingUser:(MKMapView *)mapView {
    DLog(@"");
}

- (void)mapViewDidStopLocatingUser:(MKMapView *)mapView {
    DLog(@"");
}
ChenYilong
  • 8,543
  • 9
  • 56
  • 84

3 Answers3

0

You are the one who said [self.mapView setShowsUserLocation:YES];. As long as that situation is in force, the map view will continue to track the user's location. If you want to stop tracking, tell the map view to stop tracking.

However, you can change the tracking behavior by using the map view's userTrackingMode (a MKUserTrackingMode value).

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

You can use this method to avoid the method mapView:didUpdateUserLocation: called over and over again :

[self.locationManager stopUpdatingLocation];

Before you stop updating location, remember this :

self.locationManager.delegate = self;

After you stop updating location, remember this :

self.locationManager.delegate = nil;
ChenYilong
  • 8,543
  • 9
  • 56
  • 84
0

It is behaving exactly as it is supposed to.

The location updates are supposed to be continual as the devices moves (but if it stops, and the accuracy is low, the number of updates will slow or stop).

However, reading between the lines, it doesn't sound that that is actually your problem because you say this:

"It is impossible to use the map after activating the user's location".

Yes it is possible, the fact that it is not possible for you means the cause of the problem is not the continual updates, but how you have designed your code to deal with those continual updates. Continually updating the map is a very common scenario.

You need to do those :

  • post a new question showing all your code and what it is you want to do to determine how to design your program correctly to deal with continuous updates.

  • Add code to make sure resizeRegionToFitAllPins only gets called once whenever you add or remove a pin or if the location changes a certain distance. Not every time didUpdateLocation gets called if that is what is causing you problems.

ChenYilong
  • 8,543
  • 9
  • 56
  • 84
Gruntcakes
  • 37,738
  • 44
  • 184
  • 378