我试图找出将字符串值传递给Apple地图的最佳方法,以便我可以搜索它。我正在使用类中的应用程序从数组中提取随机字符串,我希望能够调用设备上已有的苹果映射并搜索所选的任何字符串。我已经查找了MKLocalSearchRequest,我认为这可能是最简单的选择,我不知道如何将它与Apple Maps集成,而不是在应用程序内部使用mapkitview。这是我发现的一种方法,似乎它可以工作我只是不确定如何声明它。
class func openMaps(with mapItems: [MKMapItem],
launchOptions: [String : Any]? = nil) -> Bool
答案 0 :(得分:0)
import CoreLocation
然后在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)
}
}
}
}
答案 1 :(得分:0)
您可以选择多种方式:
import MapKit
第一
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)
}
答案 2 :(得分:0)
使用此:
UIApplication.shared.open(URL(string:"http://maps.apple.com/?address=yourAddress")!)