2016-04-30 75 views
0

我需要一點幫助。我正在構建一個GPS跟蹤應用程序,在該應用程序中繪製我運動的多段線,當我停止跟蹤我的動作並再次單擊開始時,應用程序將清除所有先前繪製的多段線並開始繪製新線。芽每次我開始polylinedrawing一段時間後應用程序顯示先前繪製的線(即使他們被刪除後)。谷歌地圖上刪除折線顯示

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

由我自己只是想出它,而不是

mPolylines = new ArrayList<Polyline>(); 

我已經把那裏

polylineOptions = new PolylineOptions(); 

不完全上岸,但我認爲的PolylineOptions跟蹤所有添加latlang的對象,甚至當他們從arrayList中刪除。如果有人有一些更復雜的解釋,我會感激地申請它。