iOS,如何使用 GMSCoordinateBounds 显示地图的所有标记?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21615811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS, How to use GMSCoordinateBounds to show all the markers of the map?
提问by Al_fareS
I want to show all the markers that are on my map, after doing some searches I found that it should be done with GMSCoordinateBounds(Google Maps SDK) I've read the official documentation about it, but I have not understand how to use it and implement it in my code.
我想显示我地图上的所有标记,在进行了一些搜索后,我发现它应该使用GMSCoordinateBounds(Google Maps SDK) 完成我已经阅读了有关它的官方文档,但我不明白如何使用它并在我的代码中实现它。
Here is my code
这是我的代码
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]];
Any help ?
有什么帮助吗?
回答by allemattio
- (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]];
}
UPDATE:
更新:
are you sure there is nothing wrong in you array of markers and the coordinates? I've tried this code and is working perfectly. I've put this on the viewDidAppear
你确定你的标记和坐标数组没有问题吗?我已经尝试过这段代码并且运行良好。我把它放在 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]];
This is my result:
这是我的结果:
回答by Channel
Swift 3 - Xcode 8
斯威夫特 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)
回答by average Joe
Clean swift 3version:
Clean swift 3版本:
let bounds = markers.reduce(GMSCoordinateBounds()) {
var bounds = GMSCoordinateBounds()
for location in locationArray
{
let latitude = location.valueForKey("latitude")
let longitude = location.valueForKey("longitude")
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude:latitude, longitude:longitude)
marker.map = self.mapView
bounds = bounds.includingCoordinate(marker.position)
}
let update = GMSCameraUpdate.fitBounds(bounds, withPadding: 100)
mapView.animateWithCameraUpdate(update)
.includingCoordinate(.position)
}
mapView.animate(with: .fit(bounds, withPadding: 30.0))
回答by Arpit Dongre
Swift solution using GMSCoordinateBounds()
without path,
使用GMSCoordinateBounds()
无路径的Swift 解决方案,
NSArray *myMarkers; // array of marker which sets in Mapview
GMSMutablePath *path = [GMSMutablePath path];
for (GMSMarker *marker in myMarkers) {
[path addCoordinate: marker.position];
}
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithPath:path];
[_mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds]];
回答by mars
The easiest way I have found is to create a GMSMutablePath and then add all the coordinates of your markers to it. Then you can use the GMSCoordinateBounds initializer initWithPath: to create the bounds.
我发现的最简单的方法是创建一个 GMSMutablePath,然后将标记的所有坐标添加到其中。然后您可以使用 GMSCoordinateBounds 初始值设定项 initWithPath: 创建边界。
Once you have the bounds, you can create the GMSCameraUpdate and use it to animate the map to the visible bounds containing all of your markers.
获得边界后,您可以创建 GMSCameraUpdate 并使用它来将地图动画化到包含所有标记的可见边界。
For example, if you have an array of GMSMarker's:
例如,如果您有一组 GMSMarker:
if let myLocation = mapView.myLocation {
let path = GMSMutablePath()
path.addCoordinate(myLocation.coordinate)
//add other coordinates
//path.addCoordinate(model.coordinate)
let bounds = GMSCoordinateBounds(path: path)
mapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 40))
}
回答by apinho
As for Google Maps version 2.0.0 if you try to create a GMSCoordinateBounds using the default constructor GMSCoordinateBounds() and you check the "valid" flag it will be returning false and it won't make the animateWithCameraUpdate: move.
对于 Google Maps 2.0.0 版,如果您尝试使用默认构造函数 GMSCoordinateBounds() 创建 GMSCoordinateBounds 并且您检查“有效”标志,它将返回 false 并且不会使 animateWithCameraUpdate: 移动。
Swift 2.3 Solution
Swift 2.3 解决方案
@IBOutlet weak var mapView: GMSMapView!
let camera = GMSCameraPosition.cameraWithLatitude(23.0793, longitude:
72.4957, zoom: 5)
mapView.camera = camera
mapView.delegate = self
mapView.myLocationEnabled = true
*** arry has dictionary object which has value of Latitude and Longitude. ***
let path = GMSMutablePath()
for i in 0..<arry.count {
let dict = arry[i] as! [String:AnyObject]
let latTemp = dict["latitude"] as! Double
let longTemp = dict["longitude"] as! Double
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: latTemp, longitude: longTemp)
marker.title = "Austrilia"
marker.appearAnimation = kGMSMarkerAnimationNone
marker.map = self.mapView
path.addCoordinate(CLLocationCoordinate2DMake(latTemp, longTemp))
}
let bounds = GMSCoordinateBounds(path: path)
self.mapView!.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 50.0))
回答by Dipang
//bounding to a region of markers
GMSCoordinateBounds *bounds =
[[GMSCoordinateBounds alloc] initWithCoordinate:sourceMarker.position coordinate:destMarker.position];
[_mapView moveCamera:[GMSCameraUpdate fitBounds:bounds withPadding:50.0]];
回答by Zeesha
We can find the same code every where , but make sure that it is inviewDidAppear() method
我们可以在任何地方找到相同的代码,但要确保它在viewDidAppear() 方法中
- (GMSCoordinateBounds *)includingCoordinate:(CLLocationCoordinate2D)coordinate;
- (GMSCoordinateBounds *)includingBounds:(GMSCoordinateBounds *)other;
回答by Srikanth Thangavel
As per documentation, GMSCoordinateBounds can't be mutated.
根据文档,不能改变 GMSCoordinateBounds。
GMSCoordinateBounds is immutable and can't be modified after construction.
GMSCoordinateBounds 是不可变的,并且在构建后不能修改。
var gmsBounds = GMSCoordinateBounds()
for coordinate in coordinates {
let marker = GMSMarker(position: coordinate)
gmsBounds = gmsBounds.includingCoordinate(position)
marker.map = mapView
}
mapView.animate(with: GMSCameraUpdate.fit(gmsBounds, withPadding: 30.0))
So we can mutate GMSCoordinateBounds by using above methods includingCoordinate: (for extending coordinates) and includingBounds: (for extending bounds).
所以我们可以通过使用上述方法包括坐标:(用于扩展坐标)和包括边界:(用于扩展边界)来改变 GMSCoordinateBounds。
So Swift code will looks like below:
因此 Swift 代码将如下所示:
##代码##Additional Notes:
补充说明:
If you add custom marker view, add padding as width of marker view in addition to required padding.
如果您添加自定义标记视图,除了所需的填充之外,还添加填充作为标记视图的宽度。
回答by Davi Stuart
You could iterate your map markers and get the further point to the east, west, north and south and make [[GMSCoordinateBounds alloc] initWithRegion:]
您可以迭代您的地图标记,并获得更远的东、西、北和南点,并制作 [[GMSCoordinateBounds alloc] initWithRegion:]