如何根据路径和标记快速设置谷歌地图缩放级别

Bha*_*eja 2 google-maps google-maps-api-3 google-maps-markers google-maps-sdk-ios swift

我正在尝试在Google地图中显示从一个地方到另一个地方的路径,而我得到的是这样的东西。但我需要表现出像这样。这意味着整个路径需要根据地图上的路径显示和显示,缩放级别应调整

这是我尝试从API绘制路径的代码。在这里let settingCam设置摄像机以调整到其中一个位置

func showingPathFromPickupLocToDropLoc(dropLat: Double, dropLong: Double){

    let origin = "\(dropLat),\(dropLong)"
    let destination = "\(dropLatitude),\(dropLongitude)"

    let settingCam: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: CLLocationDegrees(dropLat), longitude: CLLocationDegrees(dropLong))
    let url = URL(string: "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&key=\(NEWAPI.GOOGLE_APIKEY)")
    URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in
        if(error != nil){
            print("error")
        }else{
            do{
                let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String : AnyObject]
                if json["status"] as! String == "OK"{
                    let routes = json["routes"] as! [[String:AnyObject]]
                    OperationQueue.main.addOperation({
                        for route in routes{
                            let routeOverviewPolyline = route["overview_polyline"] as! [String:String]
                            let points = routeOverviewPolyline["points"]
                            let path = GMSPath.init(fromEncodedPath: points!)
                            self.PathFromPickupLocToDropLoc = GMSPolyline(path: path)
                            self.PathFromPickupLocToDropLoc.strokeColor = .gray
                            self.PathFromPickupLocToDropLoc.strokeWidth = 3.0
                            self.PathFromPickupLocToDropLoc.map = self.mapView

                            let camera = GMSCameraPosition.camera(withTarget: settingCam, zoom: 16.0)
                            self.mapView.animate(toLocation: settingCam)
                            self.mapView.animate(to: camera)
                            self.insertingMarkersFromPickupLocToDropLoc(dropLat: dropLat, dropLong: dropLong)
                        }
                    })
                }
            }catch let error as NSError{
                print(error)
            }
        }
    }).resume()
}
Run Code Online (Sandbox Code Playgroud)

Mit*_*dav 11

你需要这样

DispatchQueue.main.async
{
 if self.googleMap != nil
 {
  let bounds = GMSCoordinateBounds(path: path!)
  self.googleMap!.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 50.0))   
 }
}
Run Code Online (Sandbox Code Playgroud)