[英]Programmatically open Maps app in iOS 6
在iOS 6之前,打開這樣的URL會打開(Google)地圖應用:
NSURL *url = [NSURL URLWithString:@"http://maps.google.com/?q=New+York"];
[[UIApplication sharedApplication] openURL:url];
現在有了新的Apple Maps實現,這只會將Mobile Safari打開到Google Maps。 如何在iOS 6中實現相同的行為? 如何以編程方式打開地圖應用並將其指向特定位置/地址/搜索/其他?
這是官方的Apple方式:
// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
// Create an MKMapItem to pass to the Maps app
CLLocationCoordinate2D coordinate =
CLLocationCoordinate2DMake(16.775, -3.009);
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate
addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:@"My Place"];
// Pass the map item to the Maps app
[mapItem openInMapsWithLaunchOptions:nil];
}
如果您想獲得駕駛或步行指令,您可以在+openMapsWithItems:launchOptions:
中的數組中包含帶有mapItemForCurrentLocation
的MKMapItem
,並相應地設置啟動選項。
// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
// Create an MKMapItem to pass to the Maps app
CLLocationCoordinate2D coordinate =
CLLocationCoordinate2DMake(16.775, -3.009);
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate
addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:@"My Place"];
// Set the directions mode to "Walking"
// Can use MKLaunchOptionsDirectionsModeDriving instead
NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeWalking};
// Get the "Current User Location" MKMapItem
MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation];
// Pass the current location and destination map items to the Maps app
// Set the direction mode in the launchOptions dictionary
[MKMapItem openMapsWithItems:@[currentLocationMapItem, mapItem]
launchOptions:launchOptions];
}
你可以保留你原來的iOS 5和較低的代碼在else
以后聲明if
。 請注意,如果顛倒openMapsWithItems:
數組中項目的順序,您將獲得從坐標到當前位置的路線。 您可以通過傳遞構造的MKMapItem
而不是當前位置圖項來使用它來獲取任意兩個位置之間的方向。 我沒試過。
最后,如果您有想要路線的地址(作為字符串),請使用地理編碼器通過CLPlacemark
創建MKPlacemark
。
// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:@"Piccadilly Circus, London, UK"
completionHandler:^(NSArray *placemarks, NSError *error) {
// Convert the CLPlacemark to an MKPlacemark
// Note: There's no error checking for a failed geocode
CLPlacemark *geocodedPlacemark = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc]
initWithCoordinate:geocodedPlacemark.location.coordinate
addressDictionary:geocodedPlacemark.addressDictionary];
// Create a map item for the geocoded address to pass to Maps app
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:geocodedPlacemark.name];
// Set the directions mode to "Driving"
// Can use MKLaunchOptionsDirectionsModeWalking instead
NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving};
// Get the "Current User Location" MKMapItem
MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation];
// Pass the current location and destination map items to the Maps app
// Set the direction mode in the launchOptions dictionary
[MKMapItem openMapsWithItems:@[currentLocationMapItem, mapItem] launchOptions:launchOptions];
}];
}
最好的方法是在MKMapItem
openInMapsWithLaunchOptions:launchOptions
上調用新的iOS 6方法openInMapsWithLaunchOptions:launchOptions
例:
CLLocationCoordinate2D endingCoord = CLLocationCoordinate2DMake(40.446947, -102.047607);
MKPlacemark *endLocation = [[MKPlacemark alloc] initWithCoordinate:endingCoord addressDictionary:nil];
MKMapItem *endingItem = [[MKMapItem alloc] initWithPlacemark:endLocation];
NSMutableDictionary *launchOptions = [[NSMutableDictionary alloc] init];
[launchOptions setObject:MKLaunchOptionsDirectionsModeDriving forKey:MKLaunchOptionsDirectionsModeKey];
[endingItem openInMapsWithLaunchOptions:launchOptions];
這將啟動從當前位置駕駛的導航。
我看到你發現了maps.apple.com url“scheme”。 這是一個不錯的選擇,因為它會自動將舊設備重定向到maps.google.com。 但對於iOS 6,您可能希望利用一個新類: MKMapItem 。
您感興趣的兩種方法:
這是一個使用在Swift中完成的nevan king的解決方案的類:
class func openMapWithCoordinates(theLon:String, theLat:String){
var coordinate = CLLocationCoordinate2DMake(CLLocationDegrees(theLon), CLLocationDegrees(theLat))
var placemark:MKPlacemark = MKPlacemark(coordinate: coordinate, addressDictionary:nil)
var mapItem:MKMapItem = MKMapItem(placemark: placemark)
mapItem.name = "Target location"
let launchOptions:NSDictionary = NSDictionary(object: MKLaunchOptionsDirectionsModeDriving, forKey: MKLaunchOptionsDirectionsModeKey)
var currentLocationMapItem:MKMapItem = MKMapItem.mapItemForCurrentLocation()
MKMapItem.openMapsWithItems([currentLocationMapItem, mapItem], launchOptions: launchOptions)
}
我發現使用http://maps.apple.com?q= ...鏈接設置首先在較舊的設備上打開Safari瀏覽器很煩人。
因此,對於iOS 5設備,通過引用maps.apple.com打開您的應用程序,步驟如下:
我認為(非常明顯和令人困惑的)第2步和第3步令用戶煩惱。 因此,我檢查操作系統版本,並在設備上運行maps.google.com或maps.apple.com(對於分別為ios 5或ios 6 OS版本)。
我對這個問題的研究使我得出以下結論:
如果你想打開谷歌地圖,而不是(或提供作為第二選項),你可以使用comgooglemaps://
和comgooglemaps-x-callback://
記錄URL方案在這里 。
在啟動url之前,從網址中刪除任何特殊字符,並用+替換空格。 這將為您省去一些麻煩:
NSString *mapURLStr = [NSString stringWithFormat: @"http://maps.apple.com/?q=%@",@"Limmattalstrasse 170, 8049 Zürich"];
mapURLStr = [mapURLStr stringByReplacingOccurrencesOfString:@" " withString:@"+"];
NSURL *url = [NSURL URLWithString:[mapURLStr stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
if ([[UIApplication sharedApplication] canOpenURL:url]){
[[UIApplication sharedApplication] openURL:url];
}
NSString *address = [NSString stringWithFormat:@"%@ %@ %@ %@"
,[dataDictionary objectForKey:@"practice_address"]
,[dataDictionary objectForKey:@"practice_city"]
,[dataDictionary objectForKey:@"practice_state"]
,[dataDictionary objectForKey:@"practice_zipcode"]];
NSString *mapAddress = [@"http://maps.apple.com/?q=" stringByAppendingString:[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"Map Address %@",mapAddress);
[objSpineCustomProtocol setUserDefaults:mapAddress :@"webSiteToLoad"];
[self performSegueWithIdentifier: @"provider_to_web_loader_segue" sender: self];
// VKJ
根據@ PJeremyMalouf的回答更新到Swift 4:
private func navigateUsingAppleMaps(to coords:CLLocation, locationName: String? = nil) {
let placemark = MKPlacemark(coordinate: coords.coordinate, addressDictionary:nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = locationName
let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
let currentLocationMapItem = MKMapItem.forCurrentLocation()
MKMapItem.openMaps(with: [currentLocationMapItem, mapItem], launchOptions: launchOptions)
}
不使用地圖,只是以編程方式使用UiButton動作,這對我來說非常有用。
// Button triggers the map to be presented.
@IBAction func toMapButton(sender: AnyObject) {
//Empty container for the value
var addressToLinkTo = ""
//Fill the container with an address
self.addressToLinkTo = "http://maps.apple.com/?q=111 Some place drive, Oak Ridge TN 37830"
self.addressToLinkTo = self.addressToLinkTo.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
let url = NSURL(string: self.addressToLinkTo)
UIApplication.sharedApplication().openURL(url!)
}
您可以稍微傳播一些代碼。 例如,我將變量作為類級變量,讓另一個函數填充它,然后按下按鈕時,只需獲取變量中的內容並將其擦除即可在URL中使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.