从网络中读取纬度和经度

时间:2012-07-18 23:46:06

标签: java android latitude-longitude

我使用以下代码获取纬度和经度。依赖网络提供商总是好吗?

public static String getLatitudeLongitudeString() {
    LocationManager lm = (LocationManager) GlobalApplication
            .getAppContext().getSystemService(Context.LOCATION_SERVICE);

    Location location = lm
            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    if (location == null) {
        return "";
    } else {
        Geocoder geocoder = new Geocoder(GlobalApplication.getAppContext());
        try {
            List<Address> user = geocoder.getFromLocation(
                    location.getLatitude(), location.getLongitude(), 1);
            double lat = (double) user.get(0).getLatitude();
            double lng = (double) user.get(0).getLongitude();
            return lat + "," + lng;
        } catch (Exception e) {
            Trace.e("Failed to get latitude and longitude", e);
            return "";
        }
    }
}

2 个答案:

答案 0 :(得分:1)

老实说,它“取决于”。但我会说审查this article并找出最佳选择组合是什么。您可能希望被动提供商(Froyo +)从其他应用程序中捕获一些GPS更新...还取决于您使用它的目的。如果它是一个地图应用程序,那么你想要GPS,但如果它只是为“你在旧金山”类型的文字,那么不太准确就好了......

答案 1 :(得分:0)

这取决于您在用例中提到的Salil。换句话说,它将归结为您需要的位置详细信息的粒度。例如,如果您要编写天气应用程序,那么在大多数情况下了解城市就足够了。如果您正在考虑在用户移动时更新地图,那么您将需要精确的位置,理想情况下由GPS提供商提供。

相关问题