gpt4 book ai didi

iphone - 用于确定纬度/经度坐标集合的最小边界矩形的算法

转载 作者:太空狗 更新时间:2023-10-30 03:17:52 24 4
gpt4 key购买 nike

是否有一种算法可以确定一组纬度/经度坐标周围的最小边界矩形?

可以假设地球是平坦的,因为坐标不会相距太远。伪代码是可以的,但如果有人在 Objective-C 中做到了这一点,那就更好了。我想要做的是根据将在 map 上显示的点数设置 map 的缩放级别。

最佳答案

这是我在我的一个应用程序中使用的方法。

- (void)centerMapAroundAnnotations
{
// if we have no annotations we can skip all of this
if ( [[myMapView annotations] count] == 0 )
return;

// then run through each annotation in the list to find the
// minimum and maximum latitude and longitude values
CLLocationCoordinate2D min;
CLLocationCoordinate2D max;
BOOL minMaxInitialized = NO;
NSUInteger numberOfValidAnnotations = 0;

for ( id<MKAnnotation> a in [myMapView annotations] )
{
// only use annotations that are of our own custom type
// in the event that the user is browsing from a location far away
// you can omit this if you want the user's location to be included in the region
if ( [a isKindOfClass: [ECAnnotation class]] )
{
// if we haven't grabbed the first good value, do so now
if ( !minMaxInitialized )
{
min = a.coordinate;
max = a.coordinate;
minMaxInitialized = YES;
}
else // otherwise compare with the current value
{
min.latitude = MIN( min.latitude, a.coordinate.latitude );
min.longitude = MIN( min.longitude, a.coordinate.longitude );

max.latitude = MAX( max.latitude, a.coordinate.latitude );
max.longitude = MAX( max.longitude, a.coordinate.longitude );
}
++numberOfValidAnnotations;
}
}

// If we don't have any valid annotations we can leave now,
// this will happen in the event that there is only the user location
if ( numberOfValidAnnotations == 0 )
return;

// Now that we have a min and max lat/lon create locations for the
// three points in a right triangle
CLLocation* locSouthWest = [[CLLocation alloc]
initWithLatitude: min.latitude
longitude: min.longitude];
CLLocation* locSouthEast = [[CLLocation alloc]
initWithLatitude: min.latitude
longitude: max.longitude];
CLLocation* locNorthEast = [[CLLocation alloc]
initWithLatitude: max.latitude
longitude: max.longitude];

// Create a region centered at the midpoint of our hypotenuse
CLLocationCoordinate2D regionCenter;
regionCenter.latitude = (min.latitude + max.latitude) / 2.0;
regionCenter.longitude = (min.longitude + max.longitude) / 2.0;

// Use the locations that we just created to calculate the distance
// between each of the points in meters.
CLLocationDistance latMeters = [locSouthEast getDistanceFrom: locNorthEast];
CLLocationDistance lonMeters = [locSouthEast getDistanceFrom: locSouthWest];

MKCoordinateRegion region;
region = MKCoordinateRegionMakeWithDistance( regionCenter, latMeters, lonMeters );

MKCoordinateRegion fitRegion = [myMapView regionThatFits: region];
[myMapView setRegion: fitRegion animated: YES];

// Clean up
[locSouthWest release];
[locSouthEast release];
[locNorthEast release];
}

关于iphone - 用于确定纬度/经度坐标集合的最小边界矩形的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1303265/

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