简体   繁体   English

以编程方式打开 Apple 地图

[英]Open Apple Maps programmatically

I want to open the Apple Maps App in my own Swift App, but I have only zipcode, city & street.我想在我自己的 Swift 应用程序中打开 Apple Maps 应用程序,但我只有邮政编码、城市和街道。 I have NO Coordinates.我没有坐标。 I researched a lot, but there were only ways with using coordination information.我研究了很多,但只有使用协调信息的方法。

You can just pass your address information as URL parameters in the URL with which you open the maps app.您可以在打开地图应用程序的 URL 中将地址信息作为 URL 参数传递。 Say you wanted the maps app to open centered on The White House.假设您希望地图应用程序以白宫为中心打开。

UIApplication.sharedApplication().openURL(NSURL(string: "http://maps.apple.com/?address=1600,PennsylvaniaAve.,20500")!)

The Maps app opens with the ugly query string in the search field but it shows the right location.地图应用程序打开时在搜索字段中显示了丑陋的查询字符串,但它显示了正确的位置。 Note that the city and state are absent from the search query, it's just the street address and the zip.请注意,搜索查询中没有城市和州,只有街道地址和邮编。

A potentially better approach, depending on your needs, would be to get the CLLocation of the address info you have using CLGeocoder .根据您的需要,一种可能更好的方法是使用CLGeocoder获取您拥有的地址信息的 CLLocation。

let geocoder = CLGeocoder()
let str = "1600 Pennsylvania Ave. 20500" // A string of the address info you already have
geocoder.geocodeAddressString(str) { (placemarksOptional, error) -> Void in
  if let placemarks = placemarksOptional {
    print("placemark| \(placemarks.first)")
    if let location = placemarks.first?.location {
      let query = "?ll=\(location.coordinate.latitude),\(location.coordinate.longitude)"
      let path = "http://maps.apple.com/" + query
      if let url = NSURL(string: path) {
        UIApplication.sharedApplication().openURL(url)
      } else {
        // Could not construct url. Handle error.
      }
    } else {
      // Could not get a location from the geocode request. Handle error.
    }
  } else {
    // Didn't get any placemarks. Handle error.
  }
}

Swift 4 and above Swift 4 及以上

Use this version to get the coordinates for the Address, there is also a way to open it directly with the address, but that is susceptible to errors用这个版本获取地址的坐标,也有直接用地址打开的方法,但是容易出错

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)
}

Apple has a Documentation about the Map URL Scheme. Apple 有一个关于 Map URL Scheme 的文档。 Look here: https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html#//apple_ref/doc/uid/TP40007899-CH5-SW1看这里: https : //developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html#//apple_ref/doc/uid/TP40007899-CH5-SW1

Using Swift 4 and Xcode 9使用 Swift 4 和 Xcode 9

At the top:在顶部:

import CoreLocation

Then:然后:

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)
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM