需要当前的位置更好的办法 [英] need a BETTER way for current location

查看:294
本文介绍了需要当前的位置更好的办法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过几乎所有的问题/答案在这里和在网络上,对当前位置的最佳方法如下:

Through nearly all problems/answers here and on the web, the best way for current location is as follows:

if ( mLocationManager==null )
    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

Location l;
for ( String provider : mLocationManager.getAllProviders() ){
    mLocationManager.requestLocationUpdates(provider, 1000, 1, locationListener);
    l = mLocationManager.getLastKnownLocation(provider);
    if ( isBetterLocation(l, currentBestLocation) )
    currentBestLocation = l;
}

通常它工作正常的模拟器和设备,但正如一些人指出,Android 4.0以上版本这个code给出的例外

09-06 22:46:16.163: ERROR/AndroidRuntime(351): Caused by: java.lang.IllegalArgumentException: provider=network
09-06 22:46:16.163: ERROR/AndroidRuntime(351):     at android.os.Parcel.readException(Parcel.java:1325)
09-06 22:46:16.163: ERROR/AndroidRuntime(351):     at android.os.Parcel.readException(Parcel.java:1275)
09-06 22:46:16.163: ERROR/AndroidRuntime(351):     at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:646)
09-06 22:46:16.163: ERROR/AndroidRuntime(351):     at android.location.LocationManager._requestLocationUpdates(LocationManager.java:582)
09-06 22:46:16.163: ERROR/AndroidRuntime(351):     at android.location.LocationManager.requestLocationUpdates(LocationManager.java:446)

但在同一设备上的其他应用程序可以正常工作时,只有WIFI / 3G网络覆盖(GPS不能在室内工作)。

But other apps on the same device can work properly when there IS only WIFI/3G coverage(GPS not working indoors).

所以我的问题是:
如何在建筑物内检索当前位置?

So my question is: How to retrieve current location inside a building?

PS:等待Android的图像更新是不能接受的,因为其他应用程序在同一设备上正常工作。必须有一种方式来获得的LocationManager与错误了Android图像上的当前位置。

PS: waiting for an Android image update is not acceptable, since other apps work properly on the same device. There MUST be a way to get the current location on the Android image with bug in LocationManager.

最亲切的问候。

编辑:

这一次我调整我的code尽可能简单(在onResume):

This time I tweaked my code as simple as possible(in the onResume):

    if ( mLocationManager==null )
        mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    for ( String provider : mLocationManager.getAllProviders() )
        mLocationManager.requestLocationUpdates(provider, 0, 0, locationListener);

和在locationListener.onLocationChanged:

and in the locationListener.onLocationChanged:

public void onLocationChanged(Location location) {
    if (isBetterLocation(location, currentBestLocation)) {
        currentBestLocation = location;
        ...make use of this location
    }
}

只有被动 GPS 供应商可供选择,没有网络可以在我的XT928获得供应商,而其他手机上显示出来

Only passive and gps providers are available, no network provider can be obtained on my XT928, while on the other phone it shows up.

编辑2
最后我通过转向一个名为百度位置库的第三方库,它也需要访问密钥的应用来实现这一点。它的伟大工程。

EDIT 2: at last I achieved this by turning to a third-party library named as Baidu Location Library which also needs Access Key application. It works great.

推荐答案

你为什么叫 mLocationManager.requestLocationUpdates(供应商,1000,1,LocationListener的); mLocationManager.getLastKnownLocation(供应商); ?据我所知, requestLocationUpdates 将注册一个监听器,将让通知您手机的位置。 getLastKnownLocation()会给你提供程序的最后已知​​位置。于是两人是完成不同的事情。

Why do you call mLocationManager.requestLocationUpdates(provider, 1000, 1, locationListener); before mLocationManager.getLastKnownLocation(provider);? As far as I know, requestLocationUpdates will register a listener that will keep notifying you of the phone's location. getLastKnownLocation() will give you the last known location for the provider. So the two are to accomplish different things.

如果你只是想在当前位置的一个实例,甩掉 requestLocationUpdates的()。然后,你需要检查,如果上面按你的code目前最好的位置是不够好。如果不是,您可以稍后(以外的for循环)请求单个更新: locationManager.requestSingleUpdate(标准,singleUpdatePI);

If you just want one instance of the current location, get rid of requestLocationUpdates(). Then, you need to check if the current best location as per your code above is good enough. If it is not, you can request a single update later (outside your for loop): locationManager.requestSingleUpdate(criteria, singleUpdatePI);

另外请注意,你只能从供应商拿到的位置,如果用户已授权这样的设置。在Android中4+,位置信息访问权限设置明确要求授权为:

Also note that you can only get location from the providers if the user has authorized so in the settings. In Android 4+, "Location access" settings explicitly request for authorization to:

让应用使用谷歌的定位服务来估计自己的位置
  速度更快。

Let apps use Google's location service to estimate your location faster.

这篇关于需要当前的位置更好的办法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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