iphone - 使用CLLocationDistance计算最外层坐标?

标签 iphone objective-c ios xcode ipad

我在我的 map 上放置了 1-12 个地标。我在计算最外面的两个点时遇到问题,因此我无法缩小 map 以显示所有图钉。

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];

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

1) 前两行给我一个 Bad receiver type 'CLLocationDegres' (aka double'} 错误。 2) 我真的不想以用户位置为中心点,理想情况下我希望有两个最远点的中心。

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

谢谢

编辑以显示如何计算 zoomLocatin。我基本上采用对数和纬度并确定最小值和最大值...也不确定这是否正确:

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;

最佳答案

也许您应该尝试使用此代码来完美匹配您的地标:

- (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];
}

因此,您必须定义 Padding、Maximum degree arc 和 Minimum zoom arc。对于前。应该是这样的:

#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

希望你会喜欢,干杯

关于iphone - 使用CLLocationDistance计算最外层坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14056556/

相关文章:

ios - 单例 UIViewController

iphone - 需要归档CLLocation数据

iphone - 如何从 GPS 坐标查找邮政编码?

objective-c - 无法运行首选项 Pane 应用程序

iphone - iOS SDK 编辑 PDF 添加图像

ios - FBSDKCoreKit.framework/FBSDKCoreKit : no matching architecture in universal wrapper

苹果手机 : UITableView Header row repeating after 6 rows

iphone - Objective-C : uncaught exception 'NSUnknownKeyException'

iphone - 在 iPhone 上使用 RemoteIO 和 AudioUnits 播放 MP3...可能吗?

ios - 单击更改多个 UIButton 的背景颜色