我遇到谷歌地图标记的问题,我想触摸标记,但我不知道如何处理它我尝试了一些方法,但它没有工作,没有任何反应,然后我触摸地图。新闻识别似乎有问题。
更新
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)
}
}
答案 0 :(得分:6)
您不应手动为Google地图添加手势识别器,它会管理自己的交互,并具有专门的委托功能来处理常见手势。
要在GSMMapView上长按,请确保设置代理
self.mapView.delegate = self
然后连接适当的委托函数
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
}
}
上面的代码会在您长按的位置添加一个标记,您还可以添加标题和代码段。实际将其添加到地图的部分是marker.map = mapView