我正在我的论文中开发一个安卓谷歌地图,我可以问一些关于安卓驾驶方向的问题吗?
这是我的问题。
“当一个按钮被点击时,我如何在安卓谷歌地图中生成一个随机的行车路线,它会随机给出从起点到目的地的路径。”
谢谢!。
我通过这个问了谷歌地图。
private void parsing(GeoPoint start, GeoPoint end) throws ClientProtocolException, IOException, JSONException, URISyntaxException{
HttpClient httpclient = new DefaultHttpClient();
StringBuilder urlstring = new StringBuilder();
urlstring.append("https://maps.googleapis.com/maps/api/directions/json?origin=")
.append(Double.toString((double)start.getLatitudeE6()/1E6)).append(",").append(Double.toString((double)start.getLongitudeE6()/1E6)).append("&destination=")
.append(Double.toString((double)end.getLatitudeE6()/1E6)).append(",").append(Double.toString((double)end.getLongitudeE6()/1E6))
.append("&sensor=false");
//urlstring.append("http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=true");
url = new URI(urlstring.toString());
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = null;
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
reader.close();
String result = sb.toString();
JSONObject jsonObject = new JSONObject(result);
JSONArray routeArray = jsonObject.getJSONArray("routes");
JSONObject routes = routeArray.getJSONObject(0);
JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
String encodedString = overviewPolylines.getString("points");
List<GeoPoint> pointToDraw = decodePoly(encodedString);
//Added line:
mv_mapview.getOverlays().add(new RoutePathOverlay(pointToDraw));
}
点在地图上。
class MapOverlay extends Overlay {
@Override
public boolean onTap(final GeoPoint p, MapView mapView) {
// TODO Auto-generated method stub
geop_Tpoint = p;
mcon_mapcontrol = mapView.getController();
mcon_mapcontrol.animateTo(p);
mv_mapview.invalidate();
longitude = (int)(p.getLongitudeE6()*1E6);
latitude = (int)(p.getLatitudeE6()*1E6);
new AlertDialog.Builder(ShowMap.this)
.setTitle("Routes")
.setMessage("Display Routes?")
.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
textlocation.setText("Tap in the Map for Destination!");
dialog.dismiss();
}
})
.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
try{
textlocation.setText("");
parsing(geop_startpoint,geop_Tpoint);
geocodeExecuter(geop_startpoint,geop_Tpoint);
showButton.setEnabled(true);
}catch(Exception e){
textlocation.setText(""+ e.getMessage());
}
}
}).show();
showButton.setEnabled(true);
return true;
}
起点是通过 GPS,它会告诉我我在哪里。