ios - 触摸 MKMapView 时的 ReverseGeocode CLLocation

标签 ios mkmapview cllocation

所以我试图通过触摸 MKMapView 来获取 CLLocation。然后我尝试对位置进行反向地理编码。

    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        // Not sure if this is the right method to get the location or not
        CLLocationCoordinate2D coordinate = [self.mapView convertPoint:[gestureRecognizer locationInView:self.mapView] toCoordinateFromView:self.mapView];

        CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];

        [geocoder reverseGeocodeLocation:pinLocation completionHandler:^(NSArray *placemarks, NSError *error) {
            if (placemarks && placemarks.count > 0) {
                CLPlacemark *topResult = [placemarks objectAtIndex:0];
                AddressAnnotation *anAddress = [[AddressAnnotation alloc] initWithPlacemark:topResult];     

                [self.mapView addAnnotation:anAddress];
                [self.addressesArray addObject:anAddress];
            }
            else {
                AddressAnnotation *anAddress = [[AddressAnnotation alloc] initWithCoordinate:coordinate];
                [self.mapView addAnnotation:anAddress];
                [self.addressesArray addObject:anAddress];
            }
        }];

通过 if/else 语句,我想要做的是触摸 map 并获取 CLLocation。如果我可以对位置进行反向地理编码,请放下带有地址的图钉。如果我无法对位置进行反向地理编码,请放下图钉并仅在 map 标注中显示纬度和经度。

这似乎不起作用。似乎即使我在某个位置触摸 map ,反向地理编码也会使我的图钉转到反向地理编码可以找到该位置的其他位置。但这不是我想要的行为。无论如何,我想放下图钉,如果我无法在标注中显示地址,只需显示纬度/经度。

如果我完全删除 revseGeocodeLocation:pinLocation 代码,并仅使用 else block 中的内容,那么我会得到纬度/经度,并且无论如何,图钉都会掉在那里。反向地理编码器是否有理由阻止我将图钉放置在我想要的位置?

作为旁注,任何人都可以确认我基于 UITapGestureRecognizer 计算 CLLocationCooperative2D 的方式是否正确?

谢谢!

最佳答案

coordinate基于点击点的计算看起来是正确的。

问题是当 coordinate是反向地理编码的,返回的地标可能是这些坐标和 CLPlacemark 附近的某个地方。对象本身 ( topResult ) 包含该地点在 topResult.location.coordinate 中的精确坐标(这是使用 initWithPlacemark 创建时注释所使用的坐标)。

除非用户恰好点击了可反向地理编码的坐标,否则地标将不会恰好位于他们点击的位置。

由于您希望将图钉准确放置在用户点击的位置,而不管找到的最近的地标如何,您可以做的是覆盖 AddressAnnotation 使用的坐标。当它使用地标初始化时。

例如,在调用 initWithPlacemark 之后(假设该方法使用地标的位置设置注释的 coordinate 属性),您可以随后覆盖它:

AddressAnnotation *anAddress = [[AddressAnnotation alloc] initWithPlacemark...
anAddress.coordinate = coordinate; // <-- replace with tapped coordinate

或者,您可以使用 initWithCoordinate相反并设置 title等手动使用 topResult 中的信息.

此外,如果找到的地标距离点击的坐标超过一定距离 x,请考虑在 title 前面添加前缀,例如“Near”/subtitle向用户表明图钉并不完全位于点击的位置(例如“埃菲尔铁塔附近”)。

关于ios - 触摸 MKMapView 时的 ReverseGeocode CLLocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12907932/

相关文章:

ios - 传递闭包稍后调用

ios - Error Domain=NSURLErrorDomain Code=-1005 "网络连接丢失

objective-c - 将 NSNumber 转换为 CLCoordinate

ios - 将 CLLocationCooperative2D 放入 Swift 的 CLLocation

mkmapview - 您如何以编程方式定位 mkmapregion 以适合注释

iPhone 头脑 Storm - CLLocation 在后台 - 每 15 分钟轮询一次

ios - 使用 UINavigationController 的 initWithNavigationBarClass :toolbarClass: 的正确方法

ios - Swift 中的分割操作

ios - CollectionView 单元格内的 MapView 不显示折线 Swift 4

ios - 快速放大位置