如何根据 swift 中的路径和标记设置 google 地图缩放级别

How to set google map zoom level according to path & markers in swift

我试图在 google 地图中显示从一个地方到另一个地方的路径 am getting something like this. but I need to show like this。这意味着整个路径需要根据地图上的路径显示&,缩放级别应该调整

这是我试图从 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()
}

你需要这样做

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