我需要一些帮助。我正在构建一个GPS跟踪应用程序,其中应用程序绘制我的运动的折线,当我停止跟踪我的运动并再次单击开始时,应用程序将擦除所有先前绘制的折线并开始绘制新的。每当我开始聚合线条时,我会再次启动应用程序,一段时间后显示以前绘制的线条(即使它们被删除后)。
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import java.util.ArrayList;
import java.util.List;
public class MapsActivity extends AppCompatActivity implements LocationListener {
private GoogleMap mMap;
Button start,stop;
double OLDlatitude = 100;
double OLDlongitude = 100;
boolean draw = false;
int width = 5;
int color = Color.BLUE;
LocationManager locationManager;
Polyline polyline = null;
PolylineOptions polylineOptions;
List<Polyline> mPolylines = new ArrayList<Polyline>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
stop.setEnabled(false);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startK();
}});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stopK();
}});
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap=mapFragment.getMap();
polylineOptions = new PolylineOptions();
polylineOptions.width(width);
polylineOptions.color(color);
polylineOptions.geodesic(true);
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean gps_enabled = false;
try {
gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch(Exception ex) {}
if(!gps_enabled ) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Aplikácia potrebuje aktívne GPS na fungovanie. Chcete ho zapnúť ?")
.setCancelable(false)
.setPositiveButton("Áno", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("Nie", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
System.exit(0);
}
});
final AlertDialog alert = builder.create();
alert.show();
}
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 2000, 0, this);
Toast.makeText(this, "Na začatie kreslenia polohy kliknite na štart", Toast.LENGTH_LONG).show();
}
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng NEWlatLng = new LatLng(latitude, longitude);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(NEWlatLng)
.zoom(17)
.bearing(0)
.tilt(0)
.build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
if(draw)
{
if(OLDlatitude==100 && OLDlongitude==100)
{
OLDlatitude = latitude;
OLDlongitude = longitude;
}
Location loc1 = new Location("");
loc1.setLatitude(OLDlatitude);
loc1.setLongitude(OLDlongitude);
Location loc2 = new Location("");
loc2.setLatitude(latitude);
loc2.setLongitude(longitude);
float distanceInMeters = loc1.distanceTo(loc2);
if(distanceInMeters >= 1.0 )
{
polylineOptions.add(NEWlatLng);
mPolylines.add(this.mMap.addPolyline(polylineOptions));
OLDlatitude = latitude;
OLDlongitude = longitude;
}
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
public void startK() {
Toast.makeText(this, "Kreslenie bolo začaté", Toast.LENGTH_LONG).show();
draw = true;
stop.setEnabled(true);
start.setEnabled(false);
if(mPolylines.size() != 0)
{
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Na mape je nakreslená trasa chcete túto trasu uložiť ?")
.setCancelable(false)
.setPositiveButton("Áno", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
}
})
.setNegativeButton("Nie", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
for(Polyline line : mPolylines)
{
line.remove();
}
mPolylines.clear();
mPolylines = new ArrayList<Polyline>();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
}
public void stopK() {
Toast.makeText(this, "Kreslenie bolo zastavené", Toast.LENGTH_LONG).show();
OLDlongitude = 100;
OLDlatitude = 100;
draw = false;
stop.setEnabled(false);
start.setEnabled(true);
}
@Override
protected void onPause() {
if (this.isFinishing()){
draw = false;
locationManager.removeUpdates(this);
}
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
}
}
答案 0 :(得分:1)
您应该清除地图:
mMap.clear();
public final void clear()
从地图中删除所有标记,折线,多边形,叠加层等。
答案 1 :(得分:0)
只是通过我自己而不是
来计算mPolylines = new ArrayList<Polyline>();
我放在那里
polylineOptions = new PolylineOptions();
不完全支持但我认为polylineOptions会跟踪所有添加的latlang对象,即使它们是从arrayList中删除的。如果有人有一些更复杂的解释,我会非常感谢它。