2

I implemented the following MKMapView method, which runs after annotations are added. I have my MKMapView map (parishMap) set to "Shows user location" in Interface Builder, and upon loading the mapview, the blue dot always appears within a second or so on the mapview.

Here's the code I have in my Map View controller implementation:

// Center map on user location (initially)
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
    for(MKAnnotationView *annotationView in views) {
        if(annotationView.annotation == parishMap.userLocation) {
            MKCoordinateRegion region;
            MKCoordinateSpan span;

            span.latitudeDelta=0.03;
            span.longitudeDelta=0.03;

            CLLocationCoordinate2D location=parishMap.userLocation.coordinate;

            location = parishMap.userLocation.location.coordinate;

            region.span=span;
            region.center=location;

            [parishMap setRegion:region animated:TRUE];
            [parishMap regionThatFits:region];
        }
    }
}

When I open the mapview on my iPhone 4, the mapview always (and almost immediately) moves to center on the userLocation dot. However, when I open the mapview on an iPhone 3Gs or iPod Touch (2010), it doesn't center on the userLocation, but just stays at the default zoom/region I set for the map view.

Any ideas as to why this code won't work for non-iPhone 4 devices (they're all running 4.2.x latest iOS)?

geerlingguy
  • 4,682
  • 8
  • 56
  • 92
  • Also, I have a button that I use to grab the values from parishMap.userLocation.location, and that button works in updating the parishMap, on any device. – geerlingguy Feb 23 '11 at 22:33

2 Answers2

2

This is probably an issue with the amount of time it takes for the GPS to lock a position on these different devices.

I'd recommend using CLLocationManager and its delegate methods to center the map.

You can start updating location by creating an instance of CLLocationManager, setting its delegate property to self and then implementing:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

Where you can center your map accordingly based on the latest location update.

Rog
  • 18,602
  • 6
  • 76
  • 97
  • This is the best way to do it, in most cases, and I got things working better using this method, but I'm going to keep my code (original post) around in case 4.3 allows it to work on all iOS devices. Seems simpler to me. – geerlingguy Mar 09 '11 at 07:31
-1

have you tried using this code to center the map:

[mapView setCenterCoordinate:location animated:YES];

I'm not sure why it would work for iPhone 4 and not the others, but this is the code I use to adjust the region:

    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location, 0.03, 0.03);
    MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];

    [mapView setRegion:adjustedRegion animated:YES];

Try it that way and see how that works. The way you're doing the regionThatFits method, that's actually not supposed to be how the method is used. You can try commenting out that last line of your code, it should make no difference. Anyway, try it with the method I just gave and let me know if you have any questions.

Omar
  • 1,574
  • 1
  • 9
  • 14
  • Removing that regionThatFits line doesn't break things, so I've removed it permanently. However, using setCenterCoordinate, the iPod touch still won't update it's location. Could it have to do with the device taking a longer time to get an accurate location? – geerlingguy Feb 23 '11 at 23:22
  • Hmm... looks like problem might be that didAddAnnotationViews isn't getting called when the user location pin drops. Now that I think of it, my iPhone 4 is running iOS 4.3 beta - maybe Apple added that feature back in? See (http://stackoverflow.com/questions/4840015/didaddannotationviews-not-working-on-mkmapview)... – geerlingguy Feb 23 '11 at 23:26
  • @geerlingguy Your issue is likely to be related to the device not locking a position quickly enough. I've added my answer as something for you to consider. – Rog Feb 23 '11 at 23:27
  • Rog's suggestion is actually the way I do it, but I figured I'd help you figure it out the way you want to figure it out. It's possible that Apple might have fixed some issue with the new beta. Have you tried installing the beta on the other devices? As per that question you linked to, I would suggest going the same route that Rog and myself go. It's a lot cleaner. If you want to know how to do it that way, let me know and I'll write up a new answer for you. – Omar Feb 24 '11 at 05:06