I'm pretty new at programming for the iPhone but I made an app with just an MKMapView in it. I made it to zoom in to my location but I keeps updating the location. My point is that I want it to stop auto locate me, It can still be going but I just want to make it stop locate. When I slide at any direction it forces me back to my location, or when I take a walk and updating the position. My idea is that I want the app to work with some annotations.
- (void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(@"Found your location!");
MKCoordinateRegion mapregion;
map.showsUserLocation = YES;
mapregion.center.latitude = map.userLocation.coordinate.latitude;
mapregion.center.longitude = map.userLocation.coordinate.longitude;
mapregion.span = MKCoordinateSpanMake(0.005,0.005);
[map setRegion:mapregion animated:YES];
map.userLocation.title = @"You're here";
// map.mapType = MKUserTrackingModeNone;
// (found this on apple developer site, I think this can
// help but I have no clue at all //someone know what you can use this for?)
}
// In case you have the flightmode on...
- (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
{
NSLog(@"gick inte att hitta position!");
map.showsUserLocation = YES;
MKCoordinateRegion mapregion;
mapregion.center.latitude =(59.329444);
mapregion.center.longitude =(18.068611);
mapregion.span.latitudeDelta = 0.03;
mapregion.span.longitudeDelta = 0.03;
mapregion = [map regionThatFits:mapregion];
[map setRegion:mapregion animated:TRUE];
}
- (void)viewDidLoad
{
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
map.delegate = self;
}
I really hope that someone knows how to accomplish this...
Your problem is that you are setting the center of the map every time that the users location is updated. This is one solution... in your header:
BOOL userLocationShown;
and then...
- (void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if(userLocationShown) return;
// ... your code
userLocationShown = YES;
}
Or if you never want to center the map on the user you could just remove the following lines:
mapregion.center.latitude = map.userLocation.coordinate.latitude;
mapregion.center.longitude = map.userLocation.coordinate.longitude;
mapregion.span = MKCoordinateSpanMake(0.005,0.005);
[map setRegion:mapregion animated:YES];
If the device is still getting location updates, even if you are ignoring them, it is running the GPS and burning through the battery. Once you've locked down their location, either by checking how many times they have moved, or measuring the accuracy of the fix (CLLocation. horizontalAccuracy), you can simply turn off location updates on your map view like this:
self.showsUserLocation = NO;
If you had been using a CLLocationManager it would be
[locationManager stopUpdatingLocation];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With