Ios Swift:当未安装谷歌地图时,应用程序需要打开苹果地图
我已经为哥本哈根自行车手开发了一个应用程序,但遇到了一些麻烦。 当手机上没有谷歌地图时,该应用程序需要打开苹果地图 代码如下所示Ios Swift:当未安装谷歌地图时,应用程序需要打开苹果地图,ios,iphone,swift,google-maps,apple-maps,Ios,Iphone,Swift,Google Maps,Apple Maps,我已经为哥本哈根自行车手开发了一个应用程序,但遇到了一些麻烦。 当手机上没有谷歌地图时,该应用程序需要打开苹果地图 代码如下所示 // OUTLETS! @IBOutlet weak var googleMapsView: GMSMapView! // VARIABLES! var locationManager = CLLocationManager() var M1: GMSMarker! var M2: GMSMarker! //Marker funtioncs var mar
// OUTLETS!
@IBOutlet weak var googleMapsView: GMSMapView!
// VARIABLES!
var locationManager = CLLocationManager()
var M1: GMSMarker!
var M2: GMSMarker!
//Marker funtioncs
var markerFunctions: [GMSMarker: (() -> Void)]!
override func viewDidLoad() {
super.viewDidLoad()
// GET LOCATION WHILE USING APP
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
locationManager.startMonitoringSignificantLocationChanges()
initGoogleMaps()
}
// START GOOGLE MAPS!
func initGoogleMaps() {
// MARKERS
M1 = GMSMarker()
M1.position = CLLocationCoordinate2D(latitude: 55.65208178045790, longitude: 12.527047696518300)
M1.title = "Sjælør St."
M1.snippet = "Press for navigation"
M1.map = self.googleMapsView
M2 = GMSMarker()
M2.position = CLLocationCoordinate2D(latitude: 55.67530850095370, longitude: 12.583165039072400)
M2.title = "Børsen (Slotsholmsgade)"
M2.snippet = "Press for navigation"
M2.map = self.googleMapsView
// Marker functions
markerFunctions = [
M1: { UIApplication.shared.open(NSURL(string: "comgooglemaps-x-callback://" +
"?daddr=55.65208178045790,12.527047696518300&directionsmode=bicycling&zoom=17")! as URL)},
M2: { UIApplication.shared.open(NSURL(string: "comgooglemaps-x-callback://" +
"?daddr=55.67530850095370,12.583165039072400&directionsmode=bicycling&zoom=17")! as URL)},
点击信息窗口时,需要显示谷歌地图的导航(它可以工作)。但是,如果Google Maps应用程序不可用,我如何重新编写“标记函数”,以便它打开I Apple Maps?要执行此操作,您需要检查是否安装了Google Maps。这可以相当容易地做到:
func openMaps(latitude: CLLocationDegrees, longitude: CLLocationDegrees) {
let googleMapsInstalled = UIApplication.shared.canOpenURL(URL(string: "comgooglemaps://")!)
if googleMapsInstalled {
UIApplication.shared.open(URL(string: "comgooglemaps-x-callback://" +
"?daddr=\(latitude),\(longitude)&directionsmode=bicycling&zoom=17")!)
} else {
let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.openInMaps(launchOptions: nil)
}
}
默认情况下,iOS不允许检查是否可以出于隐私原因打开URL。您需要在白名单中检查应用程序中的comgooglemaps://
。要执行此操作,请将其添加到您的应用程序Info.plist
:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>comgooglemaps</string>
</array>
LSApplicationQueriesSchemes
谷歌地图
如果您正确执行此操作,openMaps(纬度:,逻辑度:)
将在安装谷歌地图后打开谷歌地图,否则将打开苹果地图。只需更改闭包(“标记函数”)即可调用新方法
当然,您还需要
导入MapKit
,才能使其正常工作。?检查“canOpenURL()”以了解哪一个可用。@nagleer非常感谢您的快速响应。我已经看过了,但它不工作,因为我需要的功能,为几个信息窗口工作。例如,如果您在信息窗口#1上加上标签,则指向XXXX,但如果您在信息窗口#2上加上标签,则指向YYYY。您知道如何为不同的信息窗口选项卡编写代码吗?您是否尝试阅读并理解我的函数?它已经做到了。它获取坐标并显示你给它的任何位置。另外,您在问题中没有告诉as关于InfoWindow
,也没有提到那些InfoWindow
是什么。根据现有资料,我再也帮不了你了。你需要澄清你想做什么。另外,请再次检查我的示例,因为我怀疑它已经完成了您希望它完成的任务。