I have weird problem here. I don't know what I'm going to do on this annotation. I get multiple annotation. It supposed to be one annotation that show my location. here's the screen
here's my code
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
static NSString *identifier = @"MyLocation";
MKAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:@"arrest.png"]; // since you're setting the image, you could use MKAnnotationView instead of MKPinAnnotationView
} else {
NSLog(@"hello world");
annotationView.annotation = annotation;
}
return annotationView;
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 350, 350);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
MKPointAnnotation * point= [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
point.title = @"Where am I?";
point.subtitle = @"I'm here!!!";
[self.mapView addAnnotation:point];
}
-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
MKAnnotationView *annotationView = [views objectAtIndex:0];
id<MKAnnotation> mp = [annotationView annotation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate] ,350,350);
[UIView animateWithDuration:5.0 animations:^{
[self.mapView setRegion:region animated:YES];
} completion:^(BOOL finished) {
[self.mapView selectAnnotation:mp animated:YES];
}];
}
I have only simple scenario on this project. Get the location, change the title and then view the current address. But If I used this -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
, It works fined but I can't change the title of annotation. Why is it I get multiple annotation? Hoping for your help. It is my first time to use this map kit in iOS.
The reason is because every time user location update code will add new annotation. Look at the method didUpdateUserLocation:
.
In this method you are creating new MKPointAnnotation
and adding every time location is updated. So for each call of this method you are adding new annotation, Here you need to remove old annotation before adding new one.
Like below code:
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
// Remove all previous annotations.
[self.mapView removeAnnotations:self.mapView.annotations];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 350, 350);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
MKPointAnnotation * point= [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
point.title = @"Where am I?";
point.subtitle = @"I'm here!!!";
[self.mapView addAnnotation:point];
}
Edit:
If you just want to update location once at a time then add one more line in above method at the end:
[self.mapView setShowsUserLocation:NO];
This line will stop updating location because it is no more interested in user location.
Also have a look on this answer.