以编程方式在iOS8,Xcode 7,Swift 2中打开Apple Maps [英] Open Apple Maps programmatically in iOS8, Xcode 7, Swift 2

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

问题描述

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

I want to open the Apple Maps App in my own Swift ios8 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参数在用于打开地图应用的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.
  }
}

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

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