打开苹果的地图程序

问题描述 投票:5回答:3

我想在我自己的斯威夫特应用程序打开苹果的地图应用程序,但我只有邮政编码,城市和街道。我有没有坐标。我研究了很多,但只有用协调方式的信息。

iphone ios8 swift2 xcode7 apple-maps
3个回答
15
投票

您只需将您的地址信息与您打开地图应用的网址URL参数。假设你的地图应用开放集中在白宫。

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

该地图应用在搜索领域的丑陋查询字符串打开,但它显示了正确的位置。需要注意的是城市和国家都来自搜索查询不存在的,它只是在街道地址和拉链。

一个潜在的更好的方法,根据您的需要,将让您使用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.
  }
}

3
投票

使用夫特4和9的Xcode

在顶部:

import CoreLocation

然后:

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

2
投票

迅速4及以上

     let myAddress = "One,Apple+Park+Way,Cupertino,CA,95014,USA"
    if let url = URL(string:"http://maps.apple.com/?address=\(myAddress)") {
        UIApplication.shared.open(url)
    }

苹果拥有约地图网址体系文件。看看这里:https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html#//apple_ref/doc/uid/TP40007899-CH5-SW1

© www.soinside.com 2019 - 2024. All rights reserved.