`

解析Google Map Routes

阅读更多
为了获得从A点到B点的路劲,经常会使用Google提供的API,例如
[url]
http://maps.googleapis.com/maps/api/directions/json?origin=40.7144,-74.0060&destination=47.6063,-122.3204&sensor=false
[/url]
从返回的结果上,大致可以了解应该怎么走,但是如果希望在地图上把路径描绘出来,就需要把“overview_polyline”节点里的内容进行解密才可以
下面就是用java进行解密的方法
String path = "[加密过的路劲字符串]";

int index = 0, len = path.length();
len = path.length();
int lat = 0, lng = 0;
while (index < len) {
	int b, shift = 0, result = 0;
	do {
		b = path.charAt(index++) - 63;
		result |= (b & 0x1f) << shift;
		shift += 5;
	} while (b >= 0x20);
	int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
	lat += dlat;
	shift = 0;
	result = 0;
	do {
		b = path.charAt(index++) - 63;
		result |= (b & 0x1f) << shift;
		shift += 5;
	} while (b >= 0x20);
	int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
	lng += dlng;
	System.out.println(String.format("%f, %f", (double) lat / 1E5, (double) lng / 1E5));
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics