我试图从地图中心获取坐标,并在文本视图中显示,当我滚动地图时,坐标发生了变化。
这是我的Kotlin代码:
class ActivityMapsNewPlotBng :
AppCompatActivity(),
OnMapReadyCallback,
GoogleMap.OnCameraIdleListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener{
...
override fun onLocationChanged(newLocation: Location?) {
val latLng = LatLng(newLocation!!.latitude, newLocation.longitude)
mMapsNPlotBng!!.moveCamera(CameraUpdateFactory.newLatLng(latLng))
mMapsNPlotBng!!.animateCamera(CameraUpdateFactory.zoomTo(18f))
if (mGoogleApiClient != null){
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this)
}
}
override fun onCameraIdle() {
val centerPos : LatLng = mMapsNPlotBng.cameraPosition.target
tv_nplotbng_lat.setText("" + centerPos.latitude)
tv_nplotbng_long.setText("" + centerPos.longitude)
}
}
这个代码不起作用,请帮帮我。感谢关注。
在onCameraIdle方法中
LatLng latLng = googleMap.getProjection().getVisibleRegion().latLngBounds.getCenter();