我在这个应用程序中开发一个Android应用程序,我想在地图上绘制路径
当用户移动路径自动绘制时,我发现lat和long以及每次更改
当位置发生变化时,我希望在每次更改位置时在地图上绘制线条
我试过这个
public void onLocationChanged(Location location) {
String msg = "Location:" + location.getLatitude() + ","
+ location.getLongitude();
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
double new_lat = location.getLatitude();
double new_long = location.getLongitude();
drawTrack(new_lat, new_long, previous_lat, previous_long);
previous_lat = new_lat;
previous_long = new_long;
}
答案 0 :(得分:2)
这就是我所做的:
public void onLocationChanged(Location location) {
prevLatitude = latitude;
prevLongitude = longitude;
latitude = location.getLatitude();
longitude = location.getLongitude();
latLng = new LatLng(latitude, longitude);
PolylineOptions pOptions = new PolylineOptions()
.width(5)
.color(Color.GREEN)
.geodesic(true);
for (int z = 0; z < routePoints.size(); z++) {
LatLng point = routePoints.get(z);
pOptions.add(point);
}
line = googleMap.addPolyline(pOptions);
routePoints.add(latLng);
}
其中routePoints是LatLng的arraylist,line是你的折线,googleMap当然是你的GoogleMap。所以在课程开头添加:
private GoogleMap googleMap;
private Polyline line;
private ArrayList<LatLng> routePoints;
并在onCreate中添加:
routePoints = new ArrayList<LatLng>();
希望这对你有所帮助!:)