ios - Swift 3 Google map iOS SDK - 通过触摸添加标记

标签 ios swift google-maps google-maps-sdk-ios

大家好!
我的 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/

相关文章:

ios - 如何在 Swift 中解析由蓝牙设备发送的 float ?

ios - 退出 .ipa 文件后,带有 Spotify 的 iPhone 应用程序崩溃

ios - 如何在没有 Stroybord/Xib 的情况下使用 JTCalendar

android - 如何将定位模式从默认模式更改为 "high accuracy/battery saving"(仅限设备)

android - 如何更改 google maps api v2 的自定义信息窗口形状

java - fragment 中的android MapView

ios - 使用 UIAppearence 更改 UIViewController 的直接 subview 的背景颜色

swift - 如何快速检查 Facebook 的首次登录?

swift - Swift 2 中的 withUnsafePointer

ios - RxSwift var-outlet 绑定(bind)组织