谷歌地图网址 - 如何在iOS中将谷歌地图中的lat和long传递给谷歌地图时显示地址?

问题描述 投票:-2回答:1

我的问题是,当我使用以下代码打开谷歌地图。

它显示lat很长的代替地址。但我想在谷歌地图中显示地址,或者有没有办法传递自定义地址和lat长,以便谷歌地图将在搜索栏上显示自定义地址(位于图像顶部)并在地图上显示确切的位置与标记。

我按照Google.ios-urlscheme提供的URLSchemes文档进行操作

但没有得到任何适合我的条件的解决方案。

UIApplication.shared.openURL(URL(string: "comgooglemaps://?q=40.00026321411133,-83.03424072265625&center=40.00026321411133,-83.03424072265625"))

enter image description here

ios swift google-maps url-scheme
1个回答
0
投票

您还必须使用lat-long传递地址地址名称:

import UIKit
import MapKit

func openMapForPlace() {

    let lat1 : NSString = self.venueLat
    let lng1 : NSString = self.venueLng

    let latitude:CLLocationDegrees =  lat1.doubleValue
    let longitude:CLLocationDegrees =  lng1.doubleValue

    let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
    let address = [CNPostalAddressStreetKey: address ?? "",
                   CNPostalAddressCityKey: city ?? "",
                   CNPostalAddressStateKey: state ?? "",
                   CNPostalAddressPostalCodeKey: zipCode,
                   CNPostalAddressISOCountryCodeKey: isoCountryCodeKey ?? ""]
    let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: address)
    let mapItem = MKMapItem(placemark: placemark)
    mapItem.name = "\(self.venueName)"
    let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
    mapItem.openInMaps(launchOptions: launchOptions)
}

swift代码将在下面:

import UIKit
import MapKit
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        openMapForPlace()
    }

    func openMapForPlace() {

        let latitude: CLLocationDegrees = 39.9517958
        let longitude: CLLocationDegrees = -75.1611398

        let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
        let address = [CNPostalAddressStreetKey: "1234 Market St",
                   CNPostalAddressCityKey: "Philadelphia",
                   CNPostalAddressStateKey: "Pennsylvania",
                   CNPostalAddressPostalCodeKey: "19107",
                   CNPostalAddressISOCountryCodeKey: "USA"]
        let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: address)
        let mapItem = MKMapItem(placemark: placemark)
        mapItem.name = "1234 Market St, Philadelphia, Pennsylvania, 19107"
        let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
        mapItem.openInMaps(launchOptions: launchOptions)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.