我想显示地图上的所有标记,在进行了一些搜索之后,我发现应该用GMSCoordinateBounds (Google )来完成,我已经阅读了关于它的正式文档,但是我不知道如何使用它并在代码中实现它。
update#aa5b05606808272f9054c54af6830df3e
这是我的密码
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init];
CLLocationCoordinate2D location;
for (NSDictionary *dictionary in array) {
location.latitude = [dictionary[@"latitude"] floatValue];
location.longitude = [dictionary[@"longitude"] floatValue];
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.icon = [UIImage imageNamed:(@"marker.png")];
marker.position = CLLocationCoordinate2DMake(location.latitude, location.longitude);
bounds = [bounds includingCoordinate:marker.position];
marker.title = dictionary[@"type"];
marker.map = mapView_;
}
[mapView_ animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:30.0f]];
有什么帮助吗?
发布于 2014-02-07 09:12:02
- (void)focusMapToShowAllMarkers
{
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init];
for (GMSMarker *marker in <An array of your markers>)
bounds = [bounds includingCoordinate:marker.position];
[<yourMap> animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:30.0f]];
}
更新:
你确定你的标记和坐标数组没有什么问题吗?我已经尝试过这段代码,并且运行得很好。我已经把这个放到viewDidAppear上了
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:[[NSDictionary alloc]initWithObjectsAndKeys:@"44.66",@"latitude",@"21.33",@"longitude", nil],
[[NSDictionary alloc]initWithObjectsAndKeys:@"44.66",@"latitude",@"21.453",@"longitude", nil],
[[NSDictionary alloc]initWithObjectsAndKeys:@"44.44",@"latitude",@"21.993",@"longitude", nil],
[[NSDictionary alloc]initWithObjectsAndKeys:@"44.635",@"latitude",@"21.553",@"longitude", nil],
[[NSDictionary alloc]initWithObjectsAndKeys:@"44.3546",@"latitude",@"21.663",@"longitude", nil],
[[NSDictionary alloc]initWithObjectsAndKeys:@"44.6643",@"latitude",@"21.212",@"longitude", nil],
[[NSDictionary alloc]initWithObjectsAndKeys:@"44.63466",@"latitude",@"21.3523",@"longitude", nil],nil];
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init];
CLLocationCoordinate2D location;
for (NSDictionary *dictionary in array)
{
location.latitude = [dictionary[@"latitude"] floatValue];
location.longitude = [dictionary[@"longitude"] floatValue];
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.icon = [UIImage imageNamed:(@"marker.png")];
marker.position = CLLocationCoordinate2DMake(location.latitude, location.longitude);
bounds = [bounds includingCoordinate:marker.position];
marker.title = dictionary[@"type"];
marker.map = mapView_;
}
[mapView_ animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:30.0f]];
这是我的结果:
发布于 2017-02-21 11:49:27
Swift 3- Xcode 8
var bounds = GMSCoordinateBounds()
for marker in yourArrayOfMarkers
{
bounds = bounds.includingCoordinate(marker.position)
}
let update = GMSCameraUpdate.fit(bounds, withPadding: 60)
mapView.animate(with: update)
发布于 2017-05-27 23:58:26
清洁迅捷3版本:
let bounds = markers.reduce(GMSCoordinateBounds()) {
$0.includingCoordinate($1.position)
}
mapView.animate(with: .fit(bounds, withPadding: 30.0))
https://stackoverflow.com/questions/21615811
复制相似问题