Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent the mapview from being updated

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.

Some Code

- (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...

like image 749
user1086801 Avatar asked Dec 08 '11 01:12

user1086801


2 Answers

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];
like image 83
Michael Frederick Avatar answered Nov 01 '22 00:11

Michael Frederick


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];
like image 43
Craig Avatar answered Nov 01 '22 00:11

Craig