2011-10-10 78 views
0

我使用下面的代碼,以確定當前位置時,接近警戒火災:如何獲得當前位置時,接近警戒火災

Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
if (location == null) { 
    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
    if (location == null) { 
     // We'll just write an empty location 
     location = new Location(""); 
    } 
} 

當我看着我找回了位置,我得到進入警報在我不應該得到他們的地方。由於接近警報在內部輪詢GPS和網絡提供商 - 因此更新了LastKnownLocation - 此代碼將產生當前位置,所以我之前感覺不到這種印象。這個假設是否正確?

回答

1

我正在使用以下代碼來確定當前位置及其工作。檢出分類代碼...

 LocationManager locationManager; 
     String context = Context.LOCATION_SERVICE; 
     locationManager = (LocationManager) getSystemService(context); 
     Criteria criteria = new Criteria(); 
     criteria.setAccuracy(Criteria.ACCURACY_FINE); 
     criteria.setAltitudeRequired(false); 
     criteria.setBearingRequired(false); 
     criteria.setCostAllowed(true); 
     criteria.setPowerRequirement(Criteria.POWER_LOW); 
     String provider = locationManager.getBestProvider(criteria, true); 

     if (null != provider) 
     { 
      Location location = locationManager.getLastKnownLocation(provider); 
      if (null != location) { 
       GpsFound=true; 
       currentLat = location.getLatitude(); 
       currentLon = location.getLongitude(); 
       System.out.println("Current Lat,Long :"+currentLat+" ,"+currentLon); 
      } 
      else 
      { 
       //GpsFound=false; 
       gpsMsg="Current Location can not be resolved!"; 
      } 

     } 
     else 
     { 
      gpsMsg="Provider is not available!"; 
      //GpsFound=false; 
     }