修复了ios谷歌地图上的标记

Yin*_*ini 10 iphone google-maps fixed drag ios

我正在使用谷歌地图SDK for ios.我想在屏幕中央固定一个标记,因此当用户拖动地图时,标记不会移动并保持在中心位置.我也试图在拖动后读取中心的坐标.任何输入将不胜感激.谢谢!

Pio*_*otr 6

我宁愿在GMSMapView顶部显示视图(不要为此使用标记)。由于您具有地图视图的屏幕位置,因此应该容易将视图放置在正确的位置。

要获取坐标,您可以使用 mapView.projection.coordinateForPoint

文档在这里

要知道拖动已完成,请使您成为地图视图(GMSMapViewDelegate)的视图控制器(或任何其他对象)的委托并实现mapView:idleAtCameraPosition:方法。

文件在这里


Shr*_*wan 6

创建 GMSMapView 的出口,并在地图中心和顶部的搜索栏附加 Image 以显示位置名称。

在 Device 上拖动地图,这将触发 GMSMapViewDelegate 的 2 个委托方法。

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition)?
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition)?
Run Code Online (Sandbox Code Playgroud)
    /**
     * Called repeatedly during any animations or gestures on the map (or once, if the camera is
     * explicitly set). This may not be called for all intermediate camera positions. It is always
     * called for the final position of an animation or gesture.
     */
    func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
      print("didchange")>             > 
       returnPostionOfMapView(mapView: mapView)
    }

    /**
     * Called when the map becomes idle, after any outstanding gestures or animations have completed (or
     * after the camera has been explicitly set).
     */
    func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
        print("idleAt")

        //called when the map is idle
        returnPostionOfMapView(mapView: mapView)

    }

    //Convert the location position to address 
    func returnPostionOfMapView(mapView:GMSMapView){
        let geocoder = GMSGeocoder()
        let latitute = mapView.camera.target.latitude
        let longitude = mapView.camera.target.longitude
        let position = CLLocationCoordinate2DMake(latitute, longitude)
        geocoder.reverseGeocodeCoordinate(position) { response , error in
            if error != nil {
                print("GMSReverseGeocode Error: \(String(describing: error?.localizedDescription))")
            }else {
                let result = response?.results()?.first
                let address = result?.lines?.reduce("") { $0 == "" ? $1 : $0 + ", " + $1 }
                self.searchBar.text = address
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Github 演示项目