//根据一组数据坐标设置地图中心位置和缩放率
func zoomToMapPoints(_annotations:[CLLocationCoordinate2D]) {
varmaxLat:CLLocationDegrees=-90
varmaxLon:CLLocationDegrees=-180
varminLat:CLLocationDegrees=90
varminLon:CLLocationDegrees=180
foritem:CLLocationCoordinate2Dinannotations {
ifitem.latitude< minLat {
minLat = item.latitude
}
ifitem.latitude> maxLat {
maxLat = item.latitude
}
ifitem.longitude< minLon {
minLon = item.longitude
}
ifitem.latitude> maxLon {
maxLon = item.longitude
}
}
//设置中心的位置坐标
letcenLng:CLLocationDegrees= (maxLon+minLon)/2;
letcenLat:CLLocationDegrees= (maxLat+minLat)/2;
letzoom =getZoom(maxLng: maxLon, maxLat: maxLat, minLng: minLon, minLat: minLat)
letcamera =GMSCameraPosition.camera(withLatitude:cenLat,
longitude:cenLng, zoom:Float(zoom))
self.setCenter(camera:camera)
}
//根据经纬极值计算绽放级别
func getZoom (maxLng:CLLocationDegrees, maxLat:CLLocationDegrees,minLng:CLLocationDegrees, minLat:CLLocationDegrees) ->Int{
letzooms:[Double] = [50.0,100.0,200.0,500.0,1000.0,2000.0,5000.0,10000.0,20000.0,25000.0,50000.0,100000.0,200000.0,500000.0,1000000.0,2000000.0]//级别18到3。
letlocation1 =CLLocation(latitude:maxLat, longitude:maxLng)
letlocation2 =CLLocation(latitude:minLat, longitude:minLng)
letdistance:Double= location1.distance(from:location2)
vari:Int=0
forzoom:Doubleinzooms {
ifzoom - distance >0{
return18- i +3
}
i +=1
}
return18
}