Android Studio:如何绘制并保存实时路线?

我正在一个项目中,要从我的位置移动时在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,//                                          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按钮时清除折线并将路线保存在数据库中:

Android Studio:如何绘制并保存实时路线?

shuojie 回答:Android Studio:如何绘制并保存实时路线?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3135553.html

大家都在问