swift iOS9之後,開啟第三方地圖導航
阿新 • • 發佈:2019-01-22
先看實現後的效果,會自動檢測手機安裝的第三方地圖。
step 1 在info.plist 裡新增 URL Scheme
百度地圖:baidumap://
高德地圖:iosamap://
google地圖:comgooglemaps://
騰訊地圖:qqmap://
step 2 程式碼部分
func creatOptionMenu(){ optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet) if(SHARE_APPLICATION.canOpenURL(NSURL(string:"qqmap://")!) == true){ let qqAction = UIAlertAction(title: "騰訊地圖", style: .Default, handler: { (alert: UIAlertAction!) -> Void in let urlString = "qqmap://map/routeplan?from=我的位置&type=drive&tocoord=\(self.centerLat),\(self.centerLng)&to=\(self.siteTitle)&coord_type=1&policy=0" let url = NSURL(string:urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!) SHARE_APPLICATION.openURL(url!) }) optionMenu.addAction(qqAction) } if(SHARE_APPLICATION.canOpenURL(NSURL(string:"iosamap://")!) == true){ let gaodeAction = UIAlertAction(title: "高德地圖", style: .Default, handler: { (alert: UIAlertAction!) -> Void in let urlString = "iosamap://navi?sourceApplication=app名&backScheme=iosamap://&lat=\(self.centerLat)&lon=\(self.centerLng)&dev=0&style=2" let url = NSURL(string:urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!) SHARE_APPLICATION.openURL(url!) }) optionMenu.addAction(gaodeAction) } if(SHARE_APPLICATION.canOpenURL(NSURL(string:"comgooglemaps://")!) == true){ let googleAction = UIAlertAction(title: "Google地圖", style: .Default, handler: { (alert: UIAlertAction!) -> Void in let urlString = "comgooglemaps://?x-source=app名&x-success=comgooglemaps://&saddr=&daddr=\(self.centerLat),\(self.centerLng)&directionsmode=driving" let url = NSURL(string:urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!) SHARE_APPLICATION.openURL(url!) }) optionMenu.addAction(googleAction) } let appleAction = UIAlertAction(title: "蘋果地圖", style: .Default, handler: { (alert: UIAlertAction!) -> Void in let loc = CLLocationCoordinate2DMake(self.centerLat, self.centerLng) let currentLocation = MKMapItem.mapItemForCurrentLocation() let toLocation = MKMapItem(placemark:MKPlacemark(coordinate:loc,addressDictionary:nil)) toLocation.name = self.siteTitle MKMapItem.openMapsWithItems([currentLocation,toLocation], launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: NSNumber(bool: true)]) }) optionMenu.addAction(appleAction) if(SHARE_APPLICATION.canOpenURL(NSURL(string:"baidumap://")!) == true){ let baiduAction = UIAlertAction(title: "百度地圖", style: .Default, handler: { (alert: UIAlertAction!) -> Void in let urlString = "baidumap://map/direction?origin={{我的位置}}&destination=latlng:\(self.centerLat),\(self.centerLng)|name=\(self.siteTitle)&mode=driving&coord_type=gcj02" let url = NSURL(string:urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!) SHARE_APPLICATION.openURL(url!) }) optionMenu.addAction(baiduAction) } let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler: { (alert: UIAlertAction!) -> Void in }) optionMenu.addAction(cancelAction) }
點選方法裡,實現彈出
self.presentViewController(optionMenu, animated: true, completion: nil)