开发者

Location manager does not detect location correctly even we are moving miles away in android

In my application I have to fetch the current location. First it correctly fetches the location. But we are moving from the current location to some other location. It shows the previous address. It is not updating the location. My code is:

locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
locListener = new MyLocationListener();  
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locListener);
location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null)                                
{
    latitude = location.getLatitude();
longitude = location.getLongitude();
}  


//My location listener is as follows

public void onLocationChanged(Location loc) {
    if (loc != null) {
        //Toast.makeText(this.getBaseContext(), 
        //"Location changed : Lat: " + loc.getLatitude() + 
        //" Lng: " + loc.getLongitude(), 
    开发者_如何学运维    //Toast.LENGTH_LONG).show();
        double lat = loc.getLatitude();
        double lon = loc.getLongitude();
    }
}

public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
}

public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
}

public void onStatusChanged(String provider, int status, 
    Bundle extras) {
    // TODO Auto-generated method stub
}

Please find the mistake and when i launch first time it is not working on second time itself it is working....so check this thing also.....


Try:

locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000,2,locListener);

This tells it to try and report back every 2 seconds if it has moved at least 2 meters since last time. The 0's may be treated differently.

Perhaps your phone's GPS is having issues (some phone are notorious for this). Does Google Maps have any problems?

In your code you can also call [myLocationManager.getBestProvider(new Criteria(), false)][1]

This will return something other than the GPS if it's having problems. It might be that location provided by the network is more accurate.

[1]: http://developer.android.com/reference/android/location/LocationManager.html#getBestProvider(android.location.Criteria, boolean)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜