0

Salut je l'ai écrit ce code pour dessiner une polyligne entre un point:Swift, MKPolyline comment créer polyligne entre point de coordonnées

var arrayToDraw: Array<Any> = [] 
var forpolyline: Array<CLLocationDegrees> = [] 
var forpolyline2: CLLocationCoordinate2D = CLLocationCoordinate2D.init() 


func showRoute() { 
    for h in 0...(orderFinalDictionary.count - 1){ 
     arrayToDraw = orderFinalDictionary[h].value 
     print(arrayToDraw) 
     var arrayToDrawCount = arrayToDraw.count 
     for n in 0...(arrayToDrawCount - 1){ 
      forpolyline = (arrayToDraw[n] as! Array<CLLocationDegrees>) 
      forpolyline2.latitude = (forpolyline[0]) 
      forpolyline2.longitude = (forpolyline[1]) 
      print(forpolyline2) 
       var geodesic = MKPolyline(coordinates: &forpolyline2, count: 1) 
       self.mapView.add(geodesic) 
     } 
    } 
} 

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { 
    let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline) 
    renderer.strokeColor = UIColor.red 
    renderer.lineWidth = 3 

    return renderer 
} 

Il prend les coordonnées d'un dictionnaire, placez-le dans un tableau (arraToDraw) et je utiliser forpolyline et forpolyline2 pour couler des valeurs.

Maintenant, le problème est qu'il ne dessine que des points sur les coordonnées comment puis-je connecter cela?

Répondre

1

Vous créez plusieurs polylignes avec un seul point, au lieu d'une seule polyligne avec plusieurs points. Il est difficile d'obtenir le code correct sans connaître la structure de votre dictionnaire, mais cela devrait correspondre davantage à ce que vous essayez de faire:

var arrayToDraw: Array<Any> = [] 
var forpolyline: Array<CLLocationDegrees> = [] 
var forpolyline2: CLLocationCoordinate2D = [CLLocationCoordinate2D]() 

func showRoute() { 
    for h in 0...(orderFinalDictionary.count - 1){ 
     arrayToDraw = orderFinalDictionary[h].value 
     print(arrayToDraw) 
     var arrayToDrawCount = arrayToDraw.count 
     for n in 0...(arrayToDrawCount - 1){ 
      forpolyline = (arrayToDraw[n] as! Array<CLLocationDegrees>) 
      forpolyline2.append(CLLocationCoordinate2D(latitude: forpolyline[0], longitude: forpolyline[1])) 
     } 
     print(forpolyline2) 
     let geodesic = MKPolyline(coordinates: &forpolyline2, count: forpolyline2.count) 
     self.mapView.add(geodesic) 
    } 
} 

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { 
    let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline) 
    renderer.strokeColor = UIColor.red 
    renderer.lineWidth = 3 

    return renderer 
} 
+0

En vous remerciant, cela fonctionne. –