Swift - GoogleMaps SDK 触摸获取坐标

4

我是Swift和Google Maps SDK的新手,想知道如何使用Google Maps SDK获取用户点击的坐标。例如,如果用户在地图上按下手指,会在那里创建一个注释。我非常感谢您的帮助,谢谢。

4个回答

13
在GMSMapViewDelegate中有一个名为mapView:didLongPressAtCoordinate:的方法,它在长按特定坐标后被调用。请参见此处的参考资料。
通过实现这个方法,你可以向地图视图添加一个标记:
func mapView(mapView: GMSMapView!, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
     let marker = GMSMarker(position: coordinate)
     marker.title = "Hello World"
     marker.map = mapView
}

对于轻触手势,可以实现一个类似的委托方法,称为mapView:didTapAtCoordinate:,可以以类似的方式使用:

对于轻拍手势,可以实现一个类似的委托方法,称为mapView:didTapAtCoordinate:,可以以类似的方式使用:

func mapView(mapView: GMSMapView!, didTapAtCoordinate coordinate: CLLocationCoordinate2D) {
     print("Tapped at coordinate: " + String(coordinate.latitude) + " " 
                                    + String(coordinate.longitude))
}

如果用户长按现有标记,会发生什么?我如何识别用户是在按标记还是地图? - Shahabuddin Vansiwala
您可以使用GMSMapViewDelegate mapView:didTapMarker:方法检测标记的轻触。不幸的是,在委托中没有检测标记长按的方法。因此,您需要使用mapView:didLongPressAtCoordinate:方法,然后根据其位置属性识别标记,可能通过循环比较标记坐标和委托提供的用户长按的坐标来实现。请参见:https://developers.google.com/maps/documentation/ios-sdk/reference/interface_g_m_s_marker.html#a2b9fdae0160d7acf439889ffcdb5f68b - George McDonnell

0

针对 Swift 5.0+

首先,请确保您已将 GMSMapViewDelegate 委托添加到您的 ViewController 类中

在您的类中添加此默认函数

func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
   debugPrint("Coordinates: ", coordinate)
}

如果你只想要坐标,那么上面的函数对你来说是完美的。但如果你想创建标记或从触摸获取本地地址,则请参见下面的函数。

func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
    let marker = GMSMarker(position: coordinate) //Add this line if you want to add marker

    let decoder = CLGeocoder()
    decoder.reverseGeocodeLocation(CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)) { placemarks, err in
        if let placeMark = placemarks?.first {
            let plName = placeMark.name ?? placeMark.subThoroughfare ?? placeMark.thoroughfare! //Place Name

            var address : String! = "" //This will be the local address
            if let subLocality = placeMark.subLocality ?? placeMark.name {
                address.append(subLocality)
                address.append(", ")
            }
            if let city = placeMark.locality ?? placeMark.subAdministrativeArea {
                address.append(city)
                address.append(", ")
            }
            if let state = placeMark.administrativeArea, let country = placeMark.country {
                address.append(state)
                address.append(", ")
                address.append(country)
            }
            // Add Marker:
            marker.title = plName
            marker.snippet = address
            marker.appearAnimation = .pop
            marker.map = mapView
        }
    }
 }

此函数不仅获取坐标,还使用从坐标中获取的所有详细信息(如地名、城市、州、国家等)创建标记

如果您只想要本地地址,则删除与marker相关的所有代码行

我使用CLGeocoder而不是GoogleMapDelegateGMSGeocoder原因是Apple的CLGeocoder在获取地名时更加精确,而GMSGeocoder不会准确地获取地名。

  • 还请注意:对于每个应用程序,Apple的地理编码请求都受到速率限制,因此在短时间内进行过多的请求可能会导致某些请求失败。

0

试试这个

    extension ViewController: GMSMapViewDelegate {

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

        print("Tapped at coordinate: " + String(coordinate.latitude) + " "
                + String(coordinate.longitude))
      }

    }

0
通过按照@Nayan Dave的答案,你可以通过点击来获取坐标,并使用下面的代码获取placeMark地理编码。
        func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
        let geocoder = GMSGeocoder()
        geocoder.reverseGeocodeCoordinate(coordinate) { response, error in

            if error != nil {
                print("reverse geodcode fail: \(error!.localizedDescription)")
            } else {
                if let places = response?.results() {
                    if let place = places.first {
                        if let lines = place.lines {

                 // this will print the address formatted in on line

                        print("GEOCODE: Formatted Address: \(lines)")

              // GEOCODE: Formatted Address: ["213 Borough High St, London SE1 1JA, UK"]

                        }
                    } else {
                        print("GEOCODE: nil first in places")
                    }
                } else {
                    print("GEOCODE: nil in places")
                }
            }
        }
        // this to clear any marker on map
        mapView.clear()

        // this to center the camera of the map to the tapped area

        let camera = GMSCameraPosition.camera(withLatitude: coordinate.latitude, longitude: coordinate.longitude, zoom: 15.0)
        mapView.animate(to: camera)

        // this to add a marker on the tapped location

        let marker = GMSMarker(position: coordinate)
        marker.appearAnimation = .pop
        marker.map = mapView
  
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接