views:

798

answers:

3

I'm using MapKit to display the user's location relative to pins around them. I'd like to be able to mimic the functionality that Maps provides via the crosshair button in the lower left-hand corner of the screen. I'm already aware that MapKit provides a CLLocation object with the user's location via MKUserLocation, I just wanted to seek advice on how I should keep focus on that location. My initial inclination was to use an NSTimer to center the map on that coordinate every 500ms or so.

Is there a better way to do this? Is there something built in to MapKit that I'm missing that will accomplish this?

Thanks so much, Brendan

+1  A: 

I think that I would actually use CLLocationManager and use it's delegate method locationManager:didUpdateToLocation:fromLocation:.

This way, you don't have the overhead of an NSTimer, and it only updates when there's a new location available.

You can pull the longitude and latitude from the CLLocation object sent to the locationManager:didUpdateToLocation:fromLocation: method and pass it to the map view.

Jacob Relkin
Wouldn't this be more inneficient in the end though since MapKit is already using the GPS anyway? I would think that GPS would use more power than a timer. That being said there's a really good chance I'm wrong. I've only been programming on the iphone for 1 week.
Bloudermilk
So you're saying the Core Location **doesn't** use GPS?
Jacob Relkin
I'm saying CL *does* use GPS. So if two classes were pinging the GPS receiver (MapKit's internal CLLocationManager for the blue user location pin and your proposed CLLocationManager) it could wind up using a lot more power than the one internal MapKit CLLocationManager and a simple NSTimer. I suppose this comes down to whether the iPhone SDK is smart enough to support multiple CLLocationManagers without attaching each one to the GPS directly. Or perhaps the MapKit delegate provides it's own didUpdateToLocation:?
Bloudermilk
I guess I'll just have to trust that this isn't going to destroy battery life :)
Bloudermilk
A: 

I go with Jacob Relkin's answer. This tutorial provides a step-by-step procedure of using CoreLocation in an iPhone app. Hope this helps you.

All the Best.

Warrior
@Warrior, thanks :)
Jacob Relkin
A: 

It's really simple to have the map update the user location automatically just like the google maps. Simply set showsUserLocation to YES

self.mapView.showsUserLocation = YES

...and then implement the MKMapViewDelegate to re-center the map when the location is updated.

-(void)               mapView:(MKMapView *)mapView 
        didUpdateUserLocation:(MKUserLocation *)userLocation
{
    if( isTracking )
    {
        pendingRegionChange = YES;
        [self.mapView setCenterCoordinate: userLocation.location.coordinate
                                 animated: YES];
    }
}

And to allow the user to zoom & pan without stealing the view back to the current location...

-(void) mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
    if( isTracking && ! pendingRegionChange )
    {
         isTracking = NO;
         [trackingButton setImage: [UIImage imageNamed: @"Location.png"] 
                         forState: UIControlStateNormal];
    }

    pendingRegionChange = NO;
}

-(IBAction) trackingPressed
{
    pendingRegionChange = YES;
    isTracking = YES;
    [mapView setCenterCoordinate: mapView.userLocation.coordinate 
                        animated: YES];

    [trackingButton setImage: [UIImage imageNamed: @"Location-Tracking.png"] 
                    forState: UIControlStateNormal];
}
Paul Alexander