2016-12-14 119 views
0

我真的被困在這裏。我只是學習如何在我的視圖中顯示谷歌地圖。現在我想從一個地方指向另一個地方。在谷歌地圖上顯示方向並畫線

這是到目前爲止我的代碼:

@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) 

,我用的是從網絡示例中的鏈接。 現在我只想知道如何製作一條線和一個方向.. 感謝您的幫助

+0

你想在兩個位置之間畫折線? –

回答

1

我的答案是斯威夫特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函數並將源和目標座標傳遞給它,它也使用Alamofir E對於API命中

重要改變這種Macros.apiKeys.key您的谷歌API密鑰,並確保它使正確的功能

0

我將使用objective-c進行解釋。我希望你能把它翻譯成迅捷。

你已經使用這個pod 'GoogleMaps'了吧? 我認爲你已經成功顯示谷歌地圖。

GMSMapView *mapView_ = [GMSMapView mapWithFrame:self.mapView.frame camera:camera]; 

....

所以我會跳過它。

這裏我只解釋一下顯示方向。

NSString *url_string = @"https://maps.googleapis.com/maps/api/directions/json?origin=Disneyland&destination=Universal+Studios+Hollywood4&"; 

如果你的出發地和目的地的緯度和經度,你可以使用這個API:從我的代碼片斷 「https://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url_string]]; 

if (data) { 

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
NSArray *routesArray = [json objectForKey:@"routes"]; 
NSDictionary *routeDict = [routesArray objectAtIndex:0]; 
NSDictionary *routeOverviewPolyline = [routeDict objectForKey:@"overview_polyline"]; 
NSString *points = [routeOverviewPolyline objectForKey:@"points"]; 

GMSMutablePath *path = [GMSMutablePath path]; 
path = [GMSPath pathFromEncodedPath:points].mutableCopy; 

GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path]; 
rectangle.map = mapView_; 

}