首页 > 解决方案 > 在 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是移动相机的像素比预期的要多。

在此处输入图像描述

标签: androidgoogle-mapscoordinates

解决方案


为了获得地图上某个 xy 位置的纬度和经度,您可以使用该fromScreenLocation()方法。

使用类似的东西:

LatLng latLng = map.getProjection().fromScreenLocation(new Point(x,y));

其中 x 和 y 是地图视图“地图”上的点。

您还可以使用toScreenLocation()来获取给定 LatLng 的 xy 位置。

Point pt = map.getProjection().toScreenLocation(latLng);

推荐阅读