I’m confused on how this works. I’m creating a CLGeocoder to drop a pin based on a string value. I have this:
- (void)placeMarkFromString:(NSString *)address {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
[placemarks enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%@", [obj description]);
}];
// Check for returned placemarks
if (placemarks && [placemarks count] > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
// Create an MKPlacemark and add it to the mapView
MKPlacemark *place = [[MKPlacemark alloc] initWithPlacemark:topResult];
AddressAnnotation *anAddress = [[AddressAnnotation alloc] init];
anAddress.address = place.subThoroughfare;
anAddress.street = place.thoroughfare;
anAddress.city = place.locality;
anAddress.state = place.administrativeArea;
anAddress.zip = place.postalCode;
anAddress.name = place.name;
//[self.mapView addAnnotation:place];
[self.mapView addAnnotation:anAddress];
self.currentPlacemark = place;
// Center map on that region
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(topResult.location.coordinate, 2000, 2000);
MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:region];
[_mapView setRegion:adjustedRegion animated:YES];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Results Found" message:@"" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
if (error) {
NSLog(@"Error: %@", [error localizedDescription]);
}
}];
}
So originally, I added my MKPlacemark to the map and it shows the red pin. It does not animate however. I basically want the ability to drop any of the 3 MKPinAnnotationView colors, have a callout and the title/subtitle to be the name and address of the place, similar to how google maps does it. But I was not getting any animation.
So I thought that maybe I needed to create my own object that conforms to the MKAnnotation class. So I did that, but when I try to add it to the location, I do not see its annotationView in the viewForAnnotation delegate method. That method is here:
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id<MKAnnotation>)annotation {
static NSString *placeMarkIdentifier = @"SimplePinIdentifier";
if ([annotation isKindOfClass:[AddressAnnotation class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[theMapView dequeueReusableAnnotationViewWithIdentifier:placeMarkIdentifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:placeMarkIdentifier];
}
else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.animatesDrop = YES;
annotationView.draggable = YES;
annotationView.pinColor = MKPinAnnotationColorPurple;
annotationView.canShowCallout = YES;
// Create a button for the annotation
// UIButton *rightArrowButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
// annotationView.rightCalloutAccessoryView = rightArrowButton;
// [self performSelector:@selector(openCallout:) withObject:annotation afterDelay:0.5];
return annotationView;
}
return nil;
}
So I guess my questions are, do I need to create my own object to do this, am I on the right track, what am I doing wrong, and why is it that in one case, I am adding a MKPlacemark object, and then if I do it the way other way, I add a object, but not necessarily a subclass of MKPlacemark. Thanks!
First, the reason you’re probably not seeing the annotation view when you use a custom annotation object (ie.
AddressAnnotation) is that itscoordinateis not being set (and so it’s appearing at 0,0).In the
placeMarkFromStringmethod, the code sets a lot of properties of theAddressAnnotationbut not thecoordinateand so the annotation is not appearing where expected.If you set the
coordinate, it will appear where expected and with the animation.Regarding the other question as to why you see no animation when using an
MKPlacemark:By default, the map view will create an
MKPinAnnotationViewwith a red pin but withanimatesDropset toNO.So in
viewForAnnotation, you have to explicitly do it forMKPlacemarkjust like you are forAddressAnnotation.If all your annotations will be using
MKPinAnnotationView, instead of checking for each different class of annotation you will be creating, you can flip the condition around by returningnilat the top of the method when the annotation class isMKUserLocationand then run the rest of the code without any class-checking (ie. returningMKPinAnnotationViewwithanimatesDropset toYESin all other cases).