android - 手机上 NETWORK_PROVIDER 的 getLastKnownLocation 返回 null

标签 android location

好吧,我花了很多时间搜索和阅读,而不仅仅是这个网站。大多数线程处理 GPS 或试图让它在模拟器中工作。 developer.android.com 上的文档只解释了一点。我无法让一件简单的事情发挥作用。像大多数遇到此问题的人一样,我只想从网络提供商那里获取最后一个已知位置,但即使在我使用 Google map (使用网络提供商,因为 GPS 已关闭)并向我显示后,它也无法获取仅 1/8 至 1/4 英里远。好的,所以“供应商”(蜂窝塔/wi-fi,如果打开)正在修复谷歌地图。我尝试了我的应用程序,但无法获取最后的已知位置。我什至没有尝试在 map 中显示它,而是让它在按下按钮获取位置后将纬度和经度坐标打印到屏幕上。该文档没有说明必须从同一 Activity 中获取最后一个位置。它说它将返回指定提供商的最后一个已知位置。我正在指定 NETWORK_PROVIDER 文档说它是手机信号塔和 wi-fi。 (是的,我已将其包含在 list 中。)

有人知道这是怎么回事吗?我还没有看到很好的解释,即使在谷歌网站上也是如此。如果你说不出来,我会很沮丧。这显然是我正在做的(或没有做的)事情,所以除了我自己,我不会责怪任何人/任何人,除了对我的需要来说有点太短的文档。

更新:我现在正在使用位置监听器并获取位置,我假设是最后一个位置。在不通过位置监听器启动位置更新的情况下获得最后一个位置似乎是合乎逻辑的,但我不是,现在我使用了位置监听器。

现在我的问题是我无法更改位置。我的代码现在除了尝试通过监听器获取位置更新之外什么都不做,并且还接受按下按钮以获取最后一个已知位置。谷歌地图会显示我所在的位置,但我在代码中一直获取 20 英里外的位置。我有一种感觉,我实际上可能会获得一个新位置,但我可能错误地使用了 final/static 声明,这使我无法在需要时存储和使用该位置。我的 list 具有 internet 和 fine_location 权限,并且该 Activity 的 xml 工作正常 - 它只提供一个按钮来打开/关闭实时搜索位置,一个用于手动检索 lastknownlocation 的按钮,以及一个用于显示纬度和经度的 TextView 。我试图去掉不必要的内容以防止它太长。

public class RMain extends Activity {

public boolean islivesearchon;
public static double netlistentime = 0 * 60 * 1000; // minutes * 60 sec/min * 1000 for milliseconds
public static double netlistendistance = 0 * 1609.344; // miles * conversion to meters
public static double gpslistentime = 30 * 60 * 1000; // minutes * 60 sec/min * 1000 for milliseconds
public static double gpslistendistance = 0 * 1609.344; // miles * conversion to meters

private static Location currentlocation;
private LocationManager locationManager;
private LocationListener locationListener;
public static Criteria criteria;
public TextView latview;
public TextView longview;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rmain);

    latview = (TextView) findViewById(R.id.display_latitude);
    longview = (TextView) findViewById(R.id.display_longitude);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    // setup bestProvider
    criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String bestprovider = locationManager.getBestProvider(criteria, true);

    // get an initial current location
    currentlocation = locationManager.getLastKnownLocation(bestprovider);
    latview.setText(Double.toString(currentlocation.getLatitude()));
    longview.setText(Double.toString(currentlocation.getLongitude()));

    // Define locationListener interface
    locationListener = new LocListener();

    // React to LiveSearch toggle button press
    final Button livesearch = (Button) findViewById(R.id.button_livesearch);
    livesearch.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            TextView livesearchview = (TextView) findViewById(R.id.button_livesearch);
            boolean isNetEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            if(islivesearchon) {
                locationManager.removeUpdates(locationListener);
                islivesearchon = false;
                livesearchview.setText("Live Search is OFF");
            }
            else {
                // ideally should check for !isProviderEnabled first, then provide option for turning it on
                if(isNetEnabled) {
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, (long) netlistentime, (float) netlistendistance, locationListener);
                    islivesearchon = true;
                    livesearchview.setText("Live Search is ON");
                }
                // ideally should check for !isProviderEnabled first, then provide option for turning it on
                if(isGpsEnabled) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, (long) gpslistentime, (float) gpslistendistance, locationListener);
                    islivesearchon = true;
                    livesearchview.setText("Live Search is ON");
                }
            }
        }
    });

    // Respond to Get Location button click to get last known location
    final Button getlocation = (Button) findViewById(R.id.button_getlocation);
    getlocation.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            String bestprovider = locationManager.getBestProvider(criteria, true);
            Location lastlocation = locationManager.getLastKnownLocation(bestprovider);

            latview.setText(Double.toString(lastlocation.getLatitude()));
            longview.setText(Double.toString(lastlocation.getLongitude()));
        }
    });

}

private class LocListener implements LocationListener {

    @Override
    public void onLocationChanged(Location latestlocation) {
      // Called when a new location is found by the network location provider.
        currentlocation.set(latestlocation);
        latview.setText(Double.toString(currentlocation.getLatitude()));
        longview.setText(Double.toString(currentlocation.getLongitude()));
    }

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

    @Override
    public void onProviderEnabled(String provider) {}

    @Override
    public void onProviderDisabled(String provider) {}
};

最佳答案

尝试检查一下...一旦您的 onLocationChange 被调用,之后您可以使用最后一个已知位置获取旧值。

 locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);

        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {

            old_latitude=location.getLatitude();
            old_longitude=location.getLongitude();
            Log.d("old","lat :  "+old_latitude);
            Log.d("old","long :  "+old_longitude);

            this.onLocationChanged(location);
        }   

关于android - 手机上 NETWORK_PROVIDER 的 getLastKnownLocation 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13814928/

相关文章:

android - Gradle 实验性 JNI OpenCv arm 64-v8a 链接器错误

android - 如何在android中重命名文件?

android - 为什么缺少 checkSelfPermission 方法?

iphone - 停止返回缓存的 CLLocationManager 位置

javascript - 您如何使用 javascript(基于接近度)对经纬度点进行排序或切换显示

android - 位置服务 onProviderEnabled 从未调用过

android - 如何在我使用 Google map 从当前位置开始移动时绘制路径

java - Android Firebase : get all object inside child?

android - 获取到一个点的最短距离

php - 是否将 json 转换为 xml