在Google地图上添加开始和结束位置

时间:2017-10-11 14:29:30

标签: ios swift google-maps

我想首先在地图视图上显示开始标记,当用户点击此标记时显示结束标记的结束位置。有我的代码添加两个标记,但那不起作用。我无法检测用户选择开始位置或结束。

var startMarker:GMSMarker!

var endMarker:GMSMarker!

     func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {

        let latitude = mapView.camera.target.latitude
        let longitude = mapView.camera.target.longitude
        centerMapCoordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        self.placeMarkerOnCenter(centerMapCoordinate:centerMapCoordinate)

      }
      func placeMarkerOnCenter(centerMapCoordinate:CLLocationCoordinate2D) {
        if startMarker:GMSMarker == nil {
          startMarker:GMSMarker = GMSMarker()
        }

        if endMarker == nil {
          endMarker = GMSMarker()
        }
        endMarker.icon = GMSMarker.markerImage(with: #colorLiteral(red: 1, green: 0.2509803922, blue: 0.5058823529, alpha: 1))
        endMarker.position = centerMapCoordinate
        endMarker.map = self.mapView

        startMarker.position = centerMapCoordinate
        startMarker.map = self.mapView
        startMarker.icon = GMSMarker.markerImage(with: #colorLiteral(red: 0.5843137503, green: 0.8235294223, blue: 0.4196078479, alpha: 1))

      }

如何隐藏结束标记,并在用户点击并指定起始位置时显示。

1 个答案:

答案 0 :(得分:0)

这是您的编辑代码。

    var startMarker:GMSMarker!

   var endMarker:GMSMarker!

 func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {

    let latitude = mapView.camera.target.latitude
    let longitude = mapView.camera.target.longitude
    centerMapCoordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    self.placeMarkerOnCenter(centerMapCoordinate:centerMapCoordinate)

  }
  func placeMarkerOnCenter(centerMapCoordinate:CLLocationCoordinate2D) {
    if startMarker:GMSMarker == nil {
      startMarker:GMSMarker = GMSMarker()
    }

    if endMarker == nil {
      endMarker = GMSMarker()
    }

    startMarker.position = centerMapCoordinate
    startMarker.map = self.mapView
    startMarker.icon = GMSMarker.markerImage(with: #colorLiteral(red: 0.5843137503, green: 0.8235294223, blue: 0.4196078479, alpha: 1))


    endMarker.position = centerMapCoordinate

  }

现在添加下面给出的另一个函数

func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool {

  startMarker.map = nil

  endMarker.icon = GMSMarker.markerImage(with: #colorLiteral(red: 1, green: 0.2509803922, blue: 0.5058823529, alpha: 1))
  endMarker.map = self.mapView

  return true
}

我希望这能解决你的问题。