How to open google maps app with a dropped pin ? - Swift

前端 未结 9 1789
无人共我
无人共我 2020-12-31 09:26

I have this code to open Google Maps App with specific location and it\'s working but what I need is to drop a pin on that location, is it possible ?

if (UIA         


        
相关标签:
9条回答
  • 2020-12-31 10:16

    Swift 3 , iOS 10

    let lat = 0.0
    let lon = 0.0
    
    if UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!) {
        UIApplication.shared.open(URL(string: "comgooglemaps://?center=\(lat),\(lon)&zoom=14&views=traffic&q=loc:\(lat),\(lon)")!, options: [:], completionHandler: nil)
    } else {
        print("Can't use comgooglemaps://")
        UIApplication.shared.open(URL(string: "http://maps.google.com/maps?q=loc:\(lat),\(lon)&zoom=14&views=traffic")!, options: [:], completionHandler: nil)
    }
    
    0 讨论(0)
  • 2020-12-31 10:16

    You should add this line in info.Plist file

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>googlechromes</string>
        <string>comgooglemaps</string>
    </array>
    

    code here:

    if dicDetails.hasValueForKey("latitude") && dicDetails.hasValueForKey("latitude") {
            if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
                if #available(iOS 10.0, *) {
                    UIApplication.shared.open(URL(string:"comgooglemaps://?center=\(dicDetails.stringValueForKey("latitude")),\(dicDetails.stringValueForKey("longitude"))&zoom=14&views=traffic&q=\(dicDetails.stringValueForKey("latitude")),\(dicDetails.stringValueForKey("longitude"))")!, options: convertToUIApplicationOpenExternalURLOptionsKeyDictionary([:]), completionHandler: nil)
                } else {
                    // Fallback on earlier versions
                     //https://www.google.com/maps/@
                    UIApplication.shared.openURL(URL(string:"https://www.google.com/?q=\(dicDetails.stringValueForKey("latitude")),\(dicDetails.stringValueForKey("longitude")),15z")!)
                }
            } else {
                //print("Can't use comgooglemaps://")
                UIApplication.shared.openURL(URL(string:"https://www.google.com/?q=\(dicDetails.stringValueForKey("latitude")),\(dicDetails.stringValueForKey("longitude")),15z")!)
            }
        }
    
    0 讨论(0)
  • 2020-12-31 10:18
    if UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!) {
            UIApplication.shared.open(URL(string: "comgooglemaps://?center=\(self.latitude),\(self.longitude)")!, options: [:], completionHandler: nil)
        } else {
             print("Opening in Apple Map")
    
        let coordinate = CLLocationCoordinate2DMake(self.latitude, self.longitude)
        let region = MKCoordinateRegionMake(coordinate, MKCoordinateSpanMake(0.01, 0.02))
        let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil)
        let mapItem = MKMapItem(placemark: placemark)
        let options = [
            MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: region.center),
            MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: region.span)]
        mapItem.name = theLocationName
        mapItem.openInMaps(launchOptions: options)
        }
    

    Use this code for opening in google map if it's installed else open in default apple map.

    0 讨论(0)
提交回复
热议问题