为了更新用户在Android地图视图中采取的路径,可以使用以下步骤:
dependencies {
implementation 'com.google.android.gms:play-services-maps:17.0.1'
}
public class MainActivity extends Activity implements OnMapReadyCallback {
private GoogleMap mMap;
private MapView mMapView;
private Polyline mPolyline;
private LatLng mLatLng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMapView = findViewById(R.id.map_view);
mMapView.onCreate(savedInstanceState);
mMapView.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
mLatLng = latLng;
if (mPolyline == null) {
mPolyline = mMap.addPolyline(new PolylineOptions()
.add(mLatLng)
.width(5)
.color(Color.RED));
} else {
List points = mPolyline.getPoints();
points.add(mLatLng);
mPolyline.setPoints(points);
}
}
});
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
protected void onDestroy