2017-09-26 94 views
1

我正在研究它需要的應用程序使用GPS跟蹤在Google地圖上繪製我的步行/跑步路徑,還需要每次分開保存我的路徑。如何在走路,跑步,使用GPS行駛的同時在Google地圖上繪製多段線跟蹤並保存我的路徑evrytime

public class MapActivity extends FragmentActivity implements OnMapReadyCallback { 
private static final String TAG = "MapActivity"; 
public GoogleMap mMap; 
private ArrayList<LatLng> points; 
Polyline line; 
Marker now; 
double lat1; 
double lon1; 
String provider; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    points = new ArrayList<LatLng>(); 
    setContentView(R.layout.activity_map); 

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(map); 
    mapFragment.getMapAsync(this); 

} 


@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 
    if (!mMap.isMyLocationEnabled()) 
     mMap.setMyLocationEnabled(true); 

    LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

    Criteria criteria = new Criteria(); 
    criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
    provider = lm.getBestProvider(criteria, true); 

    //Location myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    Location myLocation = lm.getLastKnownLocation(provider); 


    if (myLocation == null) { 

     criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
     provider = lm.getBestProvider(criteria, false); 
     myLocation = lm.getLastKnownLocation(provider); 
    } 

    if (myLocation != null) { 
     LatLng userLocation = new LatLng(myLocation.getLatitude(), myLocation.getLongitude()); 

     lat1=myLocation.getLatitude(); 
     lon1=myLocation.getLongitude(); 

     mMap.addMarker(new MarkerOptions() 
       .position(userLocation) 
       .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)) 
       .title("Welcome") 
       .snippet("Latitude:"+lat1+",Longitude:"+lon1) 
     ); 

     Log.v(TAG, "Lat1=" + lat1); 
     Log.v(TAG, "Long1=" + lon1); 

     mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 18), 1500, null); 


     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, new LocationListener() { 
      @Override 
      public void onLocationChanged(Location myLocation) { 

       // Getting latitude of the current location 
       double latitude = myLocation.getLatitude(); 

       // Getting longitude of the current location 
       double longitude = myLocation.getLongitude(); 

       // Creating a LatLng object for the current location 
       LatLng latLng = new LatLng(latitude, longitude); 

       //Adding new marker 
       now = mMap.addMarker(new MarkerOptions() 
         .icon(BitmapDescriptorFactory 
           .defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)) 
         .position(latLng).title("New") 
         .snippet("Latitude:"+lat1+",Longitude:"+lon1) 
       ); 

       // Showing the current location in Google Map 
       mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 

       // Zoom in the Google Map 
       mMap.animateCamera(CameraUpdateFactory.zoomTo(18)); 

       //Draw polyline 
       drawPolygon(latitude, longitude); 


      } 

      @Override 
      public void onProviderDisabled(String provider) { 
       // TODO Auto-generated method stub 
      } 
      @Override 
      public void onProviderEnabled(String provider) { 
       // TODO Auto-generated method stub 
      } 
      @Override 
      public void onStatusChanged(String provider, int status, 
             Bundle extras) { 
       // TODO Auto-generated method stub 
      } 
     }); 

    } 


    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    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.getUiSettings().setZoomControlsEnabled(true); 
} 

private void drawPolygon(double latitude, double longitude) { 

    List<LatLng> polygon = new ArrayList<>(); 
    //old lat and long 
    polygon.add(new LatLng(lat1, lon1)); 
    //new lat and long 
    polygon.add(new LatLng(latitude,longitude)); 



    mMap.addPolygon(new PolygonOptions() 
      .addAll(polygon) 
      .strokeColor(Color.YELLOW) 
      .strokeWidth(10) 
      .fillColor(Color.YELLOW) 
    ); 

    lat1=latitude; 
    lon1=longitude; 
} 

}

我工作它需要一個應用程序使用GPS跟蹤吸取我的行走/跑步對谷歌地圖的路徑,也需要拯救我的路每一次separately.this是我的地圖活動。實際上我正在開發一個計步器應用程序,用於統計用戶的步數,距離和時間。

回答

0

如果我理解你,我認爲你可以使用onLocationChanged(當位置發生變化時調用),然後保存它。

例如:

public class GPSClass implements LocationListener { 

    public List<LatLng> polygon; 

void onLocationChanged(Location location) { 
    // Called when a new location is found by the network location provider. 
    Log.i("Message: ","Location changed, " + location.getAccuracy() + " , " + location.getLatitude()+ "," + location.getLongitude()); 
    updatePolygon(location.getLatitude(),location.getLongitude()); 
} 

public void onStatusChanged(String provider, int status, Bundle extras) {} 
public void onProviderEnabled(String provider) {} 
public void onProviderDisabled(String provider) {} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    polygon = new ArrayList<>(); 
    // initialize polygon with old locations ..... 
    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,this); 
} 

public void updatePolygon(double latitude, double longitude){ 
    polygon.add(new LatLng(latitude,longitude)); 
    mMap.addPolygon(new PolygonOptions() 
     .addAll(polygon) 
     .strokeColor(Color.YELLOW) 
     .strokeWidth(10) 
     .fillColor(Color.YELLOW) 
    ); 
    } 
} 

要求ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION權限。 Doc here

我希望我能夠幫助你

+0

感謝響應,但可以請你解釋一下多一點如何保存。或者請給我任何建議的鏈接,如果你有。 –

+0

你能解釋一下你想在這種情況下做什麼,爲什麼要保存位置和時間? – Jamesp

+0

我上面更新了我的地圖活動。我需要繪製並保存在我的用戶走路,/跑步/駕駛路徑的地圖上。當我打開它開始調整我的距離,時間和步驟,並且還需要在Google地圖上繪製我的用戶路徑,並且當我點擊完成時,它需要保存並添加到我的列表視圖中, –

相關問題