지도가 모든 핀 주석을 동시에 표시하도록지도 영역 (중심 및 경간)을 설정하려고합니다.iPhone MKMapView :지도의 모든 핀을 표시하도록지도 영역 설정
long/lat 좌표를 NSString에서 double로 변환하는 데 문제가 있습니다. 그들과 계산해라.
- (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
I thin k 문제는 (잘못) NSString에서 값을 double로 변환하지만, 작동시킬 수있는 방법을 찾을 수 없습니다. 위치 문자열의 형식은 '43 .5686473 '과 같습니다.
힌트가 있습니까? Cheerz에
최소 최대 lat lon의 시작 값이 잘못되었습니다. 다음을 시도하십시오 : double max_long = -360.0f; double min_long = 360.0f; double max_lat = -360.0f; double min_lat = 360.0f; – igrek
및 범위 계산? 왜 그들을 추가할까요? 또한 abs는 int가 double이 아니므로 팹이되어야합니다. MKCoordinateSpan span = MKCoordinateSpanMake (fabs (max_lat - min_lat), fabs (max_long - min_long)); – igrek
어쨌든 고마워, +1 내게 시간을 절약했다 – igrek