2014-10-01 85 views
-4

我需要在Android上获取用户的当前位置,我看过一些方法,但他们都似乎矫枉过正。我已经在应用上实施了Google地图,有没有简单的方法来获取用户的当前位置?什么是最简单的方式来获取用户当前位置Android

+0

为 – 2014-10-01 09:21:11

+1

使用的LocationManager类,你可以发布一个例子请:之后,我用这个类有什么更好的位置检查?我试过但没有成功 – Signo 2014-10-01 09:22:35

回答

0

请阅读谷歌有关位置策略。我有一个应用程序与用户当前的位置,我也用这样的方式,它的工作非常好:

杉杉我得到的位置,这个类:

// Acquire a reference to the system Location Manager 
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

// Define a listener that responds to location updates 
LocationListener locationListener = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     // Called when a new location is found by the network location provider. 
     makeUseOfNewLocation(location); 
    } 

    public void onStatusChanged(String provider, int status, Bundle extras) {} 

    public void onProviderEnabled(String provider) {} 

    public void onProviderDisabled(String provider) {} 
    }; 

// Register the listener with the Location Manager to receive location updates 
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 

我创建了两个方法用于接收位置:第一个是NETWORK_PROVIDER,第二个是GPS。

private static final int TWO_MINUTES = 1000 * 60 * 2; 

/** 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) { 
    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; 
} 
+0

谢谢!我发现了一个非常类似于此的解决方案,我认为这是正确的答案 – Signo 2014-10-01 10:07:31

+0

我很高兴如果我帮助你:) – MilanNz 2014-10-01 10:22:02

相关问题