标签: clplacemark

将坐标转换为城市名称?

如何使用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)

mapkit ios clgeocoder swift clplacemark

46
推荐指数
7
解决办法
5万
查看次数

使用自定义值创建 CLPlacemark 以进行测试

我有一个移动CLPlacemark对象并使用它们的应用程序,我想对与它们交互的几个组件进行单元测试。为此,我希望能够使用生成CLPlacemark具有已知值的mock的方法从 MapKit 中删除对真正反向地理定位的调用。

CLPlacemark只有一个初始化程序(复制初始化程序)。但在文档中,它说:

地标对象通常由 CLGeocoder 对象生成,但您也可以自己显式创建它们。

但是,大多数成员都是只读的,因此我不确定如何创建具有自定义值的成员。是否可以在 Swift 中以这种方式设置内部属性?如果没有,关于它们在上述引文中的含义有什么想法吗?

core-location ios swift clplacemark

5
推荐指数
1
解决办法
742
查看次数

使用Swift 4/Xcode 9从CLPLacemark获取完整的地址字符串

我以前能够使用以下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.

ios clplacemark swift4 xcode9

4
推荐指数
1
解决办法
1940
查看次数

从 CLPlacemark 创建地址字符串

任何人都可以建议清洁工从 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)

string optional swift clplacemark

3
推荐指数
2
解决办法
2307
查看次数

CLPlacemark在Swift中崩溃

我正在使用CLGeocoder反向地理定位并得到数组CLPlacemark.当我在美国境外使用GPS(即-27,127)然后访问时placemark.postalCode,应用程序崩溃:

"致命错误:在打开一个Optional值时意外地发现了nil".

看来,这placemark.postalCodenil没有邮政编码的地方.但是postalCodeSwift中的返回类型是String!:

var postalCode: String! { get } // zip code, eg. 95014

所以我甚至无法测试nil,因为崩溃是由吸气剂引起的postalCode.

任何想法如何防止这种崩溃?谢谢!

xcode ios clgeocoder swift clplacemark

2
推荐指数
1
解决办法
914
查看次数

扩展CLPlacemark会导致EXC BAD ACCESS

虽然这里有一个类似的问题,但它没有提供答案,至少不是一般问题.

我的问题是:由于CoreLocation地理编码是速率有限的,我开发应用程序的(网络)服务提供了自己的后备地理编码服务,我想使用这个自定义地理编码服务,以防我达到Apple的速率限制.此外,我觉得避免使用此自定义REST API返回的结果的自定义数据类型是完全有意义的,因此希望使用返回的数据来生成CLPlacemarks.然而,该文档指出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)

objective-c extend core-location ios clplacemark

2
推荐指数
1
解决办法
495
查看次数