带注释的地图边界框 [英] Map Bounding Box with Annotations

查看:127
本文介绍了带注释的地图边界框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个算法可以计算包含一组给定坐标的地图的边界框的大小和位置。虽然它的工作原理非常完美,但它所产生的界限并不总是容纳我经常位于边界框边缘的坐标处的推针:



...再次,它会在大多数时间产生可以接受的结果: b
$ b



我已经仔细研究了一段时间,但我一直没能想出一种方法来修改我的算法,以确保推针总是在边界框内。我的算法列在下面,任何建议将不胜感激。 :)

  MKMapPoint * points = malloc([坐标计数] * sizeof(MKMapPoint)); 
MKMapPoint upperRightCorner;
MKMapPoint lowerLeftCorner;

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

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

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


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


解决方案

我认为可能会迟到,但这里有一个解决方案
$ b - (MKCoordinateRegion)boundingBoxForAnnotations: (NSArray *)注释{

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

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

(注解中的id< MKAnnotation>注释){

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

返回区域;

}


I have an algorithm that calculates the size and location of a bounding box for a map that contains a set of given coordinates. While it works perfectly, the bounds that it produces don't always accommodate the push pin that I place at coordinates that often lie right on the edge of the bounding box:

...and again, it produces an acceptable result most of the time:

I've mulled it over for awhile, but I haven't been able to think of a way to modify my algorithm to ensure that the push pins are always within the bounding box. My algorithm is listed below, and any suggestions would be greatly appreciated. :)

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

解决方案

I think it might be late, but here's a solution it works for me, quite similar to yours, I ended up adding a padding at the end.

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

}

这篇关于带注释的地图边界框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆