2017-05-04 111 views
0

我試圖在迅速3繪製兩個標記之間在谷歌地圖的路徑,我得到一個錯誤。誰能告訴我我在哪裏做錯了。請幫我從這個謝謝。對谷歌繪製兩點之間的路徑映射

let url = URL(string: "http://maps.googleapis.com/maps/api/directions/json?origin=\(olat),\(olng)&destination=\(dlat),\(dlng)&sensor=false&mode=driving")! 


    let task = session.dataTask(with: url, completionHandler: { 
     (data, response, error) in 
     if error != nil { 
      print(error!.localizedDescription) 
     }else{ 
      do { 
       if let json : [String:Any] = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]{ 

        let routes = json["routes"] as? [Any] 
        let overview_polyline = routes?[0] as?[String:Any] 
        let polyString = overview_polyline?["points"] as?String 

        //Call this method to draw path on map 

        DispatchQueue.main.async 
        { 
          self.showPath(polyStr: polyString!) 
        } 

       } 

      }catch{ 
       print("error in JSONSerialization") 
      } 
     } 
    }) 
    task.resume() 

有人請指導我在兩點之間的谷歌地圖上繪製路線。

+0

我會懷疑多串是空的。你的答案看起來如何? – kuzdu

+0

是多邊形是空的。但我不能找出它 –

+0

打印json,它看起來如何? – kuzdu

回答

1

你想從這裏https://maps.googleapis.com/maps/api/directions/json?origin=50,8&destination=51,8&sensor=false&mode=driving

你需要這個嵌套結構解析JSON。我的一些提示:閱讀一些教程json是什麼,它在Swift中是如何工作的。你在網上找到很多東西。 Alternativ採用像ObjectMapper或SwiftyJson的pod(庫)。

if let json : [String:Any] = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]{ 

        // print("json \(json)") 
        if let routes = json["routes"] as? [Any] { 

         if let route = routes[0] as? [String:Any] { 

          if let legs = route["legs"] as? [Any] { 

           if let leg = legs[0] as? [String:Any] { 

            if let steps = leg["steps"] as? [Any] { 

             if let step = steps[0] as? [String:Any] { 


              if let polyline = step["polyline"] as? [String:Any] { 

               if let points = polyline["points"] as? String { 
                print("points \(points)") 
               } 
              } 
             } 
            } 
           } 
          } 
         } 
        } 
       } 
+2

金字塔! –

+0

也許考慮'guard'。 –

+1

它同意,這不是最漂亮的代碼,但也許是更容易理解,什麼是happing那叫一個JSON解析器來完成。我更喜歡ObjectMapper – kuzdu

1

JUST IN兩步:

第1步:調用谷歌地圖路線解放組織API。

步驟2:句柄路徑,距離和持續時間的響應。

*****STEP 1:***** 
/* request google direction GET APIs */ 
func requestGoogleMapsDirectionApis(originLat: String,originLong:String, destinationLat:String, destinationLong:String, onSuccess:@escaping (_ response:AnyObject)->Void, onError:@escaping (_ errorMessage:String)->Void)->Void{ 


// GOOGLE DIRECTION APIS DEMO 
// https://maps.googleapis.com/maps/api/directions/json?origin=27.696981,85.2942719&destination=27.6792144,85.3632975&sensor=false&mode=driving&alternatives=falseGOOGLE_MAP_API_KEY 


let url:String = "https://maps.googleapis.com/maps/api/directions/json?origin=\(originLat),\(originLong)&destination=\(destinationLat),\(destinationLong)&sensor=false&mode=driving&alternatives=false\(MAP_API_KEY)" 
showLog(classname: classname, tagName: "**FULL URL \(url)") 


Alamofire.request(url, method: .get).responseJSON { response in 

    showLog(classname: classname , tagName: "\n*** RESPONSE SERIALIZATION:\n \(response.result)") // result of response serialization 

    switch response.result { 
    case .success: 

     if let jsonObject = response.result.value{ 
      showLog(classname: classname, tagName: "\n*** OnSuccessResponse: \(jsonObject as AnyObject)") 
      onSuccess(jsonObject as AnyObject) 
     } 

    case .failure: 

     /* Handle error just using simple message */ 
     showLog(classname: classname, tagName: "\n*** OnErrorResponse: \(response)") 
     onError("failure") 

    } 
} 


*****STEP 2:***** 
/* 
* PLOT ROUTE IN GOOGLE MAP 
* CALCULATE DISTANCE 
* CALCULATE TIME 
*/ 
func callPlotRouteCalcDistanceAndTimeApis(originLat:String, originLong:String, destinationLat:String, destinationLong:String){ 

    homeViewCOntroller.view.makeToastActivity(.center) 
    requestGoogleMapsDirectionApis(originLat: originLat, originLong: originLong, destinationLat: destinationLat, destinationLong: destinationLong, onSuccess: { (response) in 

     // filter data 
     // set data to model 
     // plot route in google map 

     showLog(classname: self.className, tagName: response) 
     self.responseJsonObjectFilter(jsonObject: response) 
     self.homeViewCOntroller.view.hideToastActivity() 

    }) { (failureMsz) in 

     showLog(classname: self.className, tagName: failureMsz) 
     self.homeViewCOntroller.view.hideToastActivity() 

    } 

} 

/* Response object filter PLOT ROUTE APIs for SUCCESS or FAILED */ 
func responseJsonObjectFilter(jsonObject:AnyObject){ 
    showLog(classname: className ,tagName: "responseJsonObjectFilter") 

    if let jsonObjectDictionary = jsonObject as? NSDictionary { 

     if let statusMessage = jsonObjectDictionary["status"] as? String{ 

      if(statusMessage == "OK"){ 

       if let routesObject = jsonObjectDictionary["routes"] as? [Any] { 

        if routesObject[0] is NSDictionary { 

         let anyObject:AnyObject = routesObject[0] as AnyObject 
         if let routeObjectDictionary = anyObject as? NSDictionary{ 

          /* legs KEY for duration and distance */ 
          if let legsObject = routeObjectDictionary["legs"] as? [Any] { 

           let legsAnyObject:AnyObject = legsObject[0] as AnyObject 
           if let legsObjectDictionary = legsAnyObject as? NSDictionary{ 

            showLog(classname: className, tagName: legsObjectDictionary) 
            /* DISTANCE KEY for distance */ 
            if let distanceObjectNsDictionary = legsObjectDictionary["distance"] as? NSDictionary { 

             var estimatedDistance:Double = distanceObjectNsDictionary["value"] as! Double 
             estimatedDistance = estimatedDistance/1000 

             bookingModel.estimatedDistance = String(format: "%.2f", estimatedDistance) 

            } 

            /* DURATION KEY for duration */ 
            if let durationObjectNsDictionary = legsObjectDictionary["duration"] as? NSDictionary { 

             var estimatedTime:Double = durationObjectNsDictionary["value"] as! Double 
             estimatedTime = estimatedTime/60 

             bookingModel.estimatedTime = "\(estimatedTime)" 

            } 

           } 

          } 

          /* overview_polyline KEY for duration and distance */ 
          if let poylineObjectNsDictionary = routeObjectDictionary["overview_polyline"] as? NSDictionary { 

           showLog(classname: className, tagName: poylineObjectNsDictionary["points"] as! String) 
           bookingModel.route = poylineObjectNsDictionary["points"] as? String 
           generateRoute(uiMapView:homeViewCOntroller.uiMapView, encodedString: poylineObjectNsDictionary["points"] as! String) 

          } 

         } 
        } 
       } 

      }else{ 

       showSnackbar(uiView: self.homeViewCOntroller.view, message: "Could not plot route. Please check your internet connection.") 

      } 
     } 

    } 
    self.homeViewCOntroller.view.hideToastActivity() 
} 


/* Generate ROUTE to UIMapView */ 
func generateRoute(uiMapView:GMSMapView, encodedString:String){ 

    uiMapView.clear() 
    let path = GMSMutablePath(fromEncodedPath: encodedString) 
    let polyLine = GMSPolyline(path: path) 
    polyLine.strokeWidth = 3 
    polyLine.strokeColor = hexStringToUIColor(hex: redColor) 
    polyLine.map = uiMapView 

    let polyline = Polyline(encodedPolyline: encodedString) 
    let decodedCoordinates: [CLLocationCoordinate2D]? = polyline.coordinates 

    let pickupLat = decodedCoordinates?[0].latitude 
    let pickupLong = decodedCoordinates?[0].longitude 


    let dropLat = decodedCoordinates?[((decodedCoordinates?.count)! - 1)].latitude 
    let dropLong = decodedCoordinates?[((decodedCoordinates?.count)! - 1)].longitude 

    // Creates a marker in the center of the map. 
    let marker = GMSMarker() 
    marker.position = CLLocationCoordinate2D(latitude: pickupLat!, longitude: pickupLong!) 
    marker.snippet = "Pickup Location" 
    marker.icon = UIImage(named: "ic_pin_pick.png") 
    marker.map = uiMapView 

    // Creates a marker in the center of the map. 
    let marker2 = GMSMarker() 
    marker2.position = CLLocationCoordinate2D(latitude: dropLat!, longitude: dropLong!) 
    marker2.snippet = "Drop Location" 
    marker2.icon = UIImage(named: "ic_pin_drop.png") 
    marker2.map = uiMapView 

} 
相關問題