我正在一个项目中,要从我的位置移动时在Google地图上绘制路径。我想知道如何保存我创建的路线。
这是我的MainActivity:
[$class: 'CascadeChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
description: 'Revision of the application on kubernetes',
name: 'revision',
randomName: 'choice-parameter-5633384460832177',
referencedParameters: 'namespaces,deployment',
script: [
$class: 'GroovyScript',
script: [
classpath: [],
sandbox: true,
script: """
if (namespaces.equals("Select")){
return["Nothing to do - Select your deployment"]
} else {
def command = "kubectl rollout history deploy --kubeconfig=${kubefilePrd} -n " + namespaces + " " + deployment + "| grep -v REVISION | grep -v deployment | cut -f1 -d' '"
def output = ['bash', '-c', command].execute().in.text
return output.split().toList()
}
"""
]
XML:
public class MainActivity extends AppCompatActivity implements
OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
com.google.android.gms.location.LocationListener {
Button btnStart;
int flag;
private GoogleMap mMap;
private Polyline gpsTrack;
private SupportMapFragment mapFragment;
private GoogleApiClient googleApiClient;
private LatLng lastKnownLatLng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = findViewById(R.id.btnStart);
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
if (googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.color(Color.RED);
polylineOptions.width(10);
gpsTrack = mMap.addPolyline(polylineOptions);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mMap = googleMap;
mMap.setMinZoomPreference(6.6f);
mMap.setMaxZoomPreference(20.20f);
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
LocationServices.getFusedLocationProviderClient(this).getLastLocation()
.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null){
Log.i("teste", "deu");
LatLng atual = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(atual).title("Localizção
atual"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(atual, 15));
}else {
Log.i("teste", "n deu");
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
@Override
protected void onStart() {
googleApiClient.connect();
super.onStart();
}
@Override
protected void onStop() {
googleApiClient.disconnect();
super.onStop();
}
@Override
protected void onPause() {
super.onPause();
stopLocationUpdates();
}
@Override
public void onResume() {
super.onResume();
if (googleApiClient.isConnected()) {
//startLocationUpdates();
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
if (googleApiClient.isConnected()) {
//startLocationUpdates();
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
lastKnownLatLng = new LatLng(location.getLatitude(), location.getLongitude());
updateTrack();
}
protected void startLocationUpdates() {
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(15 * 1000);
locationRequest.setFastestInterval(5 * 1000);
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest,
this);
}
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(
googleApiClient, this);
}
private void updateTrack() {
List<LatLng> points = gpsTrack.getPoints();
points.add(lastKnownLatLng);
gpsTrack.setPoints(points);
}
public void socorro(View v) {
switch (v.getId()) {
case R.id.btnStart:
startLocationUpdates();
btnStart.setBackgroundResource(R.drawable.stop);
break;
}
}
}
这是在手机上运行的应用程序,每次检测到新位置时,我都会将最新的LatLng点添加到我的折线中。我要绘制路径,然后在按STOP按钮时清除折线并将路线保存在数据库中: