Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

android get location from best provider available

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);

and then the listener

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
Provider: "+providershown);
    accuracy.setBackgroundColor(Color.GREEN);
    // }
    userinfo=usernamevalue+"&"+Double.toString(latid)+"&"+Double.toString(longid);
    submituserlocation(userinfo);
   }
}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...