获取谷歌地图的经纬度中心 [英] Get Latitude and longitude center of Google Map

查看:133
本文介绍了获取谷歌地图的经纬度中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示全屏mapView,始终获取mapView中心的纬度和经度,并在此点显示标记.

I want to show full screen mapView, always get Latitude and longitude of center of mapView and show marker in this point.

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {

    let lat = mapView.camera.target.latitude
    print(lat)

    let lon = mapView.camera.target.longitude
    print(lon)


    marker.position = CLLocationCoordinate2DMake(CLLocationDegrees(centerPoint.x) , CLLocationDegrees(centerPoint.y))
    marker.map = self.mapView
    returnPostionOfMapView(mapView: mapView)

  }

  func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
    print("idleAt")

    //called when the map is idle

    returnPostionOfMapView(mapView: mapView)

  }

  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 }

        print(address)
//        self.searchBar.text = address
      }
    }
  }

我如何使用此代码才能知道returnPostionOfMapView方法返回的纬度和经度是中心mapView的位置并在此位置显示标记?

i use this code in how can know Latitude and longitude that return in returnPostionOfMapView method is position of center mapView and show marker in this position?

推荐答案

通过使用Google地图的func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition)委托,您可以正确地获取地图的中心.

You are doing it right to get the center of the map by using the google maps's func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) delegate.

为中心坐标取一个变量

var centerMapCoordinate:CLLocationCoordinate2D!

让此代表知道中心位置.

Implement this delegate to know the center position.

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
    let latitude = mapView.camera.target.latitude
    let longitude = mapView.camera.target.longitude
    centerMapCoordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    self.placeMarkerOnCenter(centerMapCoordinate:centerMapCoordinate)
}

将标记放置在中心点上的功能

Function to place a marker on center point

func placeMarkerOnCenter(centerMapCoordinate:CLLocationCoordinate2D) {
    let marker = GMSMarker()
    marker.position = centerMapCoordinate
    marker.map = self.mapView
}

在这种情况下,您将获得很多标记.因此,请保留全局标记,并检查其是否已存在,只需更改位置即可.

In this case you will get a lot of markers. So keep a global hold of marker and check if it is already present, just change the position

var marker:GMSMarker!

func placeMarkerOnCenter(centerMapCoordinate:CLLocationCoordinate2D) {
    if marker == nil {
        marker = GMSMarker()
    }
    marker.position = centerMapCoordinate
    marker.map = self.mapView
}

这篇关于获取谷歌地图的经纬度中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆