设置地图中心并缩放以适合所有坐标

时间:2015-09-24 08:56:10

标签: ios google-maps

我正在使用谷歌地图的iOS。我有一套坐标。其中我想将其中一个坐标设置为地图的中心,而不管缩放级别如何。我可以使用GMSCameraUpdate fitBounds同时拟合框架中的所有坐标。但是如何将特定坐标始终设置为中心,并且还适合框架中的其余坐标。

我所尝试的是创建GMSCoordinateBounds,包括所有坐标和设置适合范围

[myMapview animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:20.0f]];

但我想要的是地图视图应该以这样的方式放大:这个特定坐标应该是精确的中心,而所有其他坐标也包含在框架中。

3 个答案:

答案 0 :(得分:1)

您需要使用此类

将所有地图点存储在 GMSCoordinateBounds
GMSCoordinateBounds* bounds =  [[GMSCoordinateBounds alloc]  init  ];
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(26.00, 75.00);
bounds = [bounds includingCoordinate:position];

使用相同的类型代码存储所有位置。添加所有GMSCoordinateBounds位置使用地图

[myMapview animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds]];
//myMapview = this is object of your google map.

答案 1 :(得分:0)

您只需创建正确的GMSCoordinatedBounds对象即可。 假设您需要通过地图相机和center点来安装许多点。

首先,您需要找到center中最远的点。然后计算与您找到的相对应的额外点far'。它看起来像这样

  

(远)---------(中心)---------(远&#39)

为此,只需使用+/-far坐标的纬度和经度的简单center操作。

Farfar'将是bounds所需的两分。

答案 2 :(得分:0)

您能否看到以下代码?我希望它会对你有所帮助

打算设置地图的边界以专注于所有标记。 enter image description here

'GMSMapView'类曾经有一个方法'markers',它返回了所有标记,但现在已弃用了。 因此,您必须跟踪添加到地图中的标记。

假设您有以下内容:

GMSMapView *mapView;
NSMutableArray *markers;

并且所有标记都添加到NSMutableArray“markers”。

- (void)ShowAllMarkers
{
CLLocationCoordinate2D firstLocation = ((GMSMarker *)markers.firstObject).position;
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:firstLocation coordinate:firstLocation];

for (GMSMarker *marker in markers) {
    bounds = [bounds includingCoordinate:marker.position];
}

[mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:50.0f]];
}

enter image description here

相关问题