iPhone MKMapView:设置地图区域以显示地图上的所有图钉

时间:2011-02-18 10:29:29

标签: ios mkmapview html region

我正在尝试设置地图区域(中心和跨度),以便地图同时显示所有引脚注释。

我无法将长/纬坐标从NSString转换为double,resp。用它们进行计算。这是我正在使用的代码:

- (void)updateMemberPins{

//calculate new region to show on map
double center_long = 0.0f;
double center_lat = 0.0f;
double max_long = 0.0f;
double min_long = 0.0f;
double max_lat = 0.0f;
double min_lat = 0.0f;

for (Member *member in members) {

    //find min and max values
    if ([member.locLat doubleValue] > max_lat) {max_lat = [member.locLat doubleValue];}
    if ([member.locLat doubleValue] < min_lat) {min_lat = [member.locLat doubleValue];}
    if ([member.locLong doubleValue] > max_long) {max_long = [member.locLong doubleValue];}
    if ([member.locLong doubleValue] < min_long) {min_long = [member.locLong doubleValue];}

    //sum up long and lang to get average later
    center_lat = center_lat + [member.locLat doubleValue];
    center_long = center_long + [member.locLong doubleValue];
}

//calculate average long / lat
center_lat = center_lat / [members count];
center_long = center_long / [members count];

NSLog(@"center long: %d, center lat: %d", center_long, center_lat);
NSLog(@"max_long: %d, min_long: %d, max_lat: %d, min_lat: %d", max_long, min_long, max_lat, min_lat);

//create new region and set map
CLLocationCoordinate2D coord = {latitude: center_lat, longitude: center_long};
MKCoordinateSpan span = MKCoordinateSpanMake(abs(max_lat) + abs(min_lat), abs(max_long) + abs(min_long));
MKCoordinateRegion region = {coord, span};
[resultMapView setRegion:region];

//remove all pins from map
[resultMapView removeAnnotations:resultMapView.annotations];

//show member pins
for (id member in members) {
    [resultMapView addAnnotation:(Member *) member];
}

}

日志输出的结果是:

  

中心长:-1946827116,中心纬度:1075651472

     

max_long:-6267216,min_long:1076018553,max_lat:0,min_lat:0

我认为问题来自(错误地)将值从NSString转换为double,但是我找不到使其工作的方法......位置字符串的格式类似于'43 .5686473'。

任何提示? Cheerz

4 个答案:

答案 0 :(得分:22)

那些寻找类似内容的人的最终工作代码:

    - (void)updateMemberPins{

    //remove all pins from map
    [resultMapView removeAnnotations:resultMapView.annotations];

    if ([members count] > 0) {

        @try {
            //calculate new region to show on map           
            Member *firstMember = [members objectAtIndex:0];
            double max_long = [firstMember.locLong doubleValue];
            double min_long = [firstMember.locLong doubleValue];
            double max_lat = [firstMember.locLat doubleValue];
            double min_lat = [firstMember.locLat doubleValue];

            //find min and max values
            for (Member *member in members) {
                if ([member.locLat doubleValue] > max_lat) {max_lat = [member.locLat doubleValue];}
                if ([member.locLat doubleValue] < min_lat) {min_lat = [member.locLat doubleValue];}
                if ([member.locLong doubleValue] > max_long) {max_long = [member.locLong doubleValue];}
                if ([member.locLong doubleValue] < min_long) {min_long = [member.locLong doubleValue];}
            }

            //calculate center of map
            double center_long = (max_long + min_long) / 2;
            double center_lat = (max_lat + min_lat) / 2;

            //calculate deltas
            double deltaLat = abs(max_lat - min_lat);
            double deltaLong = abs(max_long - min_long);

            //set minimal delta
            if (deltaLat < 5) {deltaLat = 5;}
            if (deltaLong < 5) {deltaLong = 5;}

            //debug
            //NSLog(@"center long: %f, center lat: %f", center_long, center_lat);
            //NSLog(@"max_long: %f, min_long: %f, max_lat: %f, min_lat: %f", max_long, min_long, max_lat, min_lat);

            //create new region and set map
            CLLocationCoordinate2D coord = {latitude: center_lat, longitude: center_long};
            MKCoordinateSpan span = MKCoordinateSpanMake(deltaLat, deltaLong);
            MKCoordinateRegion region = {coord, span};
            [resultMapView setRegion:region];


        }
        @catch (NSException * e) {
            NSLog(@"Error calculating new map region: %@", e);
        }
        @finally {
            //show member pins
            for (id member in members) {
                [resultMapView addAnnotation:(Member *) member];
            }
        }



    }

}

答案 1 :(得分:5)

要在NSLog()中显示double值,您应该使用%f,而不是%d 像这样改变NSLog()部分:

NSLog(@"center long: %f, center lat: %f", center_long, center_lat);
NSLog(@"max_long: %f, min_long: %f, max_lat: %f, min_lat: %f", max_long, min_long, max_lat, min_lat);

此外,使用MKMapView中的区域比制作自己的区域要简单得多。一旦设置了缩放比例,您只需要使用不同的坐标动态地在地图上移动。

MKCoordinateRegion region = self.mapView.region;
region.center = centerCoordinate;
region.span.longitudeDelta /= ratioZoomMax; // Bigger the value, closer the map view
region.span.latitudeDelta /= ratioZoomMax;
[self.mapView setRegion:region animated:YES]; // Choose if you want animate or not

答案 2 :(得分:2)

//show member pins
        for (id member in members) {
            [resultMapView addAnnotation:(Member *) member];
        }

可以用

代替
[resultMapView addAnnotations:members];

答案 3 :(得分:0)

你可以简单地使用这种代码的和平:

-(void)updateMemberPins
{
    if([members count] == 0)
        return;

    double minLat = 90;
    double minLon = 180;
    double maxLat = -90;
    double maxLon = -180;

    for(Member *member in members)
    {
        minLat = fmin(minLat, [member.locLat doubleValue]);
        minLon = fmin(minLon, [member.locLong doubleValue]);
        maxLat = fmax(maxLat, [member.locLat doubleValue]);
        maxLon = fmax(maxLon, [member.locLong doubleValue]);
    }

    NSLog(@"MAX LAT: %f, MIN LAT: %f, MAX LONG: %f, MIN LONG: %f,", maxLat, minLat, maxLon, minLon);

    double midLat =  (minLat + maxLat)/2;
    double midLong = (minLon + maxLon)/2;

    double deltaLat = abs(maxLat - minLat);
    double deltaLong = abs(maxLon - minLon);

    if (deltaLat < 5) {deltaLat = 5;}
    if (deltaLong < 5) {deltaLong = 5;}

    //...
}
相关问题