gpt4 book ai didi

ios - 从 GMSMapView Swift iOS 上的 GMSPolyline 移除行进路径

转载 作者:行者123 更新时间:2023-11-28 06:03:31 25 4
gpt4 key购买 nike

我正在使用谷歌距离 API ["https://maps.googleapis.com/maps/api/directions/json?origin= "+start.latitude + ","+ start.longitude +"&destination="+ end.latitude +","+ end.longitude + "&alternatives=false"+"&mode=driving&key="+ key;] 获取从起始位置到结束位置的路线。

我正在使用以下代码在起点和终点之间绘制路线

func drawPath()
{
if polylines != nil {
polylines?.map = nil
polylines = nil
}

if animationPolyline != nil {
self.animationIndex = 0
self.animationPath = GMSMutablePath()
self.animationPolyline.map = nil
if self.timer != nil {
self.timer.invalidate()
}
}


setupStartRideLocationMarkup(CLLocationCoordinate2D(latitude: (currentLocation?.coordinate.latitude)!, longitude: (currentLocation?.coordinate.longitude)!))

if currentLocation != nil && destinationLocation != nil {
let origin = "\((currentLocation?.coordinate.latitude)!),\((currentLocation?.coordinate.longitude)!)"
let destination = "\((destinationLocation?.latitude)!),\((destinationLocation?.longitude)!)"


let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving&key=MY_API_KEY"

Alamofire.request(url).responseJSON { response in


let json = JSON(data: response.data!)
self.jsonRoute = json
let routes = json["routes"].arrayValue

for route in routes
{
let routeOverviewPolyline = route["overview_polyline"].dictionary
let points = routeOverviewPolyline?["points"]?.stringValue
self.path = GMSPath.init(fromEncodedPath: points!)!
self.polylines = GMSPolyline.init(path: self.path)
self.polylines?.geodesic = true
self.polylines?.strokeWidth = 5
self.polylines?.strokeColor = UIColor.black
self.polylines?.map = self.mapView
}

self.shouldDrawPathToStartLocation()
self.shouldDrawPathToEndLocation()

if routes.count > 0 {
self.startAnimatingMap()
}
}
}
}

如您所见,我正在使用来自 api 的编码路径初始化路径。现在我想从整个路径中删除经过的 GMSPolyline 我该怎么做?我目前的想法是它将来自 didUpdateLocations 这是我的 didUpdateLocations 方法代码

 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
currentLocation = locations.last!

let camera = GMSCameraPosition.camera(withLatitude: (currentLocation?.coordinate.latitude)!,
longitude: (currentLocation?.coordinate.longitude)!,
zoom: zoomLevel)

if (mapView?.isHidden)! {
mapView?.isHidden = false
mapView?.camera = camera
} else {
mapView?.animate(to: camera)
}

updatePolyLineIfRequired()

}

updatePolyLineIfRequired 中,我想删除移动的多边形线

func updatePolyLineIfRequired(){
if GMSGeometryIsLocationOnPath((currentLocation?.coordinate)!, path, true) {
if startPolyline != nil {
startPolyline?.map = nil
startPolyline = nil
}

}
}

我想实现像 Uber 或 Careem 这样的解决方案,其中移动绘制的 GMSPolyline 被删除,直到用户当前位置。

提前致谢P.S 我正在使用 Alamofire SwiftyJSON

最佳答案

有两种解决方案:-

  1. 每次调用 didUpdateLocations 函数时调用 Directions Api。(效率不高)
  2. 从 GMSPath 中删除行进坐标。

调用 Directions api 将无用,除非您对 Direction api 的请求限制较少。

用于从路径中删除行进坐标:-

    //Call this function in didUpdateLocations
func updateTravelledPath(currentLoc: CLLocationCoordinate2D){
var index = 0
for i in 0..<self.path.count(){
let pathLat = Double(self.path.coordinate(at: i).latitude).rounded(toPlaces: 3)
let pathLong = Double(self.path.coordinate(at: i).longitude).rounded(toPlaces: 3)

let currentLaenter code heret = Double(currentLoc.latitude).rounded(toPlaces: 3)
let currentLong = Double(currentLoc.longitude).rounded(toPlaces: 3)

if currentLat == pathLat && currentLong == pathLong{
index = Int(i)
break //Breaking the loop when the index found
}
}

//Creating new path from the current location to the destination
let newPath = GMSMutablePath()
for i in index..<Int(self.path.count()){
newPath.add(self.path.coordinate(at: UInt(i)))
}
self.path = newPath
self.polyline.map = nil
self.polyline = GMSPolyline(path: self.path)
self.polyline.strokeColor = UIColor.darkGray
self.polyline.strokeWidth = 2.0
self.polyline.map = self.mapView
}

纬度和经度是四舍五入的,这样如果用户在旅行地点附近。根据需要使用以下扩展来舍入最多 3 位小数或更多位。

extension Double {
// Rounds the double to decimal places value
func rounded(toPlaces places:Int) -> Double {
let divisor = pow(10.0, Double(places))
return (self * divisor).rounded() / divisor
}
}

关于ios - 从 GMSMapView Swift iOS 上的 GMSPolyline 移除行进路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48865621/

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