2015-05-25 23 views
4

我正在使用Google地圖SDK for ios。我想在屏幕中心固定一個標記,所以當用戶拖動地圖時,標記不會移動並停留在中心。我也試圖在拖動之後讀取中心的座標。任何輸入將不勝感激。謝謝!谷歌地圖上的ios固定標記

回答

3

我寧願在GMSMapView之上顯示視圖(不要使用標記)。既然你有地圖視圖的屏幕位置,那應該很容易把你的視圖放在正確的位置。

要獲得座標,你可以使用mapView.projection.coordinateForPoint

文檔被here

要知道,拖動完成,讓您查看控制器(或其它任何物體)地圖視圖的委託(GMSMapViewDelegate)和實施mapView:idleAtCameraPosition:方法。

文檔是here

+0

感謝彼得。我很難在GMSMapView之上顯示視圖。我嘗試使用'insertSubview'和'addSubview',但只要設置'self.view = _mapView;',標記視圖就不會顯示在最上面。 – Yini

1

在地圖的中心創建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