开发者

Problem in getting the Latitude and Longitude value using gps at different location

I used the following code to find the longitude and latitude for my android application

public double[] getlocation() {
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = lm.getProviders(true);

    Location l = null;
    for (int i = 0; i < providers.size(); i++) {
        l = lm.getLastKnownLocation(providers.get(i));
        if (l != null)
            break;
    }
    double[] gps = new double[2];

    if (l != null) {
        gps[0] = l.getLatitude();
        gps[1] = l.getLongitude();
    }

    return gps;
}

I got the accurate or exact latitude and longitude first time 开发者_Go百科when i run the application after i tried to get the new co-ordinates from one miles away from my first location but i got the same latitude and longitude.

so please can you suggest me how this problem occurs. what is the solution for this ?


.getLastKnownLocation() will be inaccurate. You need to use a LocationListener to get accurate and up to date location updates. See this link. You generally ONLY want to use getLastKnownLocation ONCE upon starting to get a quick fix, then register a LocationListener to get more accurate, constant updates.

Also be aware that sitting in your house attempting to test will not work very well. You might need to go outside and walk around some.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜