views:

1244

answers:

1

Hello!

I use a MapView and I set annotations on it (purple pins) and my user location (which is a blue circle). Because the purple pin annotations will move, I have to remove and set them new to the map.

I set it with:

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};
    MyMapAnnotation* annotation = [[MyMapAnnotation alloc] initWithCoordinate:newCoord andID:count++];
    [mapView addAnnotation:annotation];
    [annotation release];
}

Before that, I do a

[mapView removeAnnotations:mapView.annotations];

but this line also remove my location with the blue point! How could I do this without removing my location.

Thanks a lot in advance & Best Regards.

+2  A: 

A very easy way to do this is to include this line before the loop where you remove the annotations:

    self.mapView.showsUserLocation = NO;

and then after the loop, put the user location back in

    self.mapView.showsUserLocation = YES;

EDIT: I just saw that you didn't loop through the locations to remove them. I'm not sure if this will work with the way you do it.

nevan
Hm, how could I make it better?Because I do:- (MKAnnotationView *)mapView:(MKMapView *)mapViewLocal viewForAnnotation:(id <MKAnnotation>)annotation { if (annotation == mapViewLocal.userLocation) { mapViewLocal.userLocation.title = @"Test"; mapViewLocal.userLocation.subtitle = @"Test Sub"; [mapViewLocal setRegion:MKCoordinateRegionMakeWithDistance(mapViewLocal.userLocation.coordinate, 3000, 3000) animated:NO]; return nil; } ...So i will always set my location in the center. And the blue point does not appear. I have to move the MapView, then the blue point will be displayed.
Tim
To get a map view with annotations to center, loop through them and find the max and min latitude and longitude, then set the region span and center using those. If you want to include the user location in the annotations, make sure it's displayed using the method I wrote above. `MKMapView` has a property called `annotations` which stores all the annotations (including the user location). If you just want to center the map on the user location, use `setCenterCoordinate:animated:`
nevan
If you can spare $5, this tutorial really helped me understand map views: http://pragprog.com/screencasts/v-bdmapkit/using-map-kit
nevan
I re-organized my code. I create annotation objects in the loop and add the to a NSMutableArray. And the I use the remove-annotations-method which parameter is this NSMuatable-Annotation-Array. That works fine.
Tim
glad to hear it.
nevan