2017-09-04 4 views
0

Ich möchte eine eindeutige Richtung zwischen vielen Koordinaten erhalten (lat, long). Zum Beispiel habe ich eine API bekommen Anfrage, die mir 200 Koordinaten zurückgeben, und ich muss eine eindeutige Richtung zwischen al dieser 200 Punkt in Karte erstellen. Wie kann ich das tun (das Array von 200 Punkten ist nach Mathe sortiert). Beispiel:Route berechnen zwischen mehreren Koordinaten

[9.6247279999999993, 46.170966100000001] 
[9.6244014, 46.171050399999999] 
[9.6240834999999993, 46.171273900000003] 
[9.6240570000000005, 46.171284999999997] 

Antwort

0

Eigenschaften: -

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

erstellen Array Koordinate aus Längen- und Breiten langitude:

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

// hinzufügen Linienzug: -

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

und in Delegatmethode: -

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 

}

einen begehbaren Weg schaffen: -

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

Route planen von arrayOfRoutes und zeigen auf mapview.

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

Ich möchte keine Linie, sondern einen begehbaren Pfad erstellen –

0

Sie können einen Pfad erstellen, indem GMSPolyline und GMSMutablePath verwenden.

alle Standorte auf einen anderen NSMutableArray als allLocations

Namen hinzufügen und dann folgenden Code

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; 

verwenden Wo Rechteck GMSPolyline als global erstellt ist, so dass Sie diesen Weg entfernen, wenn Sie möchten. Und mapView ist Objekt für GMSMapView.

+0

Ich möchte keine Linie erstellen, sondern einen begehbaren Pfad –

Verwandte Themen