本文介绍了得到的当前位置(GPS /无线)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用它像这样把我的位置:

I'm trying to get my location using it like this:

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

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);

String provider = myLocationManager.getBestProvider(criteria,true);

if (provider != null) {
    //there is a provider
    myLocationManager.requestLocationUpdates(provider, 10L, 500.0f, (LocationListener) mainContext);
    Location myCurLocation = myLocationManager.getLastKnownLocation(provider);
            //here i'm trying to get some data from the
            //myCurLocation but
            //myCurLocation == NULL
}

但myCurLocation总是== NULL

but the myCurLocation is always == NULL

我在做什么错了?

推荐答案

调用getLastKnownLocation()不块 - 这意味着它会返回无效 - 所以你可能想看看传递的到 requestLocationUpdates()方法相反,这会给你的位置的异步更新。

The call to getLastKnownLocation() doesn't block - which means it will return null if no position is currently available - so you probably want to have a look at passing a LocationListener to the requestLocationUpdates() method instead, which will give you asynchronous updates of your location.

看一看this问题为使用 LocationListener的 的例子。

Have a look at this question for an example of using a LocationListener.

这篇关于得到的当前位置(GPS /无线)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 04:22