2017-06-06 5 views
1

Ich mache die GMSPolyline in der GoogleMap. Wenn ich die GMSPolyline durch Codierung polyline.map = nil entfernen möchte, kann es aber nicht für mich arbeiten. Ich kopiere etwas Codierung unten. Vielen Dank für Ihre HilfeWie kann ich die GMSPolyline in Swift entfernen 3

(die Markierung vor hinzugefügt, ich brauche nur die Polylinie entfernen)

!!!

meine Codierung:

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool { 
    //MARK: create the GMSPolyline 
    let polyline = GMSPolyline() 
    //MARK: remove the old polyline from the GoogleMap 
    polyline.map = nil 

    let origin = "\(mapView.myLocation!.coordinate.latitude),\(mapView.myLocation!.coordinate.longitude)" 
    let destination = "\(marker.position.latitude),\(marker.position.longitude)" 

    let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving" 

    Alamofire.request(url).responseJSON { response in 

     let json = JSON(data: response.data!) 
     let routes = json["routes"].arrayValue 

     //MARK: print route using Polyline 
     for route in routes 
     { 
      let routeOverviewPolyline = route["overview_polyline"].dictionary 
      let points = routeOverviewPolyline?["points"]?.stringValue 
      let path = GMSPath(fromEncodedPath: points!) 
      polyline = GMSPolyline(path: path) 
      polyline.strokeWidth = 4 
      polyline.strokeColor = UIColor.yellow 
      polyline.isTappable = true 
      polyline.map = self.mapView 
     } 
    } 
    return false 
} 

Antwort

0

Sie die Karte vor dem Hinzufügen neuen Linienzug auf der Karte löschen sollen.

clear() Funktion von GMSMapView

/** 

Clears all markup that has been added to the map, including markers, polylines and ground overlays. 
This will not clear the visible location dot or reset the current mapType. 

*/ 

Versuchen Sie, diese

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool { 
    //MARK: create the GMSPolyline 
    let polyline = GMSPolyline() 
    //MARK: remove the old polyline from the GoogleMap 
    mapView.clear() 
    //TODO:- Redraw all marker here. 
    let origin = "\(mapView.myLocation!.coordinate.latitude),\(mapView.myLocation!.coordinate.longitude)" 
    let destination = "\(marker.position.latitude),\(marker.position.longitude)" 

    let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving" 

    Alamofire.request(url).responseJSON { response in 

     let json = JSON(data: response.data!) 
     let routes = json["routes"].arrayValue 

     //MARK: print route using Polyline 
     for route in routes 
     { 
      let routeOverviewPolyline = route["overview_polyline"].dictionary 
      let points = routeOverviewPolyline?["points"]?.stringValue 
      let path = GMSPath(fromEncodedPath: points!) 
      polyline = GMSPolyline(path: path) 
      polyline.strokeWidth = 4 
      polyline.strokeColor = UIColor.yellow 
      polyline.isTappable = true 
      polyline.map = self.mapView 
     } 
    } 
    return false 
} 
+1

ich die Markierungen auf der Karte hinzugefügt, wenn ich mapView.clear bin mit() Hexe gelöscht wird die Marken? – ShingHung

+0

@ShingHung Sie müssen alle Marker neu zeichnen, nachdem Sie die mapView gelöscht haben. Dann wird es so funktionieren, wie Sie es erwarten. Bearbeitete auch die Antwort. Bitte sehen Sie sich das an. – SuryaKantSharma

+0

Die Verzögerung und der Breitengrad der Marker wurden von JSON erhalten, wenn ich die Marker neu zeichne, verwende ich mehr Daten. – ShingHung

Verwandte Themen