google ios SDK swift 适合范围
google ios SDK swift fit bounds
我的 google 地图上有几个标记,如何将相机安装到它们上?
这是我的代码:
NSURLSession.sharedSession().dataTaskWithURL(
NSURL(string: url)!)
{ data, response, error in
do {
let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers ) as! NSDictionary
for (var i=0; i < jsonData.allKeys.count; i++){
var key = jsonData.allKeys[i] as! String
var id = jsonData[key] as! NSDictionary
var club = id["club"] as! NSDictionary
var location = club["location"] as! NSDictionary
var latitude = location["latitude"] as! Double
var longitude = location["longitude"] as! Double
dispatch_async(dispatch_get_main_queue()) {
let position: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let marker = GMSMarker(position: position)
marker.title = club["name"] as! String
marker.map = self.map
}
}
} catch {
// report error
}
}.resume()
首先你需要创建一个数组来保存你的标记列表
var markerList = [GMSMarker]()
然后你可以调用animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds))
调整你的相机:
func fitAllMarkers() {
var bounds = GMSCoordinateBounds()
for marker in markerList {
bounds = bounds.includingCoordinate(marker.position)
}
map.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds))
//For Swift 5 use the one below
//self.mapView.animate(with: GMSCameraUpdate.fit(bounds))
}
你能在你的 dispatch_async
闭包中调用它吗:
dispatch_async(dispatch_get_main_queue()) {
let position: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let marker = GMSMarker(position: position)
marker.title = club["name"] as! String
marker.map = self.map
//new code
self.markerList.append(marker)
self.fitAllMarkers()
}
在 SWIFT 3.1 集群点击中:
func clusterManager(_ clusterManager: GMUClusterManager, didTap cluster: GMUCluster) {
var bounds = GMSCoordinateBounds()
for marker in cluster.items {
bounds = bounds.includingCoordinate(marker.position)
}
mapView.animate(with: GMSCameraUpdate.fit(bounds))
}
我的 google 地图上有几个标记,如何将相机安装到它们上?
这是我的代码:
NSURLSession.sharedSession().dataTaskWithURL(
NSURL(string: url)!)
{ data, response, error in
do {
let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers ) as! NSDictionary
for (var i=0; i < jsonData.allKeys.count; i++){
var key = jsonData.allKeys[i] as! String
var id = jsonData[key] as! NSDictionary
var club = id["club"] as! NSDictionary
var location = club["location"] as! NSDictionary
var latitude = location["latitude"] as! Double
var longitude = location["longitude"] as! Double
dispatch_async(dispatch_get_main_queue()) {
let position: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let marker = GMSMarker(position: position)
marker.title = club["name"] as! String
marker.map = self.map
}
}
} catch {
// report error
}
}.resume()
首先你需要创建一个数组来保存你的标记列表
var markerList = [GMSMarker]()
然后你可以调用animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds))
调整你的相机:
func fitAllMarkers() {
var bounds = GMSCoordinateBounds()
for marker in markerList {
bounds = bounds.includingCoordinate(marker.position)
}
map.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds))
//For Swift 5 use the one below
//self.mapView.animate(with: GMSCameraUpdate.fit(bounds))
}
你能在你的 dispatch_async
闭包中调用它吗:
dispatch_async(dispatch_get_main_queue()) {
let position: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let marker = GMSMarker(position: position)
marker.title = club["name"] as! String
marker.map = self.map
//new code
self.markerList.append(marker)
self.fitAllMarkers()
}
在 SWIFT 3.1 集群点击中:
func clusterManager(_ clusterManager: GMUClusterManager, didTap cluster: GMUCluster) {
var bounds = GMSCoordinateBounds()
for marker in cluster.items {
bounds = bounds.includingCoordinate(marker.position)
}
mapView.animate(with: GMSCameraUpdate.fit(bounds))
}