以编程方式打开Apple Maps [英] Open Apple Maps programmatically

查看:96
本文介绍了以编程方式打开Apple Maps的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在自己的Swift应用程序中打开Apple Maps App,但我只有邮政编码,城市和街。我没有坐标。我做了很多研究,但是只有一些使用协调信息的方法。

I want to open the Apple Maps App in my own Swift App, but I have only zipcode, city & street. I have NO Coordinates. I researched a lot, but there were only ways with using coordination information.

推荐答案

您可以将地址信息作为URL参数传递在打开地图应用程序所用的网址中。假设您要以白宫为中心打开地图应用。

You can just pass your address information as URL parameters in the URL with which you open the maps app. 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.

根据您的需求,一种可能更好的方法是使用 CLGeocoder

A potentially better approach, depending on your needs, would be to get the CLLocation of the address info you have using CLGeocoder.

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

这篇关于以编程方式打开Apple Maps的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆