Android中获取纬度和经度的正确方法

发布于 2024-09-17 17:59:54 字数 107 浏览 7 评论 0原文

我需要以编程方式在 Android 中获取纬度和经度的正确方法。我查看了不同的网站和论坛,但仍然找不到正确的网站和论坛。该程序应支持所有 Android 版本。我使用 wifi 将我的设备连接到网络。

I need the correct way of getting latitude and longitude in Android programmatically. I viewed different sites and forums but still I couldn't get the correct one. The program should support for all the Android versions. I use wifi to get my device connected to net.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稀香 2024-09-24 17:59:54

下面的实现创建了一个位置监听器,它将更新的值记录为字符串,以方便提取。通过使用 LocationManager,您可以抽象底层方法(GPS/辅助 GPS、wifi、手机信号塔)来检索位置。

首先初始化:

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
final myLocationListner ll = new myLocationListner();

LocationListener:

public class myLocationListner implements LocationListener {

        public static String lattitude = "";
    public static String longitude = "";

    @Override
    public void onLocationChanged(Location loc) {
        String temp_lattitude = String.valueOf(loc.getLatitude());
        String temp_longitude = String.valueOf(loc.getLongitude());

        if(!("").equals(temp_lattitude))lattitude = temp_lattitude;
        if(!("").equals(temp_longitude))longitude = temp_longitude;
    }

    @Override
    public void onProviderDisabled(String arg0) {

    }

    @Override
    public void onProviderEnabled(String arg0) {

    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {

    }

}

Below implementation creates a locationlistener that records the updated values as strings for convenience which can be easily extracted. By using the LocationManager you abstract the underlying method (GPS/assissted-GPS, wifi, cell-tower) to retrieve the location.

Initialize first:

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
final myLocationListner ll = new myLocationListner();

LocationListener:

public class myLocationListner implements LocationListener {

        public static String lattitude = "";
    public static String longitude = "";

    @Override
    public void onLocationChanged(Location loc) {
        String temp_lattitude = String.valueOf(loc.getLatitude());
        String temp_longitude = String.valueOf(loc.getLongitude());

        if(!("").equals(temp_lattitude))lattitude = temp_lattitude;
        if(!("").equals(temp_longitude))longitude = temp_longitude;
    }

    @Override
    public void onProviderDisabled(String arg0) {

    }

    @Override
    public void onProviderEnabled(String arg0) {

    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {

    }

}
尐偏执 2024-09-24 17:59:54

别担心,在绞尽脑汁几天之后,我用几行代码来获取经度和纬度值......我认为这会帮助事情变得更好,感谢您的建议!!!!

private double[] getGPS() {
LocationManager lm = (LocationManager) 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.length();i>=0;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;}

Dont Worry, After scratching my head over few days, I got code working in over a few lines to get longitude and latitude values... I think this will help things go better, Thanks for your suggestions!!!!

private double[] getGPS() {
LocationManager lm = (LocationManager) 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.length();i>=0;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;}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文