Swift 4.2:如何在Google地图上选择/取消选择多个标记

时间:2019-06-21 06:14:47

标签: swift google-maps google-maps-markers

我正在使用iOS中的swift在Google地图上实现多个标记。我已成功将所有标记放置在地图上,并可以使用“ didTap标记”方法选择标记。但是我无法取消选择它。当用户选择一个标记时,应取消选择其余标记。

extension MapVC {
    // Mark:- Create Marker and set position
    fileprivate func setMarkerOnMap() {
        if self.markerArray.count != 0 {
            for i in 0...self.markerArray.count - 1 {
                let data = self.markerArray[i]
                guard let lat = data.lat else {
                    return
                }
                guard let lon = data.long else {
                    return
                }           
                let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: Double(lat)!, longitude: Double(lon)!, zoom: zoomLevel)
                showMarker(position: camera.target, index: i)
            }
        }
    }

      // Mark:- Show marker on map
         fileprivate func showMarker(position: CLLocationCoordinate2D, inde x: Int) {
            let marker = GMSMarker()
             marker.position = position
             marker.icon = UIImage(named: "marker-unselected-icon")
             marker.accessibilityLabel = "\(index)" // get index from array when click on each marker to identify
             marker.map = self.mapView
         }
     }
     //MARK - Map
     extension MapVC: GMSMapViewDelegate {
         // MARK:- DidTap marker 
         func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker)->      Bool {
             if let markerLbl = marker.accessibilityLabel{
                 index = Int(markerLbl)
             }
            if marker != self.userMarker{
               marker.icon = UIImage(named: "marker-selected-icon")
               self.restaurantDetailView.isHidden = false
                self.distanceView.isHidden = true
               self.infoView.isHidden = true
           }
              var isReturn = true
             mapView.selectedMarker = nil

        if self.markerArray.count != 0 {
            for i in 0...markerArray.count - 1 {
                if i == index {
                    let storeInfo = markerArray[i]
                    storeInfo.isSelected = !storeInfo.isSelected
                    self.destinationLat = Double(storeInfo.lat!)!
                    self.destinationLng =  Double(storeInfo.long!)!
                     marker.icon = UIImage(named: "marker-selected-icon")
                    markerArray[i] = storeInfo
                } else {
                    marker.icon = UIImage(named: "marker-unselected-icon")
                    let storeInfo = markerArray[i]
                    storeInfo.isSelected = false
                    markerArray[i] = storeInfo
                }
            }
        } else {
            mapView.selectedMarker = nil
            isReturn = true
            return isReturn
        }
        return isReturn
    }
}

我正在使用did Tap方法更改选定的标记图像,但是很遗憾,它不起作用。我不知道我在做什么错。提前致谢。

1 个答案:

答案 0 :(得分:0)

当用户想要选择一个标记并想要取消选择所有其他标记时,下面的代码会很有用。

要取消选择地图上的所有标记:

for (marker in self.markerArray) {
  self.mapview.selectedMarker = nil
}

在地图上选择标记:

self.mapView.selectedMarker = self.userMarker