如何在 Google MapView iOS 中设置固定标记位置

How to set fixed Marker position in Google MapView iOS

这里我正在使用 GoogleMapKit 处理 mapKitView,这里我想在用户使用 didBeginDraggingdidDragging 和 [ 拖动标记时执行此操作=18=] 并在屏幕的任意位置点释放 marker。我从 的回答中找到了修复屏幕中标记的帮助内容,但这仅适用于屏幕中心的固定标记位置 only.So 我如何在任何特定屏幕点设置标记??

    func mapView(_ mapView: GMSMapView, didEndDragging marker: GMSMarker) {
        if marker == mapMarker{
            self.markerPosition = GMSCameraPosition(latitude: marker.position.latitude, longitude: marker.position.longitude, zoom: 5.0)
            print("End Dragging")
        }
    }
 func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
     if self.markerPosition == nil{
         mapMarker.position = position.target
     }else{
         mapMarker.position = self.markerPosition.target
     }
 }

回答我自己的问题。

首先添加变量来存储位置值 CGPoint

var markerPosition : CGPoint!
  • 然后在didEndDragging 标记 方法中将CLLocationCoordinate2D 位置值转换为CGPoint 并存储到markerPosition 中。
 func mapView(_ mapView: GMSMapView, didEndDragging marker: GMSMarker) {
        if marker == mapMarker{

            self.markerPosition = self.mapView.projection.point(for: CLLocationCoordinate2D(latitude: marker.position.latitude, longitude: marker.position.longitude))

            print("End Dragging")
        }
    }
  • 之后根据 CGPoint 值在任何屏幕点设置 GMSMarker positionMAPView
 func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
     if self.markerPosition == nil{
         //To set pin into the center of mapview
         mapMarker.position = position.target 

     }else{
         //To set pin into the any particular point of the screen on mapview
         let coordinate = self.mapView.projection.coordinate(for: self.markerPosition)
         mapMarker.position = coordinate
     }
 }