gpt4 book ai didi

android - 有一个获取位置的代码 fragment ,不想考虑网络位置

转载 作者:行者123 更新时间:2023-11-30 02:35:18 25 4
gpt4 key购买 nike

我是 Android 的新手,我从 stackoverflow 中获取了一个代码 fragment 来获取位置。它工作完美,但是当网络 internet 被激活时,它从那里获取坐标,并且它们不是很准确,或者根本不准确(有时会出现 5 公里以上的错误)。

我希望我的位置代码获取 fragment 仅获取 GPS 位置,但我目前的知识不允许我修改它以便我可以获得它。

这是代码 fragment :

package com.fourbox.bocterapp;

/**
* Created by Soulstorm on 10/14/2014.
*/
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class MyLocation {
Timer timer1;
LocationManager lm;
LocationResult locationResult;
boolean gps_enabled=false;
boolean network_enabled=false;

public boolean getLocation(Context context, LocationResult result)
{
//I use LocationResult callback class to pass location value from MyLocation to user code.
locationResult=result;
if(lm==null)
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

//exceptions will be thrown if provider is not permitted.
try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}

//don't start listeners if no provider is enabled
if(!gps_enabled && !network_enabled)
return false;

if(gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
if(network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
timer1=new Timer();
timer1.schedule(new GetLastLocation(), 50000);
return true;
}

LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};

LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};

class GetLastLocation extends TimerTask {
@Override
public void run() {
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
Location net_loc=null, gps_loc=null;
if(gps_enabled)
gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(network_enabled)
net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

//if there are both values use the latest one
if(gps_loc!=null && net_loc!=null){
if(gps_loc.getTime()>net_loc.getTime())
locationResult.gotLocation(gps_loc);
else
locationResult.gotLocation(net_loc);
return;
}

if(gps_loc!=null){
locationResult.gotLocation(gps_loc);
return;
}
if(net_loc!=null){
locationResult.gotLocation(net_loc);
return;
}
locationResult.gotLocation(null);
}
}

public static abstract class LocationResult{
public abstract void gotLocation(Location location);

}
}

下面是我的使用方法:

MyLocation.LocationResult locationResult = new MyLocation.LocationResult() {
@Override
public void gotLocation(Location location) {
currentUserLatitude = location.getLatitude();
currentUserLongitude = location.getLongitude();
new RemoteDataTask().execute();
}
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);

你能帮我修改我的代码,让我的代码 fragment 不获取网络坐标,只获取 GPS 坐标吗?

提前致谢!尊重。

最佳答案

GetLastLocation 类中的以下两行是您通过网络位置提供商订阅位置更新的地方。

只需删除这两行代码即可不再接收来自网络位置提供商的更新。

if(network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);

请注意,来自 GPS 的位置更新可能需要比网络更长的时间。因此,如果速度是一个问题,另一种方法是仍然使用网络位置更新,但过滤精度值。这看起来像这样:

LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {

if (location.getAccuracy() > THRESHOLD) {
return;
}

timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};

关于android - 有一个获取位置的代码 fragment ,不想考虑网络位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26671530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com