Swift 3谷歌地图在触摸屏上添加标记
我有谷歌地图标记的问题,我想把标记放在触摸屏上,但我不知道如何处理我尝试了一些方法,但它不起作用,什么都没有发生,然后我触摸地图。压力识别器似乎有问题 更新:Swift 3谷歌地图在触摸屏上添加标记,swift,google-maps,google-maps-markers,Swift,Google Maps,Google Maps Markers,我有谷歌地图标记的问题,我想把标记放在触摸屏上,但我不知道如何处理我尝试了一些方法,但它不起作用,什么都没有发生,然后我触摸地图。压力识别器似乎有问题 更新: class MainMapController: UIViewController, CLLocationManagerDelegate { @IBOutlet weak var viewMap: GMSMapView! var makers: [GMSMarker] = [] var locationManager = CLLoca
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 Maps手动添加手势识别器,它自己管理交互,并具有专门的代理功能来处理常见手势 要长按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
viewMap是一个GMSMapView?是的,你是对的,当我点击地图时,我尝试了,但什么都没有发生。你能给我看更多的代码吗?更新了我的问题我试图添加这个,但当我点击地图时,什么都没有发生。你在长时间点击吗?我这里有一个示例项目,里面有这段代码,它对我来说很好。是的,我是,什么都没有发生,你能给我看看吗?看看这个git存储库。请记住将API_密钥添加到AppDelegate以运行。