views:

3448

answers:

5

Greetings! I'm attempting to use MKMapView without any Apple code samples, though there are a few others out there of varying clarity. (I know, "Read the friendly manual." I've done that but it's not 100% clear, so please bear with me on this one.)

Here's the situation. I have a MKMapView object, wherein I have added a set of about ten MKPinAnnotation objects. So far, so good. Everything is alloced/released sanely and there doesn't appear to be any complaints from Instruments.

Upon initial display, I set up a MKCoordinateRegion object with the centerpoint at our first pin location, and a (arbitrary) span of 0.2 x 0.2. I then call:

[mapView setRegion:region animated:YES];
[mapView regionThatFits:region];

Wow! That worked well.

Meanwhile ... I also have a segmented control to allow for movement to each pin location. So as I tap through the list, the map animates to each new pin location with a new pair of calls to setRegion:animated: and regionThatFits: ... or at least that's the idea.

While the map does "travel" to the new pin location, the map itself doesn't update underneath. Instead, I see my pin on a gray/blank-map background ... until I nudge the map in any direction, however slightly. Then the map shows through! (If I'm only moving within a short distance of the previous pin location, I'll usually see whatever part of the map was already loaded.)

I suspect I'm doing something dumb here, but I haven't been able to figure out what, at least not from the MapKit docs. Perhaps I'm using the wrong calls? (Well, I do need to set the region at least once, yes? Moving that around doesn't seem to help though.) I have also tried using setCenterCoordinate:animated: - same problem.

I'm assuming nothing at this point (no pun intended). Just trying to find my way.

Clues welcome/appreciated!

UPDATE: Calling setRegion:animated: and regionThatFits: the first time, followed by setCenterCoordinate:animated: while traversing the list, has no effect. Interesting finding though: If I change animated to NO in both cases, the map updates!!! Only when it's set to YES. (Wha happen?! Is animated: broken? That can't be ... ???)

+1  A: 

You need to invoke the setRegion:animated: call in the Main thread context. Just do something like: .... [self performSelectorOnMainThread:@selector(updateMyMap) withObject:nil waitUntilDone:NO];

}

-(void) updateMyMap { [myMap setRegion:myRegion animated:YES]; }

and it should work in any case (animated or not), with the map updated underneath.

Hope this helps. (I faced the same issue ;) Cheers

yonel
Thanks! I just tried this, and ... it works. However, I then discovered that it works without making this call too. So what changed? Well, it turns out that the map update doesn't work when using the SIMULATOR. When I try it on the device, I get the map update underneath. So I was trusting the simulator to match the device in terms of something like map updating behavior. Alas, I was mistaken! Lesson learned.
Joe D'Andrea
Have similar problem on 3.1. This approach doesn't help me. My topic is there: http://stackoverflow.com/questions/1190270/mkmapview-refresh-after-pin-moves
slatvick
+2  A: 

It turns out that the map update doesn't work when using the SIMULATOR. When I try setCenterCoordinate:animated: on the device, I do get the map update underneath.

Bottom line: I was trusting the simulator to match the device in terms of map updating behavior. Alas, I was mistaken! Lesson learned. "Don't let this happen to you." :)

Joe D'Andrea
A: 

Hum strange. The map updates on my Mac even in the simulator. Maybe a network setting (proxy or whatever) that would prevent the map widget to download the tiles on the simulator ?

yonel
Strange indeed! I'm not sure what I would set, network-wise, because all it takes is a little nudge moving the map and - ta-dah - there it is. I'd say "I'm using the 3.0 environment" but then I just realized MKMapView is only available starting with 3.0b, so that can't be it. :\
Joe D'Andrea
A: 

Guys, could you provide working piece of code?

slatvick
Alas, the surrounding scaffolding for maps is (usually) a bit more involved, but here's a link to an oft-cited tutorial. I suspect many of us dove into this one as well. http://is.gd/4fNQF (objectgraph.com). As for the issue at hand here, I set mapView.selectedAnnotation to the annotation object that I want selected. In that same object I also happen to have the location (lat/lng), so I grab that and call [mapView setCenterCoordinate:location animated:NO] … and here's where the YES/NO dilemma comes in to play. In this example it's NO. That's what I have working thus far.
Joe D'Andrea
A: 

Even though this is an old topic I thought I'd ring in with my experience. It seems the map animation only fails on devices running iOS 3.1.x and the simulator running 3.1.x. My dev iPod touch with 3.1.3 fails to zoom if animation is on.

AWinter