单击我的 swift 应用程序以跟踪驱动程序位置时,我收到上述错误。本质上,它显示了司机的位置,但未能绘制司机和酒店/客户之间的路径。该应用程序冻结并引发上述错误。希望有人可以帮助我解决这个问题。
func makePath(){
self.mapView.clear()
var bounds = GMSCoordinateBounds()
self.mapView.camera = GMSCameraPosition.camera(withLatitude: (self.riderlat as NSString).doubleValue, longitude: (self.riderlong as! NSString).doubleValue, zoom: 1.0)
self.riderMarker.title = StaticData.singleton.map_name
self.riderMarker.snippet = StaticData.singleton.map_phone
let str :String?
if (self.riderlat == "0.0" || self.riderlong == "0.0" || self.hotellat == "0.0" || self.hotellong == "0.0" ) {
str = ""
} else {
str = String(format:"https://maps.googleapis.com/maps/api/directions/json?origin=\(self.riderlat!),\(self.riderlong!)&destination=\(self.hotellat!),\(self.hotellong!)&key=AIzaSyBEDFKSDFKDSFNSDKF9434-0")
//print(str!)
}
Alamofire.request(str!).responseJSON { (responseObject) -> Void in
let resJson = JSON(responseObject.result.value)
//print(resJson)
if(resJson["status"].rawString()! == "ZERO_RESULTS") {
} else if(resJson["status"].rawString()! == "NOT_FOUND") {
} else {
if let routes : NSArray = resJson["routes"].rawValue as? NSArray {
//print(routes)
self.mapView.isMyLocationEnabled = true
let position = CLLocationCoordinate2D(latitude:(self.riderlat as NSString).doubleValue, longitude: (self.riderlong as NSString).doubleValue)
let marker = GMSMarker(position: position)
marker.icon = UIImage(named: "Car.png")
marker.title = "Customer have selected same location as yours"
marker.map = self.mapView
bounds = bounds.includingCoordinate(marker.position)
let position2 = CLLocationCoordinate2D(latitude:(self.hotellat as NSString).doubleValue, longitude: (self.hotellong as NSString).doubleValue )
let marker1 = GMSMarker(position: position2)
marker1.icon = UIImage(named: "HotelPin")
marker1.appearAnimation = GMSMarkerAnimation.pop
marker1.map = self.mapView
self.riderMarker.icon = UIImage(named:"Car.png")
self.riderMarker.map = self.mapView
self.getAddressFromLatLon(pdblLatitude:StaticData.singleton.rider_lat!, withLongitude:StaticData.singleton.rider_lon!)
for p in (0 ..< self.polyArray.count) {
(self.polyArray[p]).map = nil
}
let pathv : NSArray = routes.value(forKey: "overview_polyline") as! NSArray
//print(pathv)
let paths : NSArray = pathv.value(forKey: "points") as! NSArray
//print(paths)
let newPath = GMSPath.init(fromEncodedPath: paths[0] as! String)
let polyLine = GMSPolyline(path: newPath)
polyLine.strokeWidth = 5
polyLine.strokeColor = UIColor(red:190/255.0, green:44/255.0, blue:44/255.0, alpha:1.0)
self.polyArray.append(polyLine)
polyLine.map = self.mapView
self.polyArray.append(polyLine)
let bounds = GMSCoordinateBounds(path: newPath!)
self.mapView!.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 50.0))
}
}
}
}
特别是错误指向的代码的这一部分:
let newPath = GMSPath.init(fromEncodedPath: paths[0] as! String)