iOS上谷歌地图的固定标记

10

我正在使用谷歌地图SDK for iOS。我想让一个标记固定在屏幕中央,这样当用户拖动地图时,标记不会移动并停留在中心位置。我还想在拖动后读取中心的坐标。任何意见都将不胜感激。谢谢!

2个回答

7
我希望将视图显示在GMSMapView的顶部(不要使用标记)。由于您已经拥有地图视图的屏幕位置,因此很容易将视图放置在正确的位置。
要获取坐标,您可以使用mapView.projection.coordinateForPoint 文档在这里 为了知道拖动何时完成,请使您的视图控制器(或任何其他对象)成为地图视图的代理(GMSMapViewDelegate),并实现mapView:idleAtCameraPosition:方法。
文档在这里

谢谢Piotr。我在GMSMapView上面显示视图方面遇到了困难。我尝试使用'insertSubview'和'addSubview',但只要我设置'self.view = _mapView;',标记视图就无法显示在顶部。 - Yini
首先添加标记视图,然后使用 yourMapView.bringSubviewToFront(view: yourMarkerView) 将其置于最前面。@Yini - Vinoth Vino

6

创建GMSMapView的插座(outlet),并将图片附加在地图中心,将搜索栏放置在顶部以显示位置名称。

在设备上拖动地图,这将触发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
            }
        }
    }

在此输入图片描述

Github演示项目


在这种情况下,使用位置管理器委托的区域是什么? - Pramod Shukla

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