0

I'm developing an app to show the stores available in a particular zipcode. I start with the current location. I'm getting a blue pulsating dot for my current location. when I enter a zipcode I'm able to show the annotations in the mapview. I have a button "current location" which can to go back to the current location again. However the second time I'm not getting the blue dot? How do I get it, this is the code:

- (void)showCurrentLocation 
{  
 self.mapView.showsUserLocation = YES;
    GPSAnnotation *annotation = [[GPSAnnotation alloc] initWithCoordinate:self.mapView.userLocation.coordinate];

    annotation.title = @"Location";
    [self.mapView addAnnotation:annotation];
    [annotation release];    

    MKCoordinateRegion zoomIn;
    zoomIn.center.latitude = self.mapView.userLocation.coordinate.latitude;
    zoomIn.center.longitude=self.mapView.userLocation.coordinate.longitude;

    zoomIn.span.latitudeDelta = .1f;
    zoomIn.span.longitudeDelta =.1f;

    self.mapView.showsUserLocation = YES;
    [self.mapView setRegion:zoomIn animated:TRUE];
}

// annotation view 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(GPSAnnotation *)annotation
{
    if ([[annotation title] isEqualToString:@"Location"]) 
    {
        MKAnnotationView *annotationView=[[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease];

        annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"blueDot"];
        annotationView.annotation = annotation;
        return  annotationView;
    }

    else  if ([annotation isKindOfClass:[MKUserLocation class]])
    {        
        return nil;
    }
}
petert
  • 6,672
  • 3
  • 38
  • 46
cancerian
  • 942
  • 1
  • 10
  • 18
  • Why are you setting GPSAnnotation coordinate to same as user location? The viewForAnnotation method must be giving you the compiler warning "control reaches end of non-void function". At least add `return nil;` at the end of the method to fix that. There are a few other issues with it besides that. –  Jul 19 '11 at 12:50
  • hi Anna Karenina, thanks for responding. i tried adding "return nil" even then i don get tat blue dot for the second tym. – cancerian Jul 24 '11 at 14:44

1 Answers1

4

Do you make somewhere a [self.mapView removeAnnotations:self.mapView.annotations] to empty the map? Thats a common error because you also remove the userlocation by doing that.

yinkou
  • 5,756
  • 2
  • 24
  • 40
  • hi yinkou, thanks for responding. ya i am removing it. but isnt der a way to added annotation to user current location again?? – cancerian Jul 24 '11 at 14:43
  • Yes, you can add it again by [mapView addAnnotation: mapView.userlocation]; But it is better to don't remove it anyway, because every time you re-add it to the map, it will animate and that may lead to a laggy UI. It's better if you do it this way: `NSMutableArray *annotationsToRemove = [[NSMutableArry alloc] initWithArray: mapView.annotations]; [annotationsToRemove removeAnnotation: mapView.userLocation]; [mapView removeAnnotations: annotationsToRemove];` This will remove all annotations from the map except the userLocation. – yinkou Jul 24 '11 at 14:52