如何在谷歌地图上绘制折线,同时步行,跑步,驾驶使用GPS跟踪并保存我的路径evrytime

时间:2017-09-26 08:57:29

标签: android google-maps gps direction

我正在开发一个应用程序,它需要使用GPS跟踪在Google地图上绘制我的步行/跑步路径,还需要每次单独保存我的路径。

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跟踪在Google地图上绘制我的步行/跑步路径,还需要每次单独保存我的路径。这是我的地图活动。实际上我正在开发一个计步器应用程序,它计算用户的步数,距离,时间。

1 个答案:

答案 0 :(得分: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

我希望我能帮到你