得到的当前位置(GPS /无线) [英] get the current location (gps/wifi)

查看:160
本文介绍了得到的当前位置(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()不块 - 这意味着它会返回无效 - 所以你可能想看看传递的 LocationListener的 到<一个href=\"http://developer.android.com/intl/de/reference/android/location/LocationManager.html#requestLocationUpdates%28java.lang.String,%20long,%20float,%20android.app.PendingIntent%29\"相对=nofollow> 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.

看一看<一个href=\"http://stackoverflow.com/questions/2227292/how-to-get-latitude-and-longitude-of-the-mobiledevice-in-android\">this问题为使用 LocationListener的 的例子。

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆