简体   繁体   English

连续跟踪用户的位置并绘制路径

[英]Tracking location of a user continuously and drawing a path wherever he goes

Well i developed a android program to get your location. 好吧,我开发了一个android程序来获取您的位置。 Now what i want is to get where you are exactly and track your movements and draw a line on the map of your path. 现在我想要的是到达正确的位置并跟踪您的运动,并在路径图上画一条线。 Read a lot of guides but cant make it work at all 阅读了大量指南,但根本无法使它起作用

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener {

private GoogleMap mMap;
LocationManager locationManager;
String provider;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    locationManager = (LocationManager)    getSystemService(Context.LOCATION_SERVICE);
    provider = locationManager.getBestProvider(new Criteria(), false);

}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    Location location= locationManager.getLastKnownLocation(provider);
    double lat =location.getLatitude();
    double lng =location.getLongitude();
    mMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).title("Marker"));

    // Add a marker and move the camera

}

@Override
public void onLocationChanged(Location location) {
    Double lat = location.getLatitude();
    Double lng = location.getLongitude();
    mMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).title("Marker"));
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 10));
    Log.i("Latitude", lat.toString());

}

Everything you code is write, But you are not working onLocationChanged on starting of Application. 您编写的所有代码均已编写,但是在Application启动时您无法在onLocationChanged上工作。

  1. getLastKnownLocation returns if location if there's cache otherwise it returns null add a check there. 如果存在缓存,则getLastKnownLocation返回location,否则返回null并在此处添加检查。

      if(location!=null) { double lat =location.getLatitude(); double lng =location.getLongitude(); mMap.addMarker(new MarkerOptions().position(new LatLng(lat,lng)).title("Marker")); } 
  2. fusedlocation api for better management with location refer here. fusedlocation api位置fusedlocation api ,可通过位置进行更好的管理。

  3. Try this to addMarker: 试试addMarker:

     public void drawMarker(double lat,double lon) { if (mMap != null) { MarkerOptions marker = new MarkerOptions().position(new LatLng(lat, lon)).title(" Maps Tutorial").snippet("Android Ruler"); marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)); // Moving Camera to a Location with animation CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude, longitude)).zoom(12).build(); mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); mMap.addMarker(marker); } } 

Please refer here: http://coderzpassion.com/android-google-maps-v2-tutorial-with-markers/ 请参阅此处: http : //coderzpassion.com/android-google-maps-v2-tutorial-with-markers/

  1. onLocationChanged(Location location): If the existing location is empty or the current location accuracy is greater than existing accuracy then store the current location. onLocationChanged(Location location):如果现有位置为空或当前位置精度大于现有精度,则存储当前位置。

So, you need to revise onLocationChanged(Location location) and write the application state values you want to change to the code like this: 因此,您需要修改onLocationChanged(Location location)并将要更改的应用程序状态值写入如下代码:

@Override
public void onLocationChanged(Location location) {

    double latitude = location.getLatitude();
    double longitude = location.getLongitude();

    myLatLng = new LatLng(latitude, longitude);

    zoomToPoints();

    if(!isDirectionDrawn) {

        new LongOperation().execute("");
    }https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi
}

private void moveToCurrentLocation(LatLng currentLocation)
    {
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, 15));
    // Zoom in, animating the camera.
    mMap.animateCamera(CameraUpdateFactory.zoomIn());
    // Zoom out to zoom level 10, animating with a duration of 2 seconds.
    mMap.an


}

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM