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

时间:2015-05-25 15:42:21

标签: ios iphone google-maps fixed drag

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

2 个答案:

答案 0 :(得分:4)

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

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

文档为here

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

文档为here

答案 1 :(得分:4)

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

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

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) 
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) 
    /**
     * 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
            }
        }
    }

enter image description here

Github Demo Project