【问题标题】:How to set fixed Marker position in Google MapView iOS如何在 Google MapView iOS 中设置固定标记位置
【发布时间】:2019-08-26 13:32:23
【问题描述】:

这里我正在使用GoogleMapKit 处理mapKitView,而这里我想在用户使用didBeginDragging、didDragging 和didEndDragging 拖动标记时执行此操作 并在屏幕的任意位置释放marker。我从Hugo Jordao 的回答中找到了一个帮助内容来修复屏幕中的标记,但这仅适用于屏幕中心的固定标记位置。那么如何在任何特定的屏幕点设置标记??

  • 拖动方法
    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
     }
 }

【问题讨论】:

标签: ios swift google-maps google-maps-sdk-ios


【解决方案1】:

回答我自己的问题。

第一个添加变量来存储CGPoint中的位置值

var markerPosition : CGPoint!
  • 然后在didEndDragging marker方法中将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")
        }
    }
  • 之后将GMSMarker 位置设置在任何屏幕点上,将CGPoint 值设置为MAPView
 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
     }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 2017-03-26
    • 2020-10-30
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    相关资源
    最近更新 更多