I keep getting an inaccurate location in Android

对着背影说爱祢 提交于 2019-12-01 06:26:51

Yes there absolutely is, try adding some accuracy to your Criteria...

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);

The default constructor for Criteria states (emphasis mine)

Constructs a new Criteria object. The new object will have no requirements on accuracy, power, or response time; will not require altitude, speed, or bearing; and will not allow monetary cost.

There are a number of things to note here. Firstly GPS can be inaccurate for a number of reasons. One might be adverse weather conditions, another is visibility to a sufficient number of GPS satellites. The phone itself may have a poor GPS receiver. Finally, your phone may not have connected with enough satellites to get an accurate fix.

Specifically in your code you are using the getLastKnownLocation() with an unspecified Criteria, which if I recall correctly, will use any Provider. It is highly likely that the network provider is being used rather than your GPS provider. It can take a fair bit of time to establish a quality connection with a GPS signal so getLastKnownLocation will only work if you sit still for awhile with your GPS actually running (look for the satellite receiver icon in the notification bar). You should implement a specific criteria of GPS and then implement onLocationChanged to do something once the location has been determined.

A silly question. Is your GPS enabled? Does your device offers better precision in other applications (i.e. Goolge Maps) when you are in the same location?

If so, use a Criteria with higher accuracy

criteria.setAccuracy(Criteria.ACCURACY_FINE)

or use

locationManager.getProvider(LocationManager.GPS_PROVIDER)

instead, to force the device to use the GPS, thus obtaining a precision of meters.

Drezden

Try this:

criteria.setAccuracy(Criteria.ACCURACY_FINE);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!