Android - RemoveUpdates不删除侦听器

时间:2013-07-03 16:38:33

标签: android gps google-maps-android-api-2

我一直在尝试从我的位置管理器中删除位置更新,但即使在调用removeupdates后,GPS符号也会持续显示。我已经确保传递了正确的侦听器变量名,并且为侦听器更新执行了代码。我只想找一次用户的位置。

谢谢

public class MapActivity extends FragmentActivity implements OnInfoWindowClickListener, OnCameraChangeListener {

    Map<Marker, Integer> markers = new HashMap<Marker, Integer>();
    VenueManager venueManager;
    GoogleMap map;
    long last_map_refresh = 0;
    Button search_btn;
    CameraPosition previous_position = null;
    private LocationManager locationManager;
    private static final long MIN_TIME = 400;
    private static final float MIN_DISTANCE = 1000;
    private ProgressDialog loading;
    private Button add_venue;
    SharedPreferences sharedPreferences;
    private LatLng user_position = null;
    private NetworkManager network_manager;
    private Boolean first_map_render = false;
    private LocationListener location_listener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

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

        if( !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) ) {
            AlertDialog.Builder builder = new AlertDialog.Builder(MapActivity.this);
            builder.setTitle("GPS not enabled");  // GPS not found
            builder.setMessage("This application requires you to enable GPS location settings. We recommend that you enable 'Use wireless networks' and 'Use GPS satellites'. Would you like to enable this setting now?"); // Want to enable?
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                    finish();
                    MapActivity.this.startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                }
            });
            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent home_intent = new Intent(MapActivity.this, HomeActivity.class);
                    startActivity(home_intent);
                    Toast.makeText(MapActivity.this, "You will not be able to use the map features of this application until you enable gps settings", Toast.LENGTH_LONG).show();
                    finish();
                }
            });

            builder.create().show();
            return;
        }
        else
        {
            network_manager = new NetworkManager();

            location_listener = new LocationListener() {
                public void onLocationChanged(Location location) {
                    // TODO Auto-generated method stub

                    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
                    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 16);
                    map.animateCamera(cameraUpdate);
                    draw_map(new CameraPosition.Builder().target(new LatLng(location.getLatitude(), location.getLongitude())).build());
                    user_position = latLng;

                    locationManager.removeUpdates(location_listener);
             };

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, location_listener);
}
}

1 个答案:

答案 0 :(得分:2)

首先使用LocationManager.NETWORK_PROVIDER时,您根本看不到GPS符号。

我发现您使用的是谷歌地图Android API v2,所以我的猜测超出了您放在这里的代码

map.setMyLocationEnabled(true);

显示蓝点并在使用Activity / MapFragment显示MapView时不断更新。