gpt4 book ai didi

ios - 在 MKMapView 中围绕当前位置添加随机 MKAnnotations

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:12:03 27 4
gpt4 key购买 nike

我有一个显示当前用户位置的 MKMapView。当我点击导航栏中的一个按钮时,我想随机放置 10 个 MKAnnotation 引脚。这些可以随机掉落到任何地方,但只能在当前可见 map 区域内和当前位置周围。

如何做这样的事情?有没有办法在用户位置周围但在 map 区域内设置长/纬度范围?那么,我可以从这个范围内随机选择吗?

基本上,我需要找到当前 MKCoordinateRegion 中可用的坐标。

有没有更好的方法来解决这个问题?

最佳答案

您可以使用用户位置与其他位置之间的距离来获取注释

查看下面的代码

#define DISTANCE_RADIUS     10.0    // in KM
-(NSArray *)findNearMe:(NSArray *)lounges {
NSMutableArray *arNearme = [NSMutableArray array];

CLLocation *currentLocation;
for(NSDictionary *d in lounges)
{

currentLocation = [[CLLocation alloc] initWithLatitude:29.33891 longitude:48.077202];

CGFloat latitude=[[d valueForKey:@"latitude"] floatValue];
CGFloat longitude=[[d valueForKey:@"longitude"] floatValue];
CLLocation *newPinLocation=[[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
double distanceValue=[currentLocation distanceFromLocation:newPinLocation];

if(distanceValue/1000.0<=DISTANCE_RADIUS) {
[arNearme addObject:d];
}
}
return arNearme;
}

它将返回用户位置附近 1 到 1000 公里范围内的数组。使用注释数组并将您的注释显示在带有用户位置的 map View 中。

关于ios - 在 MKMapView 中围绕当前位置添加随机 MKAnnotations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10695323/

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