首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用CLLocationDistance计算最外层的坐标?

使用CLLocationDistance计算最外层的坐标?
EN

Stack Overflow用户
提问于 2012-12-27 22:50:25
回答 1查看 1.4K关注 0票数 0

我在我的地图上放置了1-12个地标。我在计算最外面的两个点时遇到了麻烦,这样我就可以缩小地图以显示所有的引脚。

代码语言:javascript
复制
CLLocationDistance distLong = [zoomLocationMax.longitude getDistanceFrom:zoomLocationMin.longitude];
CLLocationDistance distLat = [zoomLocationMax.latitude getDistanceFrom:zoomLocationMin.latitude];

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(m_MapView.userLocation.coordinate, distLat, distLong);
MKCoordinateRegion adjustedRegion = [m_MapView regionThatFits:viewRegion];

m_MapView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[m_MapView setRegion:adjustedRegion animated:YES];

我一直在愚弄上面的代码,但我可以预见到一些问题:

2)我并不真的想让用户的位置作为中心点,理想情况下,我希望以最远的两个点为中心。

任何代码片段、示例或解释都会有很大帮助!

谢谢

编辑以显示zoomLocatin的计算方式。我基本上取对数和后数,并确定最小值和最大值。也不确定这是不是正确的:

代码语言:javascript
复制
CLLocationCoordinate2D zoomLocationMin;
CLLocationCoordinate2D zoomLocationMax;
if (coordinate.latitude < zoomLocationMin.latitude)
    zoomLocationMin.latitude = coordinate.latitude;
if (coordinate.longitude < zoomLocationMin.longitude)
    zoomLocationMin.longitude = coordinate.longitude;
if (coordinate.latitude > zoomLocationMax.latitude)
    zoomLocationMax.latitude = coordinate.latitude;
if (coordinate.longitude > zoomLocationMax.longitude)
    zoomLocationMax.longitude = coordinate.longitude;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-28 02:35:24

也许你应该尝试这个代码来处理与你的地标完全匹配的代码:

代码语言:javascript
复制
- (void)zoomMapViewToFitAnnotations:(MKMapView *)mapView animated:(BOOL)animated
{ 
    NSArray *annotations = mapView.annotations;
    int count = [mapView.annotations count];
    if ( count == 0) { return; } //return if no annotations

    //convert NSArray of id <MKAnnotation> into an MKCoordinateRegion that can be used to set the map size
    //can't use NSArray with MKMapPoint because MKMapPoint is not an id
    MKMapPoint points[count]; //C array of MKMapPoint struct
    for( int i=0; i<count; i++ ) //load points C array by converting coordinates to points
    {
        CLLocationCoordinate2D coordinate = [(id <MKAnnotation>)[annotations objectAtIndex:i] coordinate];
        points[i] = MKMapPointForCoordinate(coordinate);
    }
    //create MKMapRect from array of MKMapPoint
    MKMapRect mapRect = [[MKPolygon polygonWithPoints:points count:count] boundingMapRect];
    //convert MKCoordinateRegion from MKMapRect
    MKCoordinateRegion region = MKCoordinateRegionForMapRect(mapRect);

    //add padding so pins aren't scrunched on the edges
    region.span.latitudeDelta  *= ANNOTATION_REGION_PAD_FACTOR;
    region.span.longitudeDelta *= ANNOTATION_REGION_PAD_FACTOR;
    //but padding can't be bigger than the world
    if( region.span.latitudeDelta > MAX_DEGREES_ARC ) { region.span.latitudeDelta  = MAX_DEGREES_ARC; }
    if( region.span.longitudeDelta > MAX_DEGREES_ARC ){ region.span.longitudeDelta = MAX_DEGREES_ARC; }

    //and don't zoom in stupid-close on small samples
    if( region.span.latitudeDelta  < MINIMUM_ZOOM_ARC ) { region.span.latitudeDelta  = MINIMUM_ZOOM_ARC; }
    if( region.span.longitudeDelta < MINIMUM_ZOOM_ARC ) { region.span.longitudeDelta = MINIMUM_ZOOM_ARC; }
    //and if there is a sample of 1 we want the max zoom-in instead of max zoom-out
    if( count == 1 )
    { 
        region.span.latitudeDelta = MINIMUM_ZOOM_ARC;
        region.span.longitudeDelta = MINIMUM_ZOOM_ARC;
    }
    [mapView setRegion:region animated:animated];
}

因此,您必须定义填充、最大度数圆弧和最小变焦圆弧。对于Ex.应该是这样的:

代码语言:javascript
复制
#define MINIMUM_ZOOM_ARC 0.05 //approximately 1 miles (1 degree of arc ~= 69 miles)
#define ANNOTATION_REGION_PAD_FACTOR 1.25
#define MAX_DEGREES_ARC 360

希望你会喜欢,干杯

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14056556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档