gpt4 book ai didi

Swift 3 谷歌地图在触摸时添加标记

转载 作者:可可西里 更新时间:2023-11-01 00:21:04 26 4
gpt4 key购买 nike

我在使用 google map 标记时遇到问题,我想触摸标记,但我不知道如何处理我尝试了几种方法,但它不起作用,没有任何反应,然后我触摸了 map 。 pressrecognizer 似乎有问题。

更新:

class MainMapController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var viewMap: GMSMapView!
var makers: [GMSMarker] = []

var locationManager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()


initializeTheLocationManager()
self.viewMap.isMyLocationEnabled = true
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
self.viewMap.addGestureRecognizer(longPressRecognizer)


}

func handleLongPress(recognizer: UILongPressGestureRecognizer)
{
if (recognizer.state == UIGestureRecognizerState.began)
{
let longPressPoint = recognizer.location(in: self.viewMap);
let coordinate = viewMap.projection.coordinate(for: longPressPoint )
let marker = GMSMarker(position: coordinate)
marker.opacity = 0.6
marker.title = "Current Location"
marker.snippet = ""
marker.map = viewMap
makers.append(marker)
}
}


func initializeTheLocationManager()
{
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}


func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

var location = locationManager.location?.coordinate

cameraMoveToLocation(toLocation: location)
locationManager.stopUpdatingLocation()
}
func cameraMoveToLocation(toLocation: CLLocationCoordinate2D?) {
if toLocation != nil {
viewMap.camera = GMSCameraPosition.camera(withTarget: toLocation!, zoom: 15)
}
}

最佳答案

您不应为 Google map 手动添加手势识别器,它会自行管理交互并具有专门的委托(delegate)函数来处理常见手势。

要长按 GSMMapView,请确保您设置了委托(delegate)

self.mapView.delegate = self

然后连接适当的委托(delegate)函数

extension ViewController: GMSMapViewDelegate {
func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
// Custom logic here
let marker = GMSMarker()
marker.position = coordinate
marker.title = "I added this with a long tap"
marker.snippet = ""
marker.map = mapView
}
}

上面的代码将在您长按的位置添加一个标记,您还可以添加标题和片段,如您所见。实际将其添加到 map 的部分是 marker.map = mapView

关于Swift 3 谷歌地图在触摸时添加标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43877300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com