在mkmapview上添加注释并将mapview设置为添加新注释的区域

时间:2013-02-21 06:31:02

标签: objective-c mkmapview mkannotation

我有一个MKMapView,上面有很多MKAnnotations。 单击按钮,将添加另一组注释。

现在点击按钮,我有一个注释数组。我将注释数组添加到Mkmapview

我希望mapview移动到添加新注释的区域,而不会缩小或放大。

请帮我解决一下。

2 个答案:

答案 0 :(得分:0)

我认为将中心设置为注释数组中的一个坐标将符合您的目的

[_mapView setCenterCoordinate:annotationCoOrd zoomLevel:_zoomLvl animated:YES];

其中,annotationCoOrd是数组中的坐标之一 _zoomLvl是您当前的缩放级别。

如果您在此处发布代码会更有帮助。

用于缩放级别

- (double)getZoomLevel{
MKCoordinateRegion reg=self.region; // the current visible region
MKCoordinateSpan span=reg.span; // the deltas
CLLocationCoordinate2D centerCoordinate=reg.center; // the center in degrees
// Get the left and right most lonitudes
CLLocationDegrees leftLongitude=(centerCoordinate.longitude-(span.longitudeDelta/2));
CLLocationDegrees rightLongitude=(centerCoordinate.longitude+(span.longitudeDelta/2));
CGSize mapSizeInPixels = self.bounds.size; // the size of the display window

// Get the left and right side of the screen in fully zoomed-in pixels
double leftPixel=[self longitudeToPixelSpaceX:leftLongitude]; 
double rightPixel=[self longitudeToPixelSpaceX:rightLongitude];
// The span of the screen width in fully zoomed-in pixels
double pixelDelta=abs(rightPixel-leftPixel);

// The ratio of the pixels to what we're actually showing
double zoomScale= mapSizeInPixels.width /pixelDelta;
// Inverse exponent
double zoomExponent=log2(zoomScale);
// Adjust our scale
double zoomLevel=zoomExponent+20; 
return zoomLevel;

}

- (double)longitudeToPixelSpaceX:(double)longitude
{
return round(MERCATOR_OFFSET + MERCATOR_RADIUS * longitude * M_PI / 180.0);
}

- (double)latitudeToPixelSpaceY:(double)latitude
{
return round(MERCATOR_OFFSET - MERCATOR_RADIUS * logf((1 + sinf(latitude * M_PI / 180.0)) / (1 - sinf(latitude * M_PI / 180.0))) / 2.0);
}

获取地图的缩放级别,在MKMapView实现中添加上述代码(覆盖MKMapview实现)。

答案 1 :(得分:0)

使用belo方法移动缩小

  [mapview setCenterCoordinate:cordinate animated:YES];