将地图中心和缩放设置为适合所有坐标的位置

3
我正在使用iOS的Google地图。我有一组坐标,其中我想将其中一个坐标设置为地图的中心,而不考虑缩放级别。我能够使用GMSCameraUpdate fitBounds将所有坐标同时适合框架,但如何将特定坐标始终设置为中心,并将其他坐标适合框架。
我尝试创建GMSCoordinateBounds,包括所有坐标并设置fit bounds。
[myMapview animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:20.0f]];

但是我想要的是地图视图应该以这种方式缩放,使得这个特定的坐标成为精确的中心,同时所有其他坐标也包含在框架内。

3个回答

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.

这就是我所做的。但是我希望其中一个特定的坐标始终是中心,同时将其余坐标适配到框架中。 - Xavi Valero
你需要存储地图上的所有坐标以便在此处显示。 - Jogendra.Com

0

你需要做的就是创建正确的GMSCoordinatedBounds对象。 假设你有一些需要地图相机适配并且需要使用center点的点。

首先,你需要找到距离center最远的点。然后计算一个与你找到的点相反的额外点far'。它看起来像这样

(far)---------(center)---------(far')

为此,只需使用farcenter坐标的纬度和经度进行简单的+/-运算。

Farfar'将是bounds所需的确切两个点。


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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接