How to set the display name for MKPlacemark?

1.7k views Asked by At

I'm trying to show routes of two places with Apple's Map.

For the two places, I have both names and coordinates.

MKMapItem *currentLocation = [[MKMapItem alloc]
  initWithPlacemark:[[MKPlacemark alloc]
                        initWithCoordinate:paramModel.fromCoordinate2D
                     addressDictionary:nil]];
MKMapItem *toLocation = [[MKMapItem alloc]
  initWithPlacemark:[[MKPlacemark alloc]
                        initWithCoordinate:paramModel.toCoordinate2D
                         addressDictionary:nil]];
return [MKMapItem openMapsWithItems:@[ currentLocation, toLocation ]
                    launchOptions:@{
                      MKLaunchOptionsDirectionsModeKey :
                          MKLaunchOptionsDirectionsModeDriving
                    }];

The names are stored inside the paramModel.

I assume this can be achieved by using addressDictionary? I tried kABPersonAddressStreetKey and kABPersonAddressCityKey, but neither will show up in the final route view.

2

There are 2 answers

1
GenericUserX On

Try creating your MKPlacemark as a variable and then modifying the title field on that variable like so:

MKPlacemark* placemark = [[MKPlacemark alloc]
                    initWithCoordinate:paramModel.fromCoordinate2D
                 addressDictionary:nil]];
placemark.title = @"Some Title";
placemark.subtitle = @"Some subtitle";

Then set the variable as the parameter in your map item constructor.

0
xi.lin On

Just find out that I can modify the name field of MKMapItem directly.

MKMapItem *currentLocation = [[MKMapItem alloc]
  initWithPlacemark:[[MKPlacemark alloc]
                    initWithCoordinate:paramModel.fromCoordinate2D
                 addressDictionary:nil]];
currentLocation.name = paramModel.fromName;