2012-04-26 1 views
4

은 내가 다음 코드이 적응하려고 ...하지만 난 내가 isBetterLocation(Location location, Location currentBestLocation)location 및 currentBestLocation은 어디에서 제공됩니까? 안드로이드 개발자 가이드 (얻기 사용자 위치)에

에 투입해야하는 위치 값 모르는 , 안드로이드 개발자 가이 드에서 튜토리얼에 대한 Obtaining User Location를 읽고

Example.class

 private LocationManager locman; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

      String context = Context.LOCATION_SERVICE; 
      locman = (LocationManager)getSystemService(context); 

      Criteria criteria = new Criteria(); 
      criteria.setAccuracy(Criteria.ACCURACY_FINE); 
      criteria.setAltitudeRequired(false); 
      criteria.setBearingRequired(false); 
      criteria.setPowerRequirement(Criteria.POWER_LOW); 

      String provider = locman.getBestProvider(criteria, true); 
      locman.requestLocationUpdates(
        provider,MIN_TIME, MIN_DISTANCE, locationListener); 

     } 

     private LocationListener locationListener = new LocationListener(){ 

     @Override 
     public void onLocationChanged(Location location) { 
      // What should i pass as first and second parameter in this method 
      if(isBetterLocation(location1,location2)){ 
       // isBetterLocation = true > do updateLocation 
       updateLocation(location); 
      } 
     } 

     @Override 
     public void onProviderDisabled(String provider) {} 
     @Override 
     public void onProviderEnabled(String provider) {} 
     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) {} 

     }; 

    protected boolean isBetterLocation(Location location, Location currentBestLocation) { 
     if (currentBestLocation == null) { 
      // A new location is always better than no location 
      return true; 
     } 
      //Brief ... See code in Android Dev Guid "Obtaining User Location" 
    } 

답변

7

아니고 어려운 정말. 코드는 발견 된 위치에 대한 지속적인 업데이트를받습니다. 여러 청취자가 서로 다른 청취자를 청취하도록 할 수 있습니다. 따라서 그러한 청취자는 제공 업체 (예 : GPS는 네트워크보다 정확할 수 있음)에 따라 다소 정확할 수 있습니다. isBetterLocation(...)은 청취자가 찾은 위치가 실제로 이미 알고있는 위치보다 나은지 (그리고 코드에서 참조가 있어야하는지)를 평가합니다. isBetterLocation (...) 코드는 잘 설명되어 있으므로 이해하기 어렵지는 않지만 첫 번째 매개 변수 의 위치는이고 공급자는 새로운 위치이며 currentBestLocation은 이미 알고있는 위치입니다.

내가 사용하는 코드는 가장 좋은 제공자를 사용하는 것 외에는 귀하의 코드와 거의 같습니다. 계속 업데이트를 원하지 않기 때문에 처리기에 문제가 있습니다. 최대 시간대 인 2 분 내에 정확한 위치 인 GPS를 찾을 수 있습니다.

private Location currentBestLocation = null; 
private ServiceLocationListener gpsLocationListener; 
private ServiceLocationListener networkLocationListener; 
private ServiceLocationListener passiveLocationListener; 
private LocationManager locationManager; 

private Handler handler = new Handler(); 


public void fetchLocation() { 
    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

    try { 
     LocationProvider gpsProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER); 
     LocationProvider networkProvider = locationManager.getProvider(LocationManager.NETWORK_PROVIDER); 
     LocationProvider passiveProvider = locationManager.getProvider(LocationManager.PASSIVE_PROVIDER); 

     //Figure out if we have a location somewhere that we can use as a current best location 
     if(gpsProvider != null) { 
      Location lastKnownGPSLocation = locationManager.getLastKnownLocation(gpsProvider.getName()); 
      if(isBetterLocation(lastKnownGPSLocation, currentBestLocation)) 
       currentBestLocation = lastKnownGPSLocation; 
     } 

     if(networkProvider != null) { 
      Location lastKnownNetworkLocation = locationManager.getLastKnownLocation(networkProvider.getName()); 
      if(isBetterLocation(lastKnownNetworkLocation, currentBestLocation)) 
       currentBestLocation = lastKnownNetworkLocation; 
     } 

     if(passiveProvider != null) { 
      Location lastKnownPassiveLocation = locationManager.getLastKnownLocation(passiveProvider.getName()); 
      if(isBetterLocation(lastKnownPassiveLocation, currentBestLocation)) { 
       currentBestLocation = lastKnownPassiveLocation; 
      } 
     } 

     gpsLocationListener = new ServiceLocationListener(); 
     networkLocationListener = new ServiceLocationListener(); 
     passiveLocationListener = new ServiceLocationListener(); 

     if(gpsProvider != null) { 
      locationManager.requestLocationUpdates(gpsProvider.getName(), 0l, 0.0f, gpsLocationListener); 
     } 

     if(networkProvider != null) { 
      locationManager.requestLocationUpdates(networkProvider.getName(), 0l, 0.0f, networkLocationListener); 
     } 

     if(passiveProvider != null) { 
      locationManager.requestLocationUpdates(passiveProvider.getName(), 0l, 0.0f, passiveLocationListener); 
     } 

     if(gpsProvider != null || networkProvider != null || passiveProvider != null) { 
      handler.postDelayed(timerRunnable, 2 * 60 * 1000); 
     } else { 
      handler.post(timerRunnable); 
     } 
    } catch (SecurityException se) { 
     finish(); 
    } 
} 

private class ServiceLocationListener implements android.location.LocationListener { 

    @Override 
    public void onLocationChanged(Location newLocation) { 
     synchronized (this) { 
      if(isBetterLocation(newLocation, currentBestLocation)) { 
       currentBestLocation = newLocation; 

       if(currentBestLocation.hasAccuracy() && currentBestLocation.getAccuracy() <= 100) { 
        finish(); 
       } 
      } 
     } 
    } 

    @Override 
    public void onStatusChanged(String s, int i, Bundle bundle) {} 

    @Override 
    public void onProviderEnabled(String s) {} 

    @Override 
    public void onProviderDisabled(String s) {} 
} 

private synchronized void finish() { 
    handler.removeCallbacks(timerRunnable); 
    handler.post(timerRunnable); 
} 

/** Determines whether one Location reading is better than the current Location fix 
* @param location The new Location that you want to evaluate 
* @param currentBestLocation The current Location fix, to which you want to compare the new one 
*/ 
protected boolean isBetterLocation(Location location, Location currentBestLocation) { 
    //etc 
} 

private Runnable timerRunnable = new Runnable() { 

    @Override 
    public void run() { 
     Intent intent = new Intent(LocationService.this.getPackageName() + ".action.LOCATION_FOUND"); 

     if(currentBestLocation != null) { 
      intent.putExtra(LocationManager.KEY_LOCATION_CHANGED, currentBestLocation); 

      locationManager.removeUpdates(gpsLocationListener); 
      locationManager.removeUpdates(networkLocationListener); 
      locationManager.removeUpdates(passiveLocationListener); 
     } 
    } 
}; 
+0

내 코드에 예제를 표시 하시겠습니까? 여러 청취자에 대해. 나는 여전히 어떤 위치 값을 메소드 isBetterlocation()에 전달해야하는지와 currentBestLocation을 설정할 때를 혼동했다. –

+0

코드 예제를 위해 편집했다. – MrJre

+0

고맙습니다. –