从可用的最佳供应商Android开位置 [英] android get location from best provider available
问题描述
我有这个code,以获得最佳的供应商
I have this code to get the best available provider
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider(criteria, true);
Location mostRecentLocation = lm.getLastKnownLocation(provider);
if(mostRecentLocation != null) {
latid=mostRecentLocation.getLatitude();
longid=mostRecentLocation.getLongitude();
}
lm.requestLocationUpdates(provider, 1, 0, locationListener);
,然后听众
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
if (loc != null) {
latid = loc.getLatitude();
longid = loc.getLongitude();
// if(loc.hasAccuracy()==true){
accuracyd = loc.getAccuracy();
String providershown = loc.getProvider();
accuracy.setText("Location Acquired. Accuracy:"
+ Double.toString(accuracyd) + "m\nProvider: "+providershown);
accuracy.setBackgroundColor(Color.GREEN);
// }
userinfo=usernamevalue+"&"+Double.toString(latid)+"&"+Double.toString(longid);
submituserlocation(userinfo);
}
}
在我测试的设备(HTC Magic的),我发现,当GPS被禁用它从网络上立即锁定。当启用了GPS它不采取从网络上的任何数据,并等待,直到它从GPS锁定。
我想锁定像谷歌的位置映射的,直到他们有一个良好的GPS信号,他们利用网络来确定我的位置。照片
不过,我觉得最好的标准将做到这一点,但他们做的是选择一个供应商一次。
是不是有什么毛病我code或我必须做的线程和超时等来做到这一点?
When I tested it to a device(htc magic) I found out that when gps is disabled it locks from the network immediately. When I enable the gps it doesnt take any data from the network and waits till it locks from the gps.
I would like to lock the position like the google maps that until they have a good gps signal they use the network to determine my location.
I though the best criteria would do that but what they do is pick a provider once.
Is there something wrong with my code or I have to do threads and timeouts etc to make it happen?
推荐答案
也许你可以尝试听这两个网络供应商和全球定位系统提供了一定的时间,然后从两个检查结果。如果没有来自GPS的结果,使用网络,而不是结果。这就是我做的。
Maybe you can try listening to both the network provider and gps provider for a certain amount of time and then check the results from the two. If you don't have results from the gps, use the network results instead. That's how I did it.
这篇关于从可用的最佳供应商Android开位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!