如何在 Google MapView iOS 中设置固定标记位置
How to set fixed Marker position in Google MapView iOS
这里我正在使用 GoogleMapKit
处理 mapKitView
,这里我想在用户使用 didBeginDragging
、didDragging
和 [ 拖动标记时执行此操作=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
position 到 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
}
}
这里我正在使用 GoogleMapKit
处理 mapKitView
,这里我想在用户使用 didBeginDragging
、didDragging
和 [ 拖动标记时执行此操作=18=]
并在屏幕的任意位置点释放 marker
。我从
- 拖动方法
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
position 到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
}
}