如何使用MapKit从坐标获取地址?
当我在地图上长按它获取坐标时,我有这个代码:
func didLongPressMap(sender: UILongPressGestureRecognizer) {
if sender.state == UIGestureRecognizerState.Began {
let touchPoint = sender.locationInView(self.mapView)
let touchCoordinate = self.mapView.convertPoint(touchPoint, toCoordinateFromView: self.mapView)
var annotation = MKPointAnnotation()
annotation.coordinate = touchCoordinate
annotation.title = "Your position"
self.mapView.addAnnotation(annotation) //drops the pin
println("lat: \(touchCoordinate.latitude)")
var num = (touchCoordinate.latitude as NSNumber).floatValue
var formatter = NSNumberFormatter()
formatter.maximumFractionDigits = 4
formatter.minimumFractionDigits = 4
var str = formatter.stringFromNumber(num)
println("long: \(touchCoordinate.longitude)")
var num1 = (touchCoordinate.longitude as NSNumber).floatValue
var formatter1 = NSNumberFormatter()
formatter1.maximumFractionDigits = 4
formatter1.minimumFractionDigits = 4
var str1 = formatter1.stringFromNumber(num1) …
Run Code Online (Sandbox Code Playgroud) 我有一个移动CLPlacemark
对象并使用它们的应用程序,我想对与它们交互的几个组件进行单元测试。为此,我希望能够使用生成CLPlacemark
具有已知值的mock的方法从 MapKit 中删除对真正反向地理定位的调用。
CLPlacemark
只有一个初始化程序(复制初始化程序)。但在文档中,它说:
地标对象通常由 CLGeocoder 对象生成,但您也可以自己显式创建它们。
但是,大多数成员都是只读的,因此我不确定如何创建具有自定义值的成员。是否可以在 Swift 中以这种方式设置内部属性?如果没有,关于它们在上述引文中的含义有什么想法吗?
我以前能够使用以下Swift 3代码从CLPlacemark获取完整地址字符串:
let addressList = placemark.addressDictionary?["FormattedAddressLines"] as? [String]
let address = addressList!.joined(separator: "\n")
Run Code Online (Sandbox Code Playgroud)
addressDictionary现在已在swift 4中弃用.
我可以提取每个CLPlacemark地址变量字符串(name,country postalCode等),但我想知道是否有更简单的方法.
我知道有一个postalAddress var,其类型为CNPostalAddress,但不确定如何将其转换为String.
任何人都可以建议清洁工从 CLPlacemark 创建地址字符串。
目前我正在使用这个扩展
extension CLPlacemark {
func makeAddressString() -> String {
var address = ""
if subThoroughfare != nil { address = address + " " + subThoroughfare! }
if thoroughfare != nil { address = address + " " + thoroughfare! }
if locality != nil { address = address + " " + locality! }
if administrativeArea != nil { address = address + " " + administrativeArea! }
if postalCode != nil { address = address …
Run Code Online (Sandbox Code Playgroud) 我正在使用CLGeocoder
反向地理定位并得到数组CLPlacemark
.当我在美国境外使用GPS(即-27,127)然后访问时placemark.postalCode
,应用程序崩溃:
"致命错误:在打开一个Optional值时意外地发现了nil".
看来,这placemark.postalCode
是nil
没有邮政编码的地方.但是postalCode
Swift中的返回类型是String!
:
var postalCode: String! { get } // zip code, eg. 95014
所以我甚至无法测试nil
,因为崩溃是由吸气剂引起的postalCode
.
任何想法如何防止这种崩溃?谢谢!
虽然这里有一个类似的问题,但它没有提供答案,至少不是一般问题.
我的问题是:由于CoreLocation
地理编码是速率有限的,我开发应用程序的(网络)服务提供了自己的后备地理编码服务,我想使用这个自定义地理编码服务,以防我达到Apple的速率限制.此外,我觉得避免使用此自定义REST API返回的结果的自定义数据类型是完全有意义的,因此希望使用返回的数据来生成CLPlacemark
s.然而,该文档指出CLPlacemark
如属性location, locality, administrativeArea
等是read-only
.因此,我创建了一个子类,CLPlacemark
将所需的属性合成到我可以访问的私有变量上,即:
// interface: (.h)
@interface CustomPlacemark : CLPlacemark
- (nonnull id)initWithLocation: (nonnull CLLocation *)location
locality: (nullable NSString *)locality
administrativeArea: (nullable NSString *)adminArea
country: (nullable NSString *)country;
@end
// implementation (.m)
@implementation CustomPlacemark
@synthesize location = _location;
@synthesize locality = _locality;
@synthesize country = _country;
@synthesize administrativeArea = _administrativeArea;
- (nonnull id)initWithLocation: (nonnull CLLocation *)location
locality: (nullable NSString *)locality
administrativeArea: (nullable NSString *)adminArea …
Run Code Online (Sandbox Code Playgroud) clplacemark ×6
ios ×5
swift ×4
clgeocoder ×2
extend ×1
mapkit ×1
objective-c ×1
optional ×1
string ×1
swift4 ×1
xcode ×1
xcode9 ×1