我是 Android 开发的新手。我正在尝试获取当前位置和目的地坐标之间的方向。我已经设法捕获当前位置坐标并可以在它们之间折线。但是当我尝试使用 Google 地图方向 API 时它不起作用。任何人都可以建议我逐步实现这一目标。这是我的代码。如有失误请见谅。
try 块中的代码不起作用。非常感谢您的帮助。谢谢
@Override public void onLocationChanged(Location arg0) {
// getting the current location coordinates
currentLatitude = Double.toString(arg0.getLatitude());
currentLongitude = Double.toString(arg0.getLongitude());
dCurrentLatitude = arg0.getLatitude();
dCurrentLongitude = arg0.getLongitude();
LatLng Current_Location = new LatLng(dCurrentLatitude,
dCurrentLongitude);
Toast.makeText(this, currentLatitude, Toast.LENGTH_SHORT).show();
Toast.makeText(this, currentLongitude, Toast.LENGTH_SHORT).show();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
CameraUpdate updateCurrentLoc = CameraUpdateFactory.newLatLngZoom(
Current_Location, 16);
map.animateCamera(updateCurrentLoc);
map.addMarker(new MarkerOptions().position(Current_Location).title(
"Find me here"));
Polyline line = map.addPolyline(new PolylineOptions()
.add(new LatLng(dCurrentLatitude, dCurrentLongitude),
new LatLng(51.399191, -0.772394)).width(5)
.color(Color.RED));
try {
HttpResponse response = null;
String url = "http://maps.googleapis.com/maps/api/directions/xml?origin="
+ dCurrentLatitude
+ ","
+ dCurrentLongitude
+ "&destination="
+ 51.41642777550773
+ ","
+ -0.7500430941581 + "&sensor=false&units=metric";
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
response = httpClient.execute(httpPost, localContext);
} catch (Exception e) {
e.printStackTrace();
}
}