在Swift项目中以缩放级别适合整个Google Map [英] Fit entire Google Map in zoom level in Swift project
问题描述
我有一个带有一堆坐标的谷歌地图
path.addCoordinate(CLLocationCoordinate2DMake(-37.813047,144.959911))
path.addCoordinate(CLLocationCoordinate2DMake(-37.814895,144.960759))
path.addCoordinate(CLLocationCoordinate2DMake(-37.814361,144.963140))
path.addCoordinate(CLLocationCoordinate2DMake(-37.812386,144.962239))
我希望地图可以根据点自动放大到最佳级别,但是我找不到任何东西
我有这个工作: = CLLocationCoordinate2DMake(-37.813047,144.959911)
var calgary = CLLocationCoordinate2DMake(-37.814361,144.963140)
var bounds = GMSCoordinateBounds(坐标:温哥华,坐标:卡尔加里)
var camera = viewMap.cameraForBounds(但是它只接受2个坐标,而不是一个坐标我可能有多达100位的饮食,可能有100多美元。b / b
谢谢 可以使用 GMSCoordinateBounds(path:)
来适应所有坐标。但如果您在另一次更新后立即更新相机,它将显示世界尺寸比例
。因此,您可以使用 dispatch_after
来解决问题。
覆盖func viewDidLoad(){
super.viewDidLoad()
self.view .backgroundColor = UIColor.whiteColor();
let camera = GMSCameraPosition.cameraWithLatitude(-37.813047,经度:-72.8561644,zoom:5)
mapView = GMSMapView.mapWithFrame(CGRectZero,camera:camera)
let marker = GMSMarker()
marker.position = camera.target
marker.snippet =Hello World
marker.appearAnimation = kGMSMarkerAnimationPop
marker.map = mapView
self.view = mapView
delay(seconds:2){() - > ()in
let path = GMSMutablePath()
path.addCoordinate(CLLocationCoordinate2DMake(37.36,-122.0))
path.addCoordinate(CLLocationCoordinate2DMake(37.45,-122.0))
path .addCoordinate(CLLocationCoordinate2DMake(37.45,-122.2))
path.addCoordinate(CLLocationCoordinate2DMake(37.36,-122.2))
path.addCoordinate(CLLocationCoordinate2DMake(37.36,-122.0))
让矩形= GMSPolyline(路径:路径)
rectangle.map = self.mapView
让边界= GMSCoordinateBounds(路径:路径)
self.mapView! .animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds,withPadding:15.0))
}
}
延迟
方法使用 dispatch_after
:
func delay(#seconds:Double,completion:() - >()){
let popTime = dispatch_time(DISPATCH_TIME_NOW ,Int64(Double(NSEC_PER_SEC)* seconds))
dispatch_after(popTime,dispatch_get_main_queue()){
completion()
}
}
I have a google map with a bunch of coordinates
path.addCoordinate(CLLocationCoordinate2DMake(-37.813047, 144.959911))
path.addCoordinate(CLLocationCoordinate2DMake(-37.814895, 144.960759))
path.addCoordinate(CLLocationCoordinate2DMake(-37.814361, 144.963140))
path.addCoordinate(CLLocationCoordinate2DMake(-37.812386, 144.962239))
I would like the map to be automatically zoomed to the best level based on the points however I can't find anything relating to this.
I have this working:
var vancouver = CLLocationCoordinate2DMake(-37.813047, 144.959911)
var calgary = CLLocationCoordinate2DMake(-37.814361, 144.963140)
var bounds = GMSCoordinateBounds(coordinate: vancouver, coordinate: calgary)
var camera = viewMap.cameraForBounds(bounds, insets:UIEdgeInsetsZero)
viewMap.camera = camera
however it only accepts 2 coordinates where I may have up to 100
Thanks
You can use GMSCoordinateBounds(path:)
to fit all coordinates. But it will display a world size scale
if you update the camera right after your another update. So you can use dispatch_after
to solve the problem.
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor();
let camera = GMSCameraPosition.cameraWithLatitude(-37.813047, longitude: -72.8561644, zoom:5)
mapView = GMSMapView.mapWithFrame(CGRectZero, camera:camera)
let marker = GMSMarker()
marker.position = camera.target
marker.snippet = "Hello World"
marker.appearAnimation = kGMSMarkerAnimationPop
marker.map = mapView
self.view = mapView
delay(seconds: 2) { () -> () in
let path = GMSMutablePath()
path.addCoordinate(CLLocationCoordinate2DMake(37.36, -122.0))
path.addCoordinate(CLLocationCoordinate2DMake(37.45, -122.0))
path.addCoordinate(CLLocationCoordinate2DMake(37.45, -122.2))
path.addCoordinate(CLLocationCoordinate2DMake(37.36, -122.2))
path.addCoordinate(CLLocationCoordinate2DMake(37.36, -122.0))
let rectangle = GMSPolyline(path: path)
rectangle.map = self.mapView
let bounds = GMSCoordinateBounds(path: path)
self.mapView!.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 15.0))
}
}
The delay
method uses the dispatch_after
:
func delay(#seconds: Double, completion:()->()) {
let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64( Double(NSEC_PER_SEC) * seconds ))
dispatch_after(popTime, dispatch_get_main_queue()) {
completion()
}
}
这篇关于在Swift项目中以缩放级别适合整个Google Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!