开发者

Abt retrieving the latitude and longitude of the device

开发者 https://www.devze.com 2023-01-07 19:42 出处:网络
I want to retrieve the latitude and longitude of the use开发者_JAVA技巧r\'s present location, I already tried using location observer which fires event based upon the given input distance and time,Hen

I want to retrieve the latitude and longitude of the use开发者_JAVA技巧r's present location, I already tried using location observer which fires event based upon the given input distance and time,Hence it triggers the event to identify the location based upon the given time duration and distance.

And what I want is method, to get the location, just by calling that method. When ever I call that method, it should notify me of the latitude and longitude.

Is there any methods in android?


/**
 * This is a fast code to get the last known location of the phone. If there
 * is no exact gps-information it falls back to the network-based location
 * info. This code is using LocationManager. Code from:
 * http://www.androidsnippets.org/snippets/21/
 * 
 * @param ctx
 * @return
 */
public static Location getLocation(Context ctx) {
    LocationManager lm = (LocationManager) ctx
            .getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = lm.getProviders(true);

    /*
     * Loop over the array backwards, and if you get an accurate location,
     * then break out the loop
     */
    Location l = null;

    for (int i = providers.size() - 1; i >= 0; i--) {
        l = lm.getLastKnownLocation(providers.get(i));
        if (l != null)
            break;
    }
    return l;
}
0

精彩评论

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