gpt4 book ai didi

ios - 触摸 MKMapView 时的 ReverseGeocode CLLocation

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:19:15 26 4
gpt4 key购买 nike

所以我试图根据触摸 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 中的内容,那么我会得到纬度/经度,无论如何都会将 pin 放在那里。反向地理编码器是否有理由阻止我将图钉放在我想要的地方?

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

谢谢!

最佳答案

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com