Iphone 为地图中的某些位置设置缩放和居中
我尝试在地图中的几个位置调整缩放和居中。这是我使用但不起作用的代码:Iphone 为地图中的某些位置设置缩放和居中,iphone,objective-c,ios,Iphone,Objective C,Ios,我尝试在地图中的几个位置调整缩放和居中。这是我使用但不起作用的代码: MKCoordinateRegion region; CLLocationDegrees maxLat = -90; CLLocationDegrees maxLon = -180; CLLocationDegrees minLat = 90; CLLocationDegrees minLon = 180; for(int idx
MKCoordinateRegion region;
CLLocationDegrees maxLat = -90;
CLLocationDegrees maxLon = -180;
CLLocationDegrees minLat = 90;
CLLocationDegrees minLon = 180;
for(int idx = 0; idx < arrLocation.count; idx++)// here use your array or points
{
CLLocation* currentLocation = [arrLocation objectAtIndex:idx];
if(currentLocation.coordinate.latitude > maxLat)
maxLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.latitude < minLat)
minLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.longitude > maxLon)
maxLon = currentLocation.coordinate.longitude;
if(currentLocation.coordinate.longitude < minLon)
minLon = currentLocation.coordinate.longitude;
}
region.center.latitude = (maxLat + minLat) / 2;
region.center.longitude = (maxLon + minLon) / 2;
region.span.latitudeDelta = (maxLat - minLat) * 2;
region.span.longitudeDelta = (maxLon - minLon) * 2;
[mapView setRegion:region animated:YES];
mk协调区域;
CLLocationDegrees maxLat=-90;
CLLocationDegrees maxLon=-180;
CLLocationDegrees minLat=90;
CLLocationDegrees minLon=180;
for(int idx=0;idxmaxLat)
maxLat=currentLocation.coordinate.latitude;
if(currentLocation.coordinate.latitudemaxLon)
maxLon=当前位置.坐标.经度;
if(currentLocation.coordinate.longitude
我在didAddAnnotationViews
和didUpdateUserLocation
中尝试了它,但没有成功。你能帮我吗?使用这个代码
MKMapRect flyTo = MKMapRectNull;
for (id <MKAnnotation> annotation in self.arrayAnnotation)
{
MyLog(@"fly to on");
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
if (MKMapRectIsNull(flyTo))
{
flyTo = pointRect;
}
else
{
flyTo = MKMapRectUnion(flyTo, pointRect);
//MyLog(@"else-%@",annotationPoint.x);
}
}
// Position the map so that all overlays and annotations are visible on screen.
mapView.visibleMapRect = flyTo;
MKMapRect flyTo=MKMapRectNull;
for(self.arrayAnnotation中的id注释)
{
MyLog(“飞到on”);
MKMapPoint annotationPoint=MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect=MKMapRectMake(annotationPoint.x,annotationPoint.y,0,0);
if(MKMapRectIsNull(flyTo))
{
flyTo=pointRect;
}
其他的
{
flyTo=MKMapRectUnion(flyTo,pointRect);
//MyLog(@“else-%@”,annotationPoint.x);
}
}
//定位地图,使所有覆盖和注释在屏幕上可见。
mapView.visibleMapRect=flyTo;
希望它能帮助你
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(theCoordinate, 1000, 1000);
MKCoordinateRegion adjustedRegion = [pointOnMapView regionThatFits:viewRegion];
[pointOnMapView setRegion:adjustedRegion animated:NO];
pointOnMapView
是我的MKMapView
coordinate
类型是CLLocationCoordinate2D
最后我通过以下方式找到了解决方案:
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(id<MKAnnotation> annotation in mapView.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;
// Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
CLLocationCoordinate2D TopLeftCoordinate;
topLeftCoord.纬度=-90;
topLeftCoord.longitude=180;
CLLocationCoordinate2D底部右坐标;
bottomRightCoord.纬度=90;
bottomRightCoord.经度=-180;
用于(mapView.annotations中的id注释){
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);
}
协调区域;
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(底部右坐标经度-顶部左坐标经度)*1.1;
region=[mapView RegionAtfits:region];
[地图视图设置区域:区域动画:是];
我应该在哪里使用它?什么是arrayAnnotation?你能提供更多关于这种方法的信息吗?arrayAnnotation是存储所有注释的地方,在你的情况下,它可能有不同的名称