MKMapItem with specific Location
Asked Answered
S

1

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?

Stoichiometry answered 2/10, 2012 at 15:2 Comment(9)
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?Bunk
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 :)Stoichiometry
MKPlacemark *placemark=[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(51.455919, 6.746442) addressDictionary:yourLocationAddressDictionary];Bunk
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'"Stoichiometry
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};Bunk
You can also geocode the coordinate you have, and that will give you a placemark with the proper address dictionary you can use.Bunk
ok thanks, but is there no easyer way to open the Maps App at a particular location? I also know the coordinate.Stoichiometry
You are opening at a particular location. You are passing a location with the MKMapItemBunk
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 :)Stoichiometry
W
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];
Wimbush answered 9/10, 2012 at 5:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.