Updating user's location

2019-09-06 18:12发布

I'm having some trouble with Google Maps. I need to display user's location on the map and then update it. I'm using a default marker. However, the way my code is currently set up, it adds a new marker every time OnLocationChanged is called. I want it to just update that one marker or remove old one as it adds a new one with current location. Here's the code:

public class GoogleMapActivity extends FragmentActivity implements LocationListener {
Double longitude_user, latitude_user;
LatLng koordinate_user;
GoogleMap supportMap;
String title, address, type;
BitmapDescriptor bdf;
private LocationManager locationManager;
private String provider;
Location location;
Marker marker;

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_googlemaps);

    SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    supportMap = fm.getMap();

    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    Criteria criteria = new Criteria();
    provider = locationManager.GPS_PROVIDER;
    if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        provider = locationManager.NETWORK_PROVIDER;
    }


    if (provider == null){
        provider = locationManager.getBestProvider(criteria, false);
    }

    if (location != null) {
        System.out.println("Provider " + provider + " has been selected.");
        onLocationChanged(location);
      } else {
        Log.i("test", "Location not available");
      }

    location = locationManager.getLastKnownLocation(provider);

}

/* Request updates at startup */
  @Override
  protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(provider, 1000, 10, this);
    //Log.i("test", "Provider: " + provider);
  }

  /* Remove the locationlistener updates when Activity is paused */
  @Override
  protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
  }

  @Override
  public void onLocationChanged(Location location) {
    latitude_user = location.getLatitude();
    longitude_user = location.getLongitude();

    if (latitude_user != null && longitude_user != null) {
        koordinate_user = new LatLng(latitude_user, longitude_user);
        marker = supportMap.addMarker(new MarkerOptions()
                .position(koordinate_user));
    }

  }

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

  }

  @Override
  public void onProviderEnabled(String provider) {
    Toast.makeText(this, "Enabled new provider " + provider,
        Toast.LENGTH_SHORT).show();

  }

  @Override
  public void onProviderDisabled(String provider) {
    Toast.makeText(this, "Disabled provider " + provider,
        Toast.LENGTH_SHORT).show();
  }
}

2条回答
Melony?
2楼-- · 2019-09-06 18:44

you can update user location onLocationchanged method....

 @Override
 public void onLocationChanged(Location location) {
   latitude = location.getLatitude();
   longitude = location.getLongitude();

   if (latitude != null && longitude != null) {
     if(marker != null)
       marker.remove();
       latlng = new LatLng(latitude, longitude);
        marker = supportMap.addMarker(new MarkerOptions()
        .position(latlng));
     }

    }
查看更多
Melony?
3楼-- · 2019-09-06 18:47

Update your onLocationChanged method by this

@Override
public void onLocationChanged(Location location) {
latitude_user = location.getLatitude();
longitude_user = location.getLongitude();

if (latitude_user != null && longitude_user != null) {
    if(marker != null)
        marker.remove();
    koordinate_user = new LatLng(latitude_user, longitude_user);
    marker = supportMap.addMarker(new MarkerOptions()
            .position(koordinate_user));
}

}

Hope this is what you wanted.

查看更多
登录 后发表回答