Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将字符串从我的应用程序传递到Apple Maps,以便它可以进行搜索_Ios_Swift_Maps - Fatal编程技术网

Ios 将字符串从我的应用程序传递到Apple Maps,以便它可以进行搜索

Ios 将字符串从我的应用程序传递到Apple Maps,以便它可以进行搜索,ios,swift,maps,Ios,Swift,Maps,我正试图找出将字符串值传递给apple maps的最佳方式,以便搜索它。我正在课堂上开发一个应用程序,它从数组中提取一个随机字符串,我希望能够调用设备上已有的apple maps并搜索所选的任何字符串。我查阅了MKLocalSearchRequest,我认为这可能是最简单的选择。我只是不知道如何将它与Apple Maps集成,而不是将它与应用程序内的mapkitview结合使用。这是我发现的一个方法,似乎它可以工作,我只是不知道如何声明它 class func openMaps(with ma

我正试图找出将字符串值传递给apple maps的最佳方式,以便搜索它。我正在课堂上开发一个应用程序,它从数组中提取一个随机字符串,我希望能够调用设备上已有的apple maps并搜索所选的任何字符串。我查阅了MKLocalSearchRequest,我认为这可能是最简单的选择。我只是不知道如何将它与Apple Maps集成,而不是将它与应用程序内的mapkitview结合使用。这是我发现的一个方法,似乎它可以工作,我只是不知道如何声明它

 class func openMaps(with mapItems: [MKMapItem], 
 launchOptions: [String : Any]? = nil) -> Bool
然后在
viewDidLoad
中:

    let geocoder = CLGeocoder()

    let locationString = "London"

    geocoder.geocodeAddressString(locationString) { (placemarks, error) in
        if let error = error {
            print(error.localizedDescription)
        } else {
            if let location = placemarks?.first?.location {
                let query = "?ll=\(location.coordinate.latitude),\(location.coordinate.longitude)"
                let urlString = "http://maps.apple.com/".appending(query)
                if let url = URL(string: urlString) {
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                }
            }
        }
    }

您有很多选择:

import MapKit
首先

Swift 4及以上 使用此版本获取地址的坐标

import CoreLocation

let myAddress = "One,Apple+Park+Way,Cupertino,CA,95014,USA"
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(myAddress) { (placemarks, error) in
    guard let placemarks = placemarks?.first else { return }
    let location = placemarks.location?.coordinate ?? CLLocationCoordinate2D()
    guard let url = URL(string:"http://maps.apple.com/?daddr=\(location.latitude),\(location.longitude)") else { return }
    UIApplication.shared.open(url)
}

可能是@rbaldwin的复制品我看了那个,它似乎对我毫无帮助all@CalebBartholomew如果它是一个字符串值,您可以使用rbaldwin建议的方法,请检查更多关于中的地图链接的信息,尽管它很旧,但仍在工作。@NajdanTomić您给我的文档链接与rbaldwin给我的非常好地配合。我能把它拼凑起来。谢谢,所以很多人都说我在使用
mapItem.name=“Taco Bell”
每当我执行搜索时,它会把我带到海洋中一个随机的地方。有没有办法让它自动在你周围搜索?这与上面的文档完美结合。非常感谢。
UIApplication.shared.openURL(NSURL(string:
    "comgooglemaps://?saddr=&daddr=\(place.latitude),\(place.longitude)&directionsmode=driving")! as URL)
@IBAction func action(_ sender: Any) {
    let latitude: CLLocationDegrees = 37.2
    let longitude: CLLocationDegrees = 22.9

    let regionDistance:CLLocationDistance = 10000
    let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
    let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
    let options = [
        MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
        MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
    ]
    let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
    let mapItem = MKMapItem(placemark: placemark)
    mapItem.name = "Place Name"
    mapItem.openInMaps(launchOptions: options)
    }
import CoreLocation

let myAddress = "One,Apple+Park+Way,Cupertino,CA,95014,USA"
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(myAddress) { (placemarks, error) in
    guard let placemarks = placemarks?.first else { return }
    let location = placemarks.location?.coordinate ?? CLLocationCoordinate2D()
    guard let url = URL(string:"http://maps.apple.com/?daddr=\(location.latitude),\(location.longitude)") else { return }
    UIApplication.shared.open(url)
}