पूर्व IOS 6
आप वहाँ से आप मार्ग, एक सड़क का पता या स्थान के लिए करने के लिए नक्शे कोर स्थान का उपयोग करने के वर्तमान स्थान प्राप्त करने की आवश्यकता है, लेकिन यह अक्षांश / देशांतर जोड़ी के साथ, आप प्राप्त कर सकते हैं। इस तरह:
CLLocationCoordinate2D currentLocation = [self getCurrentLocation];
// this uses an address for the destination. can use lat/long, too with %f,%f format
NSString* address = @"123 Main St., New York, NY, 10001";
NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%@",
currentLocation.latitude, currentLocation.longitude,
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
अंत में, यदि आप स्पष्ट रूप से वर्तमान स्थान प्राप्त करने CoreLocation उपयोग करने से बचें, और उपयोग करना चाहते हैं करना चाहते हैं @"http://maps.google.com/maps?saddr=Current+Location&daddr=%@"बजाय यूआरएल, तो इस लिंक है कि मैं नीचे टिप्पणी में प्रदान की देख कैसे स्थानीय बनाना के लिए वर्तमान + स्थान स्ट्रिंग। हालांकि, अगर आप एक और गैर-दस्तावेजी सुविधा का लाभ ले रहे हैं, और के रूप में जेसन McCreary नीचे बताते हैं, यह आगामी रिलीज़ में मज़बूती से काम न करें।
आईओएस 6 के लिए अद्यतन
मूल रूप से, मैप्स गूगल के नक्शे का इस्तेमाल किया है, लेकिन अब, एप्पल और गूगल अलग नक्शे क्षुधा की है।
1) यदि आप Google नक्शे का उपयोग कर मार्ग करना चाहते हैं, का उपयोग comgooglemaps यूआरएल स्कीम :
NSString* url = [NSString stringWithFormat: @"comgooglemaps://?daddr=%@&directionsmode=driving",
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
BOOL opened = [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
2) का उपयोग करने के लिए एप्पल मैप्स, आप नए उपयोग कर सकते हैं MKMapItemआईओएस 6 के लिए वर्ग एप्पल एपीआई डॉक्स यहाँ देखें
मूल रूप से, आप कुछ इस तरह का उपयोग करेगा, यदि गंतव्य के लिए मार्ग निर्देशांक ( latlong):
MKPlacemark* place = [[MKPlacemark alloc] initWithCoordinate: latlong addressDictionary: nil];
MKMapItem* destination = [[MKMapItem alloc] initWithPlacemark: place];
destination.name = @"Name Here!";
NSArray* items = [[NSArray alloc] initWithObjects: destination, nil];
NSDictionary* options = [[NSDictionary alloc] initWithObjectsAndKeys:
MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsDirectionsModeKey, nil];
[MKMapItem openMapsWithItems: items launchOptions: options];
आदेश में एक ही कोड में दोनों iOS 6+ और पूर्व IOS 6 का समर्थन करने के लिए, मैं इस कोड एप्पल पर है ऐसा ही कुछ का उपयोग कर की सलाह देते हैं MKMapItemएपीआई डॉक पेज:
Class itemClass = [MKMapItem class];
if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
// iOS 6 MKMapItem available
} else {
// use pre iOS 6 technique
}
यह ग्रहण करेंगे कि अपने Xcode बेस SDK, iOS 6 (या है नवीनतम आईओएस )।