gpt4 book ai didi

iphone - 如何确定坐标是否在当前可见的 map 区域内?

转载 作者:行者123 更新时间:2023-12-03 19:30:16 38 4
gpt4 key购买 nike

我有数百个位置的列表,只想显示当前屏幕上这些位置的 MKPinAnnotation。屏幕从 2 英里半径内的用户当前位置开始。当然,用户可以滚动和缩放屏幕。现在,我等待 map 更新事件,然后循环遍历我的位置列表,并检查坐标,如下所示:

-(void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {
CGPoint point;
CLLocationCoordinate2D coordinate;

. . .
/* in location loop */
coordinate.latitude = [nextLocation getLatitude];
coordinate.longitude = [nextLocation getLongitude];

/* Determine if point is in view. Is there a better way then this? */
point = [mapView convertCoordinate:coordinate toPointToView:nil];
if( (point.x > 0) && (point.y>0) ) {
/* Add coordinate to array that is later added to mapView */
}

所以我要求转换该点在屏幕上的坐标(除非我误解了这种方法,这是很有可能的)。如果坐标不在屏幕上,那么我永远不会将其添加到 map View 中。

所以我的问题是,这是确定位置的纬度/经度是否出现在当前 View 中并应添加到 map View 的正确方法吗?或者我应该以不同的方式来做这件事?

最佳答案

在您的代码中,您应该为 toPointToView: 选项传递一个 View 。我给了它我的mapView。您还必须指定 x 和 y 的上限。

这是一些对我有用的代码(告诉我 map 上当前可见的注释,同时循环注释):

for (Shop *shop in self.shops) {
ShopAnnotation *ann = [ShopAnnotation annotationWithShop:shop];
[self.mapView addAnnotation:ann];

CGPoint annPoint = [self.mapView convertCoordinate:ann.coordinate
toPointToView:self.mapView];

if (annPoint.x > 0.0 && annPoint.y > 0.0 &&
annPoint.x < self.mapView.frame.size.width &&
annPoint.y < self.mapView.frame.size.height) {
NSLog(@"%@ Coordinate: %f %f", ann.title, annPoint.x, annPoint.y);
}
}

关于iphone - 如何确定坐标是否在当前可见的 map 区域内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1197398/

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