更新用户的位置

时间:2013-09-02 12:30:30

标签: android google-maps geolocation gps

我在使用Google地图时遇到了一些麻烦。我需要在地图上显示用户的位置,然后进行更新。我正在使用默认标记。但是,我的代码当前设置方式,每次调用OnLocationChanged时都会添加一个新标记。我希望它只是更新那个标记或删除旧标记,因为它添加了一个带有当前位置的标记。这是代码:

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 个答案:

答案 0 :(得分:2)

通过此

更新您的onLocationChanged方法
@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));
}

}

希望这是你想要的。

答案 1 :(得分:1)

您可以更新用户位置 onLocationchanged 方法....

 @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));
     }

    }