获取多个坐标之间的路线

时间:2017-09-04 12:09:15

标签: ios swift google-maps mapkit

我想在许多坐标(lat,long)之间获得一个独特的方向。 例如,我有一个API获取请求,返回200坐标,我必须在地图中的200点之间创建一个唯一的方向。 我该怎么做(200点的数组按数学距离排序)。 例如:

protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    base.OnElementPropertyChanged(sender, e);

    if (e.PropertyName == "IsFocused")
    {
        // Do something
    }
}

2 个答案:

答案 0 :(得分:1)

属性: -

var mapView: MKMapView!
var polyLine: MKPolyline!
var lineView: MKPolylineView!

从纬度和经度创建坐标数组:

  var coordinateArray = [CLLocationCoordinate2D]()
for obj in arrayOfLatlong {
let coordinate = CLLocationCoordinate2DMake(lat1, lon1); 
coordinateArray.append(coordinate)
 }

//添加折线: -

 polyLine = MKPolyline(coordinates: coordinateArray, count: coordinateArray.count)
 mapView?.visibleMapRect = polyLine.boundingMapRect
//If you want the route to be visible
 mapView?.add(polyLine)

并在委托方法中: -

 func mapView(_ mapView: MKMapView, viewFor overlay: MKOverlay) -> 
  MKOverlayView {
if overlay == polyLine {
    if lineView == nil {
        lineView = MKPolylineView(polyline: routeLine)
        lineView.fillColor = UIColor.red
        lineView.strokeColor = UIColor.red
        lineView.lineWidth = 5
    }
    return lineView
}
return nil

}

创建一个步行路径: -

   // Source
     let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: 
  long)
     let placemark: MKPlacemark = MKPlacemark(coordinate: coordinate)
      let source =  MKMapItem(placemark: placemark)

   //Destination
    let destination =  MKMapItem(placemark: placemark)

    let request:MKDirectionsRequest = MKDirectionsRequest()
    request.source = source
    request.destination = destination
    // Specify the transportation type
    request.transportType = MKDirectionsTransportType.walking;

    request.requestsAlternateRoutes = true

    let directions = MKDirections(request: request)

    directions.calculate (completionHandler: {
        (response: MKDirectionsResponse?, error: NSError?) in
        if error == nil {
            let directionsResponse = response
            // Get whichever currentRoute you'd like, ex. 0
       self.arrayOfRoutes =  directionsResponse?.routes
        }
    } as! MKDirectionsHandler)

从arrayOfRoutes获取路线并在mapview上显示。

mapView.addOverlay(route.polyline, level: MKOverlayLevel.AboveRoads)

答案 1 :(得分:0)

您可以使用GMSPolylineGMSMutablePath创建路径。

将您的所有位置添加到名为NSMutableArray

的其他allLocations

然后使用以下代码

 GMSMutablePath *path = [GMSMutablePath path];
 for (NSArray *eachLocation in allLocations){
       NSString *latitude = [eachLocation objectAtIndex:1];
       NSString *longitude = [eachLocation objectAtIndex:0];
      [path addCoordinate:CLLocationCoordinate2DMake(latitude.doubleValue,longitude.doubleValue)];
 }
       rectangle = [GMSPolyline polylineWithPath:path];
       rectangle.strokeColor = [UIColor blueColor];
       rectangle.strokeWidth = 2.5f;
       rectangle.map = self.mapView;

将矩形GMSPolyline创建为全局矩形,以便您可以根据需要删除该路径。 mapViewGMSMapView的对象。