2014-03-05 1 views
0

현재 서비스를 제공하고 있습니다. GPS가 활성화되어 있는지 확인합니다. GPS 위치가 표시되고 내지도가 위치를 확대 할 수 있는지 확인합니다. 그것은 getLastKnownLocation입니다. 문제는, getLastKnownLocation 마일 떨어져있을 수도 있습니다 (어제 내가 시도했을 때).GPS를 통해 사용자의 위치를 ​​얻은 다음 네트워크와 Wi-Fi를 연결합니다.

GPS 체크를 먼저 실행합니다. 네트워크 체크가 실행되지 않아 활성화되었습니다.

GPS가 활성화되어 있지만 getLastKnownLcation() 이외의 수정 사항을 얻을 수없는 경우 네트워크 기반 위치가 기본이되도록 할 수있는 방법이 있습니까? 그 후에 네트워크가 활성화되어 있지 않거나 lastKnownLocation이 너무 멀리 떨어져 있으면 Wifi를 확인할 수 있는지 확인합니다. 여기

서비스에 대한 내 코드입니다 : 어제 내가 내 집에서 몇 마일 떨어진 갔다 와이파이 연결을 가지고와 GPS를 사용할 수 있었지만, 무슨 일 것이라고했다 무슨 일이 있었는지
public class GPSTracker extends Service implements LocationListener { 

private final Context mContext; 
boolean isGPSEnabled = false; 
boolean isNetworkEnabled = false; 
boolean canGetLocation = false; 

Location location; 
double latitude; 
double longitude; 

//Minimum distance for update 
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; //10 meters 

// The minimum time between updates in milliseconds 
private static final long MIN_TIME_BW_UPDATES = 1000 * 40; //40 seconds 

protected LocationManager locationManager; 

public GPSTracker(Context context) { 
    this.mContext = context; 
    getLocation(); 
} 

public Location getLocation() { 
    Log.i("i", "Get location called"); 

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

    //Getting GPS status 
    isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

    //Getting network status 
    isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
    this.canGetLocation = true; 
    if (isGPSEnabled) { 
     if (location == null) { 
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
      Log.d("GPS Enabled", "GPS Enabled"); 
      if (locationManager != null) { 
       location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
       if (location != null) { 
        latitude = location.getLatitude(); 
        longitude = location.getLongitude(); 
        Log.i("GPS_LOCATION", "Location: "+location.toString()); 
        if(location.toString().contains("0.000000")) { 
         Log.i("Called", "Called inside"); 
         isNetworkEnabled = true; 
         isGPSEnabled = false; 
         getLocation(); 
        } 
       } 
      } 
     } 
    } 
    else if (isNetworkEnabled) { 
     Log.d("Network", "Network"); 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
     if (locationManager != null) { 
      location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
      if (location != null) { 
       latitude = location.getLatitude(); 
       longitude = location.getLongitude(); 
       Log.i("NETWORK_LOCATION", "Location: "+location.toString()); 
      } 
     } 
    } 
    else if (!isGPSEnabled && !isNetworkEnabled) { 
     // no network provider is enabled and GPS is off. 
     Log.d("NOT ENABLED", "Use WIFI"); 
    } 
    return location; 
} 

나의 있지만, 위치가 Wi-Fi를 통해 업데이트되었으며 파란색 점이지도에 표시되었습니다. 그것을 확대하지 않을 것입니다. GPS가 활성화되었으므로 (수정 사항을 얻을 수 없었기 때문에) 해당 경로를 따라 갔고 LastKnownLocation()이 내 집에 돌아 왔습니다. 파란 점이 맞았음에도 불구하고 마지막 점이 계속 확대되었습니다.

GPS를 확인하지만 LastKnownLocation을 사용하지 않는 방법이 있습니까? 대신 기본적으로 네트워크 검사로 설정 한 다음 네트워크 검사는 lastknownLocation을 사용하지 않으며 기본값은 Wifi입니다. 그러면 Wifi가 필요할 경우 LastKnownLocation을 가질 수 있습니다. 현실적으로 나는 최후의 수단으로 Wi-Fi 단계에서 lastKnownLocation을 얻고 싶습니다.

누군가가이 문제에 도움이되기를 바랍니다. 코드에서 lastKnownLocation을 제거하는 것만 큼 간단하지 않다고 생각합니다.

도움을 주셔서 감사합니다.

답변

1

여기에는 여러 가지 방법이 있습니다. 다음은 몇 가지 팁입니다.

  1. 이전 LastKnownLocation을 사용하지 마십시오. 먼저 나이를 확인하십시오!
  2. 위치는 getProvider()을 통해 제공자에게 제공되며 가장 우선 순위가 높은지 확인한 다음 보조 기관에서 작업합니다.
  3. GPS를 가장 정확하게 사용하십시오.

특히 isBetterLocation 함수 인 Location Strategies을주의 깊게 살펴볼 수 있습니다. 필요에 맞게 조정해야하지만, 이와 같은 것을 원할 것입니다.

protected boolean isBetterLocation(Location location, Location currentBestLocation) { 
    if (currentBestLocation == null) { 
     // A new location is always better than no location 
     return true; 
    } 

    // Check whether the new location fix is newer or older 
    long timeDelta = location.getTime() - currentBestLocation.getTime(); 
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; 
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; 
    boolean isNewer = timeDelta > 0; 

    // If it's been more than two minutes since the current location, use the new location 
    // because the user has likely moved 
    if (isSignificantlyNewer) { 
     return true; 
    // If the new location is more than two minutes older, it must be worse 
    } else if (isSignificantlyOlder) { 
     return false; 
    } 

    // Check whether the new location fix is more or less accurate 
    int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); 
    boolean isLessAccurate = accuracyDelta > 0; 
    boolean isMoreAccurate = accuracyDelta < 0; 
    boolean isSignificantlyLessAccurate = accuracyDelta > 200; 

    // Check if the old and new location are from the same provider 
    boolean isFromSameProvider = isSameProvider(location.getProvider(), 
      currentBestLocation.getProvider()); 

    // Determine location quality using a combination of timeliness and accuracy 
    if (isMoreAccurate) { 
     return true; 
    } else if (isNewer && !isLessAccurate) { 
     return true; 
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { 
     return true; 
    } 
    return false; 
} 

/** Checks whether two providers are the same */ 
private boolean isSameProvider(String provider1, String provider2) { 
    if (provider1 == null) { 
     return provider2 == null; 
    } 
    return provider1.equals(provider2); 
} 
관련 문제