【问题标题】:Android Google Map V2 location update GPS? NETWORK?Android 谷歌地图 V2 位置更新 GPS?网络?
【发布时间】:2013-08-09 23:12:59
【问题描述】:

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

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

  2. 室内,开WIFI(甚至没有连接任何热点)第一次位置更新速度还可以。

我按照下面的教程进行了第一次位置更新(不是来自 LocationManager 的 getLastLocation())速度,如果在户外的话就可以了。

(谷歌教程) https://developers.google.com/maps/documentation/android/location 和 http://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”设置为 Google Map。现在谷歌地图即使在室内并且没有 WIFI 开启也可以接收到非常快速更新的位置。

【问题讨论】:

  • 你可能没看过这篇文章:developer.android.com/guide/topics/location/…
  • @ss1271 这已经过时了。 Google Maps Android API v2 使用 Google I/O 2013 上宣布的新 LocationClient API。
  • @MaciejGórski 感谢您提供的信息,我会检查一下 =)

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


【解决方案1】:

这是我用于 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;
}

【讨论】:

  • 谢谢,现在我不使用“LocationClient”转而将另一个位置源(新的位置源来自android Location manager、GPS_Provider 和NETWORK_PROVIDER)设置为Google 地图。现在即使在室内没有 GPS 信号和 WIFI 关闭,位置更新也很快。
  • @andrewwang1TW 嘿,你能在 GPS_PROVIDER 部分帮助我吗:stackoverflow.com/questions/20112140/…
猜你喜欢
  • 1970-01-01
  • 2012-03-24
  • 2012-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-01
  • 1970-01-01
相关资源
最近更新 更多