0 iphone google-maps ios apple-maps swift
我为哥本哈根的自行车手制作了一个应用程序,但遇到了一些麻烦.当手机上没有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)},
Run Code Online (Sandbox Code Playgroud)
点击信息窗口时,需要显示Google地图导航(可以使用).但是,如果谷歌地图应用程序不可用,我该如何重新编写"标记功能",以便它将打开我的Apple地图?
为此,您需要检查是否已安装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)
}
}
Run Code Online (Sandbox Code Playgroud)
默认情况下,iOS不允许检查是否可以出于隐私原因打开URL.您需要comgooglemaps://在应用中将检查列入白名单.为此,请将此添加到您的应用中Info.plist:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>comgooglemaps</string>
</array>
Run Code Online (Sandbox Code Playgroud)
如果您已正确完成此操作openMaps(latitude:, logitude:)将打开Google地图(如果已安装),否则将打开Apple地图.只需更改闭包("标记功能")即可调用新方法.
当然,你也需要import MapKit为此工作.
| 归档时间: |
|
| 查看次数: |
1898 次 |
| 最近记录: |