我尝试做一个Android应用程序,我试图使用谷歌地图方向API在2标记之间绘制线条,当我尝试解码JSON中的折线点我从goole服务器我有一堆标记设置在0.0,0.0附近,就像我的剧情片下面一样
我的折线解码功能是
private fun decodePoly(polylineArrayList: ArrayList<String>): List<LatLng> {
val poly = ArrayList<LatLng>()
var index = 0
for (i in 0 until polylineArrayList.size-1) {
val encoded = polylineArrayList[i]
val len = encoded.length
var lat = 0
var lng = 0
while (index < len -1) {
var b: Int
var shift = 0
var result = 0
do {
b = encoded[index++].toInt() - 63
result = result or (b and 0x1f shl shift)
shift += 5
} while (b >= 0x20 )
val dlat = if (result and 1 != 0) (result shr 1).inv() else result shr 1
lat += dlat
shift = 0
result = 0
do {
b = encoded[index++].toInt() - 63
result = result or (b and 0x1f shl shift)
shift += 5
} while (b >= 0x20 )
val dlng = if (result and 1 != 0) (result shr 1).inv() else result shr 1
lng += dlng
val p = LatLng(lat.toDouble() / 1E5,
lng.toDouble() / 1E5)
poly.add(p)
}
}
return poly
}
这里是我画线的代码(我从asynck任务中取得成功
private val options = PolylineOptions()
private val latLongB = LatLngBounds.Builder()
override fun onSuccess(googleDirectionData: GoogleDirectionData?) {
val polylineArrayList = ArrayList<String>()
for (route in googleDirectionData?.routes!!) {
for (leg in route.legs) {
for (step in leg.steps)
polylineArrayList.add(step.polyline.points)
}
}
val polypts = decodePoly(polylineArrayList)
for (point in polypts) {
options.add(point)
latLongB.include(point)
}
val caisseMarker = LatLng(caisse.wsg84[0], caisse.wsg84[1])
options.add(caisseMarker)
latLongB.include(caisseMarker)
val bounds = latLongB.build()
// add polyline to the map
map.addPolyline(options)
// show map with route centered
map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100))
}
有人知道如何解决这个问题 不再有标记位置0.0,0.0?
答案 0 :(得分:0)
如果你想提升你的表现,你应该使用预建的库。
看看
https://github.com/jd-alexander/Google-Directions-Android
https://github.com/bkhezry/MapDrawingTools
https://github.com/akexorcist/Android-GoogleDirectionLibrary
简单语法
GoogleDirection.withServerKey("YOUR_SERVER_API_KEY")
.from(new LatLng(41.8838111, -87.6657851))
.and(new LatLng(41.8766061, -87.6556908))
.and(new LatLng(41.8909056, -87.6467561))
.to(new LatLng(41.9007082, -87.6488802))
.transportMode(TransportMode.DRIVING)
.execute(new DirectionCallback() {
@Override
public void onDirectionSuccess(Direction direction, String rawBody) {
if(direction.isOK()) {
// Do something
} else {
// Do something
}
}
@Override
public void onDirectionFailure(Throwable t) {
// Do something
}
});
答案 1 :(得分:-1)
是的但是我并不是真的在寻求表现它只是一个实习的项目,以便学习kotlin所以我不需要提高性能