作者:odile微笑头 | 来源:互联网 | 2023-02-11 16:52
我为哥本哈根的自行车手制作了一个应用程序,但遇到了一些麻烦.当手机上没有Google地图时,该应用需要打开Apple地图.
代码看起来像这样
// 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地图导航(可以使用).但是,如果谷歌地图应用程序不可用,我该如何重新编写"标记功能",以便它将打开我的Apple地图?
1> naglerrr..:
为此,您需要检查是否已安装Google地图.这可以很容易地完成:
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
:
LSApplicationQueriesSchemes
comgooglemaps
如果您已正确完成此操作openMaps(latitude:, logitude:)
将打开Google地图(如果已安装),否则将打开Apple地图.只需更改闭包("标记功能")即可调用新方法.
当然,你也需要import MapKit
为此工作.