大家好!
我的 iOS 应用程序中有一个 Google map ,我想做的是,当我点击或长按时,我在 map 中添加一个标记,并将坐标保存在数组中
例如:在 Android 中,我有方法 mMap.setOnLongClickListener()
并将这些标记保存在 Latlng 数组类型
任何帮助都会很有用! 谢谢
最佳答案
首先,您需要将 UILongPressGestureRecognizer
添加到您的 GMSMapView
并实现 UIGestureDelegate
协议(protocol),使其与 的所有手势同时工作>GMSMapView
之后,在长按操作中,您应该翻译触摸的 CGPoint
并将其转换为 CLLocationCooperative2D
剩下的很简单
以此为例
//
// DashedOverlayViewController.swift
// GoogleMapsExample
//
// Created by Reinier Melian on 17/07/2017.
// Copyright © 2017 Pruebas. All rights reserved.
//
import UIKit
import GoogleMaps
import GooglePlaces
class DashedOverlayViewController: UIViewController {
@IBOutlet weak var mapView: GMSMapView!
var arrayCoordinates : [CLLocationCoordinate2D] = []
var longPressRecognizer = UILongPressGestureRecognizer()
@IBAction func longPress(_ sender: UILongPressGestureRecognizer) {
debugPrint("You tapped at YES")
let newMarker = GMSMarker(position: mapView.projection.coordinate(for: sender.location(in: mapView)))
self.arrayCoordinates.append(newMarker.position)
newMarker.map = mapView
}
override func viewDidLoad() {
super.viewDidLoad()
longPressRecognizer = UILongPressGestureRecognizer(target: self,
action: #selector(self.longPress))
longPressRecognizer.minimumPressDuration = 0.5
longPressRecognizer.delegate = self
mapView.addGestureRecognizer(longPressRecognizer)
mapView.isMyLocationEnabled = true
mapView.settings.compassButton = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension DashedOverlayViewController : UIGestureRecognizerDelegate
{
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool
{
return true
}
}
关于ios - Swift 3 Google map iOS SDK - 通过触摸添加标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47755853/