2017-06-06 75 views
1

我正在GoogleMap中制作GMSPolyline。当我想通过编码polyline.map = nil删除GMSPolyline,但它不能为我工作。我在下面复制一些编码。我如何删除Swift 3中的GMSPolyline

(前要加上标记,我需要删除折线只)

谢谢您的帮助!

我的编码:

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 
} 

回答

0

你应该在地图上添加新的折线之前清除地图。 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. 

*/ 

clear()函数试试这个

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

我添加了标记到地图上,如果我使用mapView.clear()巫婆将被删除分数? – ShingHung

+0

@ShingHung在清除mapView之后,您必须重新绘制所有标记。然后它将以您期望的方式工作。还编辑了答案。请看看这个。 – SuryaKantSharma

+0

从JSON获取标记的滞后和纬度,如果我重绘标记,则使用更多数据。 – ShingHung