2013-03-11 38 views
0

中打開,則不使用GPS服務我正在編寫代碼以便能夠使用本地化(基於GPS(如果可用,則基於網絡)。如果我在移動設備上打開GPS,進入我的活動,一切正常。 但是,如果我輸入關閉GPS的活動,然後在活動中啓用GPS,則沒有任何反應,並且基於網絡的位置仍然活動,而不是切換到基於GPS的位置。如果在活動

我錯過了什麼嗎? 感謝

public class MapActivity extends Activity implements LocationListener { 

    //Used CameraPosition attributes 
    private final int MAP_BEARING = 69; 
    private final int SETUP_ZOOM = 16; 
    private final int AT_POSITION_ZOOM = 19; 
    private final int UPDATE_FREQUENCY = 20000; //Update frequency (ms) 
    private final LatLng MAP_CENTER = new LatLng(44.80736, -0.596572); 

    private CheckBox mServices, mRestauration, mBuildings; 
    private Resources mResources; 
    private GoogleMap mMap; 
    private LocationManager mLocationManager; 
    private Marker mCurrentLocation; 
    private ArrayList<Marker> mServicesMarkers, mRestaurationMarkers, mBuildingsMarkers; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_map); 
     mResources = getResources(); 
     mServices = (CheckBox) findViewById(R.id.services_check); 
     mRestauration = (CheckBox) findViewById(R.id.restauration_check); 
     mBuildings = (CheckBox) findViewById(R.id.buildings_check); 
     setUpLocationServices(); 
    } 

    public void setUpLocationServices() { 
     mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 
     criteria.setAccuracy(Criteria.ACCURACY_FINE); 
     String provider = mLocationManager.getBestProvider(criteria, true); 
     Location location = mLocationManager.getLastKnownLocation(provider); 
     double latitude = location.getLatitude(); 
     double longitude = location.getLongitude(); 
     LatLng currentPosition = new LatLng(latitude, longitude); 

     mCurrentLocation = mMap.addMarker(new MarkerOptions() 
     .position(currentPosition) 
     .title("Votre position") 
     .icon(BitmapDescriptorFactory.fromResource(R.drawable.user_location_marker))); 

     if (location != null) 
      onLocationChanged(location); 
     if (isGpsEnabled()) 
      mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
     else 
      mLocationManager.requestLocationUpdates(provider, UPDATE_FREQUENCY, 0, this); 

    } 

    @Override 
    public void onLocationChanged(Location location) { 
     mCurrentLocation.remove(); 
     LatLng currentPosition = new LatLng(location.getLatitude(), location.getLongitude()); 
     mCurrentLocation = mMap.addMarker(new MarkerOptions() 
     .position(currentPosition) 
     .title("Votre position") 
     .icon(BitmapDescriptorFactory.fromResource(R.drawable.user_location_marker)));  
    } 

    public boolean isGpsEnabled() { 
     LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); 
     return service.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
     if (isGpsEnabled()) 
      mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
     else 
      mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, UPDATE_FREQUENCY, 0, this);    
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
     if (isGpsEnabled()) 
      mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
     else 
      mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, UPDATE_FREQUENCY, 0, this);   
    } 

    @Override 
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
     // TODO Auto-generated method stub 
    } 
} 
+0

沒有關於這個問題的想法? =( – 2013-03-13 09:54:10

回答

0

這個問題似乎是在這裏:

if (isGpsEnabled()) 
     mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
    else 
     mLocationManager.requestLocationUpdates(provider, UPDATE_FREQUENCY, 0, this); 

您運行這段代碼只有一次,在onCreate()。如果您想在GPS啓用後進行位置更新,則需要詢問。在現代設備上,我更願意使用Google's Play Service Location API與熔合位置提供程序。這是我在上次的商業應用程序中實現的方式,只有在未安裝Google Play的情況下才會回到GPS。

另一種選擇,如果你look at this link,在isBetterLocation方法,你可以比較你的當前值和新的值,並選擇兩者中的更好的。這樣,如果你有一個並獲得另一個,你可以決定它是否比以前更好。

相關問題