gpt4 book ai didi

iphone - 带注释的 map 边界框

转载 作者:行者123 更新时间:2023-12-01 19:22:33 25 4
gpt4 key购买 nike

我有一种算法,可以为包含一组给定坐标的 map 计算边界框的大小和位置。虽然效果很好,但它产生的边界并不总是容纳我放置在通常位于边界框边缘上的坐标处的图钉:



...并且在大多数情况下,它会产生可接受的结果:

我已经考虑了一段时间,但是我还没有想出一种方法来修改我的算法,以确保图钉始终在边界框内。我的算法在下面列出,任何建议将不胜感激。 :)

MKMapPoint *points = malloc([coordinates count] * sizeof(MKMapPoint));    
MKMapPoint upperRightCorner;
MKMapPoint lowerLeftCorner;

for(int i = 0; i < [coordinates count]; i++)
{
CLLocation *location = [coordinates objectAtIndex:i];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(herp.coordinate.latitude, herp.coordinate.longitude);

MKMapPoint point = MKMapPointForCoordinate(coordinate);
points[i] = point;

if (i == 0) {
upperRightCorner = point;
lowerLeftCorner = point;
}
else {
if (point.x > upperRightCorner.x) upperRightCorner.x = point.x;
if (point.y > upperRightCorner.y) upperRightCorner.y = point.y;
if (point.x < lowerLeftCorner.x) lowerLeftCorner.x = point.x;
if (point.y < lowerLeftCorner.y) lowerLeftCorner.y = point.y;
}
}

MKMapRect boundingBox = MKMapRectMake(lowerLeftCorner.x, lowerLeftCorner.y,
upperRightCorner.x - lowerLeftCorner.x,
upperRightCorner.y - lowerLeftCorner.y);

最佳答案

我认为可能已经晚了,但是这是一个对我有用的解决方案,与您的解决方案非常相似,我最终在最后添加了填充。

- (MKCoordinateRegion)boundingBoxForAnnotations:(NSArray *)annotations { 

CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;

CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;

for (id<MKAnnotation> annotation in annotations) {

topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}

MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;

region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;

return region;

}

关于iphone - 带注释的 map 边界框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9493049/

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