【问题标题】:correct way of getting latitude and longitude in Android在Android中获取纬度和经度的正确方法
【发布时间】:2010-09-03 12:55:32
【问题描述】:

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

【问题讨论】:

    标签: android geolocation gps location


    【解决方案1】:

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

    先初始化:

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

    位置监听器:

    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) {
    
        }
    
    }
    

    【讨论】:

    【解决方案2】:

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

    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;}
    

    【讨论】:

      猜你喜欢
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多