java - Gps android - 获取纬度和经度

标签 java android geolocation

你能帮我吗..当我添加地理编码器以使用纬度和经度获取城市名称时,应用程序开始崩溃..

    double latitude, longitude;
    private TextView lala;

    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    longitude = location.getLongitude();
    latitude = location.getLatitude();
    final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            longitude = location.getLongitude();
            latitude = location.getLatitude();
        }

        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub

        }

        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub

        }

        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        }
    };

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
    lala = (TextView) findViewById(R.id.textView1);

    Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());   
    List<Address> myList = null;
    try {
        myList = myLocation.getFromLocation(latitude, longitude, 1);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }


    lala.setText((CharSequence) myList);

最佳答案

lm.getLastKnownLocation 可以返回 null,然后尝试 getLongitude() 你会得到 NullPointerException

还有 lala.setText((CharSequence) myList); 可以做同样的事情(抛出异常)因为这里的 myList 可以为空(如果 Geocoder 失败)。

编辑 要处理 Geocoder 中的异常,请考虑以下 list

  1. 确保您的纬度和经度不为空
  2. 确保 -90 <= latitude <= 90 和 -180 <= longitude <= 180
  3. 确保您有可用的网络连接

编辑2 改进代码

private TextView lala;

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if(location != null) {
    showMyAddress(location);
}

final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        showMyAddress(location);
    }

    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }

    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub

    }

    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }
};

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
lala = (TextView) findViewById(R.id.textView1);



// Also declare a private method

private void showMyAddress(Location location) {
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());   
    List<Address> myList;
    try {
        myList = myLocation.getFromLocation(latitude, longitude, 1);
        if(myList.size() == 1) {
            lala.setText(myList.get(0).toString());             
        }
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

关于java - Gps android - 获取纬度和经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6100967/

相关文章:

java - 获取文件资源的静态方式

Java ISBN 格式

java - 对于 Android 来说,一个对象向另一个对象发送消息的最佳方法是什么?

android - 如何知道Android的电子邮件客户端是否已安装?

android - 以编程方式在Android手机中查找互联网IP地址,不需要设备IP地址

react-native - navigator.geolocation.getCurrentPosition 在使用 React Native 的 iOS 模拟器中未触发

Eclipse/Ant下的Java编译器默认为%JAVA_HOME%?

java - java中如何保存图像文件?

javascript - 从网络浏览器获取 GPS 位置

c# - 如何使用纬度和经度计算距离和方位?