MKPlacemark pin title

7.4k views Asked by At

I have my mapview working fine, but the pin that is placed on the map has the title United States. How can I change this title?

    MKCoordinateRegion thisRegion = {{0.0,0.0}, {0.0,0.0}};

        thisRegion.center.latitude = 22.569722;
        thisRegion.center.longitude = 88.369722;

        CLLocationCoordinate2D coordinate;
        coordinate.latitude = 22.569722;
        coordinate.longitude = 88.369722;

        thisRegion.center = coordinate;

        MKPlacemark *mPlacemark = [[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil] autorelease];

        [mapView addAnnotation:mPlacemark];
        [mapView setRegion:thisRegion animated:YES];
3

There are 3 answers

0
Jaminyah On

The code below demonstrates placing an annotation on a map using CLGeocoder in iOS 5.1

-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

CLGeocoder *geocoder = [[CLGeocoder alloc] init];

// Apple recommendation - if location is older than 30s ignore
// Comment out below during development
/*  if (fabs([newLocation.timestamp timeIntervalSinceDate:[NSDate date]]) > 30) {
    NSLog(@"timestamp");
    return;
}*/   

CLLocation *coord = [[CLLocation alloc] initWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude];                            
[geocoder reverseGeocodeLocation:coord completionHandler:^(NSArray *placemarks, NSError *error) {

    if (error) {
        NSLog(@"Geocode failed with error");
    }

    // check for returned placemarks
    if (placemarks && placemarks.count > 0) {
        CLPlacemark *topresult = [placemarks objectAtIndex:0];
        MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
        annotation.coordinate = locationManager.location.coordinate;
        annotation.title = NSLocalizedString(@"You are here", @"Title");
        annotation.subtitle = [NSString stringWithFormat:@"%@, %@", [topresult subAdministrativeArea], [topresult locality]];
        [self.mapView addAnnotation:annotation];
    }
}];
}
0
Markus On

Quite old question, but maybe someone else stumbles upon the same problem (as I did):

Don't add a MKPlacemark to the map's annotations; use MKPointAnnotation instead. This class has title and subtitle properties that are not readonly. When you set them, the annotation on the map is updated accordingly - and this is probably what you want.

To use MKPointAnnotation in your code, replace the lines that allocate and add the MKPlacemark with this code:

MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = coordinate;
annotation.title = NSLocalizedString(@"Dropped Pin",
                                     @"Title of a dropped pin in a map");
[mapView addAnnotation:annotation];

You can set the title and subtitle properties any time later, too. For instance, if you have an asynchronous address query running, you can set the subtitle to the annotation's address as soon as the address is available.