【问题标题】:How to show coordinate from center of maps in textview?如何在textview中显示地图中心的坐标?
【发布时间】:2018-07-22 06:45:37
【问题描述】:

我想从地图中心获取坐标并在 textview 中显示坐标,当我滚动地图时,textview 中的坐标会发生变化。此代码不起作用,虽然我滚动地图,但 textview 仅显示我当前位置的坐标。

我的 Kotlin 代码:

class ActivityMaps :
        AppCompatActivity(),
        OnMapReadyCallback,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener{
...
override fun onLocationChanged(newLocation: Location?) {
        val latLng = LatLng(newLocation!!.latitude, newLocation.longitude)
        mMaps!!.moveCamera(CameraUpdateFactory.newLatLng(latLng))
        mMaps!!.animateCamera(CameraUpdateFactory.zoomTo(18f))
        if (mGoogleApiClient != null){
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this)
        }

       mMaps.setOnCameraIdleListener(GoogleMap.OnCameraIdleListener() {
            val centerPos : LatLng = mMapsNPlotBng.cameraPosition.target

            tv_latitute!!.text = centerPos.latitude.toString()
            tv_longitude!!.text = centerPos.longitude.toString()

        })
    }
...
}

请帮助我,谢谢。

【问题讨论】:

  • 您希望您的文本视图始终位于地图中心吗?意味着即使地图移动,文本视图也应该保持在地图的中心?

标签: android google-maps kotlin


【解决方案1】:

在您的布局 xml 文件中,您必须在地图中心取一个标记。然后实现 setOnCameraChangeListener 并在 onCameraChange 方法中使用 cameraPosition.target 找到 latlong

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-20
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-25
    • 1970-01-01
    相关资源
    最近更新 更多