Android谷歌地图V2位置更新GPS?网络?

时间:2013-08-08 09:38:22

标签: android google-maps-android-api-2

使用Samsung S3和Android Google Map V2(使用" LocationClient"获取位置)。它的功能完全正常,除了

  1. 室内,第一次更新位置(不是来自LocationManager的getLastLocation())非常非常慢,甚至没有更新。 (GPS打开,wifi关闭)

  2. 室内,第一次更新地点时,开启WIFI(甚至没有连接任何热点)。

  3. 我按照下面的教程进行了第一次位置更新(不是来自LocationManager的getLastLocation())速度在室外时很好。

    (谷歌教程) https://developers.google.com/maps/documentation/android/locationhttp://developer.android.com/training/location/receive-location-updates.html


     

    根据谷歌此处https://developers.google.com/maps/documentation/android/start,它说

    android.permission.ACCESS_COARSE_LOCATION 允许API使用WiFi或移动单元格数据(或两者)来确定设备的位置。

    android.permission.ACCESS_FINE_LOCATION 允许API使用全球定位系统(GPS)确定设备在非常小的区域内的位置。

    因此,我认为它应该从NETWORK和GPS更新位置。

    ========================

    但我得到的是

    如果只打开GPS,它(LocationClient)似乎只能收听GPS信号。如果您想从NETWORK更新位置,您还必须打开WIFI。

    这是对的吗?

    感谢...

    ========================

    更新

    根据Google Maps Android API v2 creating a new LocationSource

    现在,我不使用" LocationClient"并设置另一个" LocationSource"到谷歌地图。现在谷歌地图即使在室内也可以非常快速地接收位置,并且无需开启WIFI。

1 个答案:

答案 0 :(得分:2)

这是我用于locationlistener的内容,使用与您相同的权限。

private void startLocationListener() {

    userPosition.setPosition(myPosition);

    // Get the location manager
    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            // Called when a new location is found by the network location provider.


            if(currLocation != null)
            {
                boolean better = isBetterLocation(location, currLocation);
                if(better)
                {

                    currLocation.set(location);
                    myPosition = new LatLng(currLocation.getLatitude(), currLocation.getLongitude());
                    userPosition.setPosition(myPosition);
                }
            }
            else
            {
                currLocation = location;
                myPosition = new LatLng(currLocation.getLatitude(), currLocation.getLongitude());
                userPosition.setPosition(myPosition);
            }

        }

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

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {}
    };


    locationManager.requestLocationUpdates(GPSPROVIDER, 5000, 0, locationListener);
    locationManager.requestLocationUpdates(NETPROVIDER, 30000, 0, locationListener);

}

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 > TIME;
    boolean isSignificantlyOlder = timeDelta < -TIME;
    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;
}