我已創建地圖,如this link所示,並且其工作完美。iOS:在地圖上放大和縮小效果
但問題是,它只能放大一種方式(它只會變大)。我怎麼能使它在任何方面工作?
可能就像我們在google map(加號 - 減號棒在左側)。
我已創建地圖,如this link所示,並且其工作完美。iOS:在地圖上放大和縮小效果
但問題是,它只能放大一種方式(它只會變大)。我怎麼能使它在任何方面工作?
可能就像我們在google map(加號 - 減號棒在左側)。
對於ZoomIn
-(void)zoomIn
{
region.span.latitudeDelta = region.span.latitudeDelta/4 ;
region.span.longitudeDelta = region.span.longitudeDelta/4;
region.center.latitude = mapView.centerCoordinate.latitude ;
region.center.longitude = mapView.centerCoordinate.longitude ;
[mapView setRegion:region animated:YES];
}
對於縮小(ZoomOut)
-(void)zoomOut
{
region.span.latitudeDelta = region.span.latitudeDelta*4 ;
region.span.longitudeDelta = region.span.longitudeDelta*4;
region.center.latitude = mapView.centerCoordinate.latitude ;
region.center.longitude = mapView.centerCoordinate.longitude ;
[mapView setRegion:region animated:YES];
}
可以使用在該結構中的δ值,以指示地圖的期望 縮放水平,與對應於更小的增量值一個 更高的縮放級別。
欲瞭解更多信息,請參閱此link。
這是Midhun VP和Slavco Petkovski的回答修訂版本,它在設置latitudeDelta過大時避免了zoomOut中「無效區域」的NSInvalidArgumentException。放大似乎沒有任何問題 - 一旦達到極限,它不會再放大。
- (void)zoomIn {
MKCoordinateRegion region = [self.mapView region];
region.span.latitudeDelta = region.span.latitudeDelta/4;
region.span.longitudeDelta = region.span.longitudeDelta/4;
region.center.latitude = self.mapView.centerCoordinate.latitude;
region.center.longitude = self.mapView.centerCoordinate.longitude;
[self.mapView setRegion:region animated:YES];
//NSLog(@"zoomIn: center %lf, %lf; spanDelta %lf, %lf, upper left %lf, %lf; lower right %lf %lf", region.center.latitude, region.center.longitude, region.span.latitudeDelta, region.span.longitudeDelta, region.center.latitude + region.span.latitudeDelta/2, region.center.longitude - region.span.longitudeDelta/2, region.center.latitude - region.span.latitudeDelta/2, region.center.longitude + region.span.longitudeDelta/2);
}
- (void)zoomOut {
MKCoordinateRegion region = [self.mapView region];
region.span.latitudeDelta = region.span.latitudeDelta*4;
region.span.longitudeDelta = region.span.longitudeDelta*4;
region.center.latitude = self.mapView.centerCoordinate.latitude;
region.center.longitude = self.mapView.centerCoordinate.longitude;
// The region upper latitude must not exceed 90.0 degrees, and the region lower latitude must not fall below -90.0.
double upperLatitude = region.center.latitude + region.span.latitudeDelta/2.0;
double lowerLatitude = region.center.latitude - region.span.latitudeDelta/2.0;
if (upperLatitude > 90 || lowerLatitude < -90) {
region.center.latitude = 0.0;
double spanRatio = region.span.latitudeDelta/region.span.longitudeDelta;
region.span.latitudeDelta = 180;
region.span.longitudeDelta = 180/spanRatio;
}
[self.mapView setRegion:region animated:YES];
//NSLog(@"zoomOut: center %lf, %lf; spanDelta %lf, %lf, upper left %lf, %lf; lower right %lf %lf", region.center.latitude, region.center.longitude, region.span.latitudeDelta, region.span.longitudeDelta, region.center.latitude + region.span.latitudeDelta/2, region.center.longitude - region.span.longitudeDelta/2, region.center.latitude - region.span.latitudeDelta/2, region.center.longitude + region.span.longitudeDelta/2);
}
驗證是從我們的結尾,答案將不包括從A-Z的一切。 –
感謝您的回答。我會研究它,並回過頭來查詢任何疑問。 –
我添加了這段代碼,並將區域變量作爲全局變量MapViewController {MKCoordinateRegion region; }'並將MKCoordinateRegion region = {cords,span};'改爲'region = {cords,span};'。但是,我在'region = {cords,span}'的紅色警告;''爲'Expected expression'。我錯過了什麼? –
也會在'region.span.latitudeDelta'處得到錯誤,因爲'Member reference type'MKCoordinateRegion *'是一個指針;也許你打算使用' - >'?' –