iPhone MKMapView:设置地图区域以显示地图上的所有引脚 [英] iPhone MKMapView: set map region to show all pins on map

查看:118
本文介绍了iPhone MKMapView:设置地图区域以显示地图上的所有引脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我无法将NSString的长/纬度坐标转换为double,resp。与他们进行计算。这里是我使用的代码:

   - (void)updateMemberPins {

//计算新地区在地图上显示
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;

(会员*会员){

//找到最小值和最大值
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]>如果([member.locLong doubleValue]< min_long){min_long = [member.locLong doubleValue];}

// max_long = [member.locLong doubleValue];}
//总结起来long和lang以后得到平均值
center_lat = center_lat + [member.locLat doubleValue];
center_long = center_long + [member.locLong doubleValue];
}

//计算平均长/美元
center_lat = center_lat / [会员数];
center_long = center_long / [会员数];

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

//创建新区域并设置地图
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];

//从地图
中删除所有的引脚[resultMapView removeAnnotations:resultMapView.annotations];

//显示成员引脚
(成员中的id成员){
[resultMapView addAnnotation:(Member *)member];
}

}

日志输出的结果是:


center long:-1946827116,center lat:1075651472

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

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



任何提示? Cheerz

解决方案

为了在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; //更大的值,更接近地图视图
region.span.latitudeDelta / = ratioZoomMax;
[self.mapView setRegion:region animated:YES]; //选择是否要动画


I'm trying to set the map region (center and span) so that the map shows all pin-annotations at the same time.

I'm having trouble converting the long/lat coordinates from NSString to double, resp. make calculations with them. Here is the code I'm using:

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

}

The result of the log-output is:

center long: -1946827116, center lat: 1075651472

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

I think the problem comes from (wrongly) converting values from NSString to double, however I cannot find a way to make it work... The format of the location-strings is like '43.5686473'.

Any hints? Cheerz

解决方案

To show double value in NSLog(), you should use %f, instead of %d Change NSLog() part like this:

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

Also, using region from MKMapView is much simpler than making your own. Once it's set with zoom ratio, all you need is to dynamically move around the map with different coordinates.

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

这篇关于iPhone MKMapView:设置地图区域以显示地图上的所有引脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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