10

How can I launch the default maps app for iOS6 and pass it a custom location?

For example:

[[MKMapItem setLocation:MyLocation] openInMapsWithLaunchOptions:nil];

I followed the example here, but was unable to figure it out. How can I launch the Google Maps iPhone application from within my own native application?

Community
  • 1
  • 1
Nils
  • 1,469
  • 3
  • 13
  • 17
  • You open MKMapItem with an specific location. You can use either the user location, or a location defined by a MKPlacemark. What is the problem you are having? – J2theC Oct 02 '12 at 15:06
  • OK thanks, i'am a newbie in xcode. How can I set a Placemark? for example with these Coordinates? latitude = 51.455919; longitude = 6.746442; thank's :) – Nils Oct 02 '12 at 17:32
  • 2
    MKPlacemark *placemark=[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(51.455919, 6.746442) addressDictionary:yourLocationAddressDictionary]; – J2theC Oct 02 '12 at 17:50
  • ok thanks :) thas now my code: MKPlacemark *placemark=[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(51.455919, 6.746442) addressDictionary:yourLocationAddressDictionary]; [[MKMapItem setLocation:placemark]openInMapsWithLaunchOptions:nil]; but i get 2 errors 1."Use of undeclared identifier 'yourLocationAddressDictionary'" 2. "No Known class method for selector 'setLocation'" – Nils Oct 02 '12 at 19:08
  • 2
    You need to create a dictionary with the address information. Note that to create this dictionary you need to use the keys from the address book. Take a look at one dictionary I create: NSDictionary *addressDictionary=@{(NSString *)kABPersonAddressStreetKey : yourLocationStreet, (NSString *)kABPersonAddressCityKey : yourLocationCity, (NSString *)kABPersonAddressStateKey : yourLocationState, (NSString *)kABPersonAddressZIPKey : yourLocationPostalCode, (NSString *)kABPersonAddressCountryKey : yourLocationCountry}; – J2theC Oct 02 '12 at 19:26
  • You can also geocode the coordinate you have, and that will give you a placemark with the proper address dictionary you can use. – J2theC Oct 02 '12 at 19:26
  • ok thanks, but is there no easyer way to open the Maps App at a particular location? I also know the coordinate. – Nils Oct 02 '12 at 20:10
  • You are opening at a particular location. You are passing a location with the MKMapItem – J2theC Oct 02 '12 at 21:12
  • Ok but I do not know how it works :( I'am a newbi in Xcode. Can you give me a Code which open the Maps App at 51.455919,6.746442 ? Thank you a lot :) – Nils Oct 02 '12 at 21:35

1 Answers1

34

Here is the code to open the Maps native application with a custom location:

double latitude = 35.0;
double longitude = 1.0;
MKPlacemark *placemark = [[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude, longitude) addressDictionary:nil] autorelease];
MKMapItem *mapItem = [[[MKMapItem alloc] initWithPlacemark:placemark] autorelease];
[mapItem setName:@"Name of your location"];
[mapItem openInMapsWithLaunchOptions:nil];
Eneko Alonso
  • 18,884
  • 9
  • 62
  • 84