views:

4010

answers:

3

I use this tutorial for integrating MapKit to my application: http://iphonebcit.wordpress.com/iphone-map-kit-tutorial/

CLLocationCoordinate2D coordinate;
coordinate.latitude = 49.2802;
coordinate.longitude = -123.1182;

NSUInteger count = 1;
for(int i = 0; i < 10; i++) {
 CGFloat latDelta = rand()*.035/RAND_MAX - .02;
 CGFloat longDelta = rand()*.03/RAND_MAX - .015;

 CLLocationCoordinate2D newCoord = {coordinate.latitude+latDelta, coordinate.longitude+longDelta};
 MapDemoAnnotation* annotation = [[MapDemoAnnotation alloc] initWithCoordinate:newCoord andID:count++];
 [mapView addAnnotation:annotation];
 [annotation release];
}

and

- (MKAnnotationView *)mapView:(MKMapView *)mapViewLocal viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapViewLocal dequeueReusableAnnotationViewWithIdentifier:@"Pin"];
if(pinView == nil) {
 pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];
 pinView.pinColor = MKPinAnnotationColorPurple;
 pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
 pinView.animatesDrop = YES;
 pinView.canShowCallout = YES;
} else {
 pinView.annotation = annotation;
}
return pinView;

}

So the pins will set on the map randomly. In my application, the coordinates will change. How can I change the coordinates of the annotations, so they will be updated on the map?

Does anyone know?

Thanks & Regards

+1  A: 

That tutorial is only for getting a map view to show, nothing else. You're going to need a bigger tutorial. I found this one useful:

http://blog.objectgraph.com/index.php/2009/04/02/iphone-sdk-30-playing-with-map-kit/

Probably what you'll need to do is to loop through all the annotations on the map, removing them, then loop through your data array, adding the annotations back again. You could be more clever about it and loop through the data array, checking if there's already a pin with the same latitude/longitude on the map, but that way gets more complicated.

nevan
Thanks a lot. My implemented annotation protocol has an additional instance variable called locationId. This id is unique and I know which locationId should be updated with the new coordinates.How could I access it easily? Can I remove an annotation with:"annotation with locationId=3999 remove and add it with the new coordinates?" - better: "annotation with locationId=3999 set new coordinates and update the location if the pin".
Tim
Ah, I forgot. I already saw this tutorial, but this can't help me, because there is nothing useful in it to solve my problem.
Tim
+1  A: 

At iPhone SDK 3.x you have to remove the pin annotations and set it again. That is not very nice if you have many annotations an your map.

I try to make it better so I ony display/renew my pin annotations which are on the screen. So if an user zoom in to New York, there won't be pin annotations in San Francisco or other than the user can't see. So the performance will be much better.

Perhaps in the future this would be possible. I hope so :-)

Tim
+1  A: 

Since I'm doing annotations at the moment I just made a quick test. You do get a compiler warning, so it might not be supported. But it works.

Make a custom MKAnnotation class so you can set the coordinate property to be writable:

@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;

Then at whatever event or interval you want, change the coordinate using something like this:

    CLLocation *loc=[[CLLocation alloc] initWithLatitude:55.0 longitude:17.0];
annotation.coordinate=loc.coordinate;
[loc release];
Henrik Erlandsson