2011-10-10 90 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; 
     }