0

我在使用 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();
  }
}
4

2 回答 2

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

}

希望这是你想要的。

于 2013-09-02T12:34:25.243 回答
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));
     }

    }
于 2013-09-02T12:44:53.910 回答