Swift 3 Google 地图 iOS SDK - 通过触摸添加标记
Swift 3 Google Maps iOS SDK - Add Marker via Touch
大家好!
我的 iOS 应用程序中有一个 Google 地图,我想做的是当我点击或长按时在地图中添加一个标记,并将坐标保存在数组中
例如:在 Android 中,我有方法 mMap.setOnLongClickListener()
并将这些标记保存在 Latlng 数组类型
中
任何帮助都是有用的!
谢谢
首先你需要添加一个 UILongPressGestureRecognizer
到你的 GMSMapView
并实现 UIGestureDelegate
协议以使其在 longPress 之后与 GMSMapView
的所有手势同时工作你应该翻译触摸的 CGPoint
并将其转换为 CLLocationCoordinate2D
剩下的是微不足道的
以此为例
//
// 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
}
}
GMSMapView 有一个委托,它有这个方法:
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate;
大家好!
我的 iOS 应用程序中有一个 Google 地图,我想做的是当我点击或长按时在地图中添加一个标记,并将坐标保存在数组中
例如:在 Android 中,我有方法 mMap.setOnLongClickListener()
并将这些标记保存在 Latlng 数组类型
任何帮助都是有用的! 谢谢
首先你需要添加一个 UILongPressGestureRecognizer
到你的 GMSMapView
并实现 UIGestureDelegate
协议以使其在 longPress 之后与 GMSMapView
的所有手势同时工作你应该翻译触摸的 CGPoint
并将其转换为 CLLocationCoordinate2D
剩下的是微不足道的
以此为例
//
// 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
}
}
GMSMapView 有一个委托,它有这个方法:
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate;