android - 在 Android 谷歌地图中使用像素修改坐标
问题描述
我正在使用谷歌地图并尝试camera
使用纬度和经度将其移动到某个位置。我不想使用对象scrollBy
中提供的方法,Camera
因为我想在修改后获得新的纬度和经度。
val myLocation = LatLng(lat,lng)
val latitudeOffset = 100 // pixels
val cameraCenter = getModifiedLocation(myLocation, latitudeOffset)
moveCamera(CameraUpdateFactory.newLatLngZoom(cameraCenter, CAMERA_ZOOM))
我试过用这样的东西
fun getModifiedLocation(location, latitudeOffset) {
val offsetInCoordinates = latitudeOffset / 2.pow(CAMERA_ZOOM)
return LatLng(location.latitude - offsetInCoordinates, location.longitude)
}
问题offsetInCoordinates
是移动相机的像素比预期的要多。
解决方案
为了获得地图上某个 xy 位置的纬度和经度,您可以使用该fromScreenLocation()
方法。
使用类似的东西:
LatLng latLng = map.getProjection().fromScreenLocation(new Point(x,y));
其中 x 和 y 是地图视图“地图”上的点。
您还可以使用toScreenLocation()
来获取给定 LatLng 的 xy 位置。
Point pt = map.getProjection().toScreenLocation(latLng);