【发布时间】:2016-01-05 06:19:57
【问题描述】:
我正在使用以下代码来获取用户的位置。
LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
这会返回一个android.location.Location 对象,我可以成功获取纬度和经度,但是当我尝试获取模式时,它给了我“融合”。
而我想知道获取位置的方式是使用 GPS 还是网络。
【问题讨论】:
我正在使用以下代码来获取用户的位置。
LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
这会返回一个android.location.Location 对象,我可以成功获取纬度和经度,但是当我尝试获取模式时,它给了我“融合”。
而我想知道获取位置的方式是使用 GPS 还是网络。
【问题讨论】:
如果您想检查 gps 和网络提供商,请使用以下代码。我认为使用融合 api 我们无法区分它
public Location getLocation() {
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
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();
}
}
}
if(isGPSEnabled) {
if(location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if(locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
【讨论】:
您可以确定使用的准确性。如果精度高,则来自 GPS。
【讨论】: