ios - 使用 google maps sdk swift 创建标记

标签 ios swift xcode google-maps xcode8

我觉得我在做一些非常愚蠢的事情,而答案就在我面前。我已经尝试实现在 map 上点击的功能,这将在点击的点创建一个带有地址标题的标记。但是每当我点击 map 时,什么都没有发生,没有错误或任何东西。我什至尝试使用 longPress 函数,但这也不起作用。有人可以看看我的代码并告诉我我做错了什么吗?谢谢。

我的导入、locationManager 和 viewDidLoad

import UIKit
import GoogleMaps

class EventCreatorVC: UIViewController, GMSMapViewDelegate {

@IBOutlet weak var SearchMap: UISearchBar!
@IBOutlet weak var mapView: GMSMapView!


let marker = GMSMarker()

private lazy var locationManager: CLLocationManager = {
    let locationManager = CLLocationManager()
    locationManager.delegate = self
    return locationManager
}()


override func viewDidLoad() {
    super.viewDidLoad()


    locationManager.delegate = self
    mapView.accessibilityElementsHidden = false
    mapView.settings.myLocationButton = true
    locationManager.requestWhenInUseAuthorization() 
}

我的 map 配置:

  private func configureMapView() {
    mapView.delegate = self

    // Center on California.
    let camera = GMSCameraPosition.camera(withLatitude: 37.0, longitude: -120.0, zoom: 6.0)
    mapView.camera = camera

    mapView.settings.setAllGesturesEnabled(true)
    mapView.settings.consumesGesturesInView = true

    checkLocationAuthorizationStatus()
    //addLocationMarkers()
}

我有 2 个功能来尝试使其工作,第一个是长按,第二个只是轻按。

func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
let location = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)

    marker.position = location

    self.reverseGeocodeCoordinate(coordinate: coordinate, marker: marker)

    marker.map = mapView
}


func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {

    let location = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)

    marker.position = location

    self.reverseGeocodeCoordinate(coordinate: coordinate, marker: marker)

    marker.map = mapView

}

还有我的反向地理编码器

func reverseGeocodeCoordinate(coordinate: CLLocationCoordinate2D, marker: GMSMarker) {

    // 1
    let geocoder = GMSGeocoder()
    geocoder.reverseGeocodeCoordinate(coordinate) { response, error in

        //Add this line


        //Rest of response handling
    }

    // 2
    geocoder.reverseGeocodeCoordinate(coordinate) { response, error in
        if let address = response?.firstResult() {

            // 3
            let title = address.lines as [String]?
            marker.title = title?.first

            UIView.animate(withDuration: 0.25) {
                self.view.layoutIfNeeded()
            }
            }
        }
    }
}

最佳答案

userinteractionenabled 设置为 true 并将此行写入 viewdidload

    let polyLine: GMSPolyline = GMSPolyline()
    polyLine.isTappable = true
    mapView.isUserInteractionEnabled = true
    mapView.delegate = self

同时用这个改变你的didTap方法

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
        locationOfMarker = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)

        marker.position = locationOfMarker

        self.reverseGeocodeCoordinate(coordinate: coordinate, marker: marker)

        marker.appearAnimation = kGMSMarkerAnimationPop
    }

关于ios - 使用 google maps sdk swift 创建标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42125274/

相关文章:

ios - 支持 iPhone 4、5、6、6+ 的最佳接口(interface)方式是什么?

ios地址簿类似应用

ios - Xcode 无法识别我的 pod 框架

xcode - .htaccess 语法代码需要修改

ios - Xcode 6.4 Scheme 选项在 El Capitan Beta 中消失

ios - 验证一个无效的 NSTimer

ios - 如何在 Swift 中访问 iOS 私有(private) API?

swift - 获取不同颜色的背景向上滚动

swift - 为什么 Int.random() 比 arc4random_uniform() 慢?

swift - 获取相同路径下载的文件但无法打开该文件