我尝试用地图开发一个Android应用程序。我是Android应用开发的初学者,我不知道如何在我当前位置和目的地之间生成路径。我可以通过下面的代码按当前位置查看。
package com.example.myapp;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class Grandinmaps extends Activity implements OnMapReadyCallback{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grandinmaps);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
// TODO Auto-generated method stub
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(12.971907, 80.220462), 18));
}
}
我不知道生成目的地路径的代码。 任何帮助...... !!!请: - )
答案 0 :(得分:0)
对于绘图路线,您可以使用以下代码:
要求打印位置路线:
GmapV2Direction route;
Document document;
LatLng toPosition = new LatLng(routeData.get(i).latitude, routeData.get(i).longitude);
new GetRouteTask() {
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}.execute((LatLng) toPosition);
private class GetRouteTask extends AsyncTask<LatLng, Void, String> {
String response = "";
@Override
protected String doInBackground(LatLng... params) {
document = route.getDocument(fromPosition, params[0], GmapV2Direction.MODE_WALKING);//fromPosition is another LatLng object.can be your current location.
response = "Success";
return response;
}
@Override
protected void onPostExecute(String s) {
// directionMap.clear();
if (response.equalsIgnoreCase("Success")) {
Log.v("Map", "got here");
ArrayList<LatLng> directionPoint = route.getDirection(document);
PolylineOptions rectLine = new PolylineOptions().width(10).color(
Color.RED);
for (int i = 0; i < directionPoint.size(); i++) {
rectLine.add(directionPoint.get(i));
}
// Adding route on the map
map.addPolyline(rectLine);
}
}
}