gpt4 book ai didi

ios - 在谷歌地图中显示方向并画线

转载 作者:行者123 更新时间:2023-11-28 16:00:36 27 4
gpt4 key购买 nike

我真的被困在这里了。我刚刚学习如何在我的 View 中显示谷歌地图。现在我想确定从一个地方到另一个地方的方向。

到目前为止,这是我的代码:

@IBAction func direction(_ sender: Any) {    
Alamofire.request("https://maps.googleapis.com/maps/api/directions/json?" +
"origin=Disneyland&destination=Universal+Studios+Hollywood4&").responseJSON
{response in
if(response.result.isSuccess){
print(response.result.value)

我使用的链接是来自网络的示例。现在我只想知道如何画一条线和一个方向..感谢您的帮助

最佳答案

我的答案是来 self 的代码片段的 Swift 2

    //MARK:- Draw Route Betweeen two points
/**
To draw a route between to cordinates.

- parameter origin: Source marker cordinate
- parameter destination: destination marker cordinate
- parameter zoomCamera: True if you want to zoom the map.
- parameter completionHandler: GMSPolyline - to draw the route.
*/

func drawRoute(origin: CLLocation, destination: CLLocation, zoomCamera : Bool!, completionHandler: (polyline : AnyObject) -> Void)
{
let key : String = Macros.apiKeys.key

let originString: String = "\(origin.coordinate.latitude),\(origin.coordinate.longitude)"

let destinationString: String = "\(destination.coordinate.latitude),\(destination.coordinate.longitude)"

let directionsAPI: String = "https://maps.googleapis.com/maps/api/directions/json?"

let directionsUrlString: String = "\(directionsAPI)&origin=\(originString)&destination=\(destinationString)&key=\(key)"

Alamofire.request(.GET, directionsUrlString, parameters: ["": ""])
.responseJSON { response in

if let JSON = response.result.value
{
self.getRoutesWayPointsBetweenCordinates(origin.coordinate, destination: destination.coordinate, completionHandler:
{ (routesArray) in

if routesArray.count > 0
{
let routesArray : NSArray = JSON.objectForKey("routes") as! NSArray

if routesArray.count > 0
{
let routeDic = routesArray[0]

let routeOverviewPolyline = routeDic .objectForKey("overview_polyline")

let points : String = routeOverviewPolyline! .objectForKey("points") as! String


// Creating Path between source and destination.
self.path = GMSPath(fromEncodedPath: points)

if self.polyline != nil
{
self.polyline.map = nil
}

self.polyline = GMSPolyline(path: self.path)

self.polyline.strokeWidth = 4.5

self.polyline.geodesic = true

self.animateRoute(self.polyline, origin: origin.coordinate, destination: destination.coordinate, pathColor:UIColor.blueColor(), zoomCamera: zoomCamera)

completionHandler(polyline: self.polyline)
}

}else
{
let poly : GMSPolyline = GMSPolyline()
poly.strokeWidth = 5.5
completionHandler(polyline: poly)
}
})
}
}
}


func animateRoute(polyline : GMSPolyline, origin : CLLocationCoordinate2D, destination : CLLocationCoordinate2D, pathColor : UIColor, zoomCamera : Bool!)
{

polyline.strokeColor = pathColor

polyline.map = self.customMapView // Drawing route

let bounds = GMSCoordinateBounds(path: path)

var pad : CGFloat = 40.0

if padding != nil
{
pad = padding
}
if zoomCamera == true
{
zoomCameraWithBounds(bounds, pad: pad)
}
}

/**
It will zoom the camera at specific bounds

- parameter bounds: Bounds around which the camera should zoom
- parameter pad: Padding value from the edges of the window.
*/
func zoomCameraWithBounds(bounds : GMSCoordinateBounds, pad : CGFloat)
{
let camera = self.customMapView.cameraForBounds(bounds, insets:UIEdgeInsetsZero)

self.customMapView.camera = camera!

let zoomCamera = GMSCameraUpdate.fitBounds(bounds, withPadding: pad)

self.customMapView.animateWithCameraUpdate(zoomCamera) // Above lines will update map camera to fit to bounds so that the complete route between source and destination is visible.
}

你所做的是调用 drawRoute 函数并将源和目标坐标传递给它,它还使用 Alamofire 进行 api 命中

重要更改此 Macros.apiKeys.key使用您的 google api key 并确保它启用了适当的功能

关于ios - 在谷歌地图中显示方向并画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41138588/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com