GPS android - 获取纬度和经度

发布于 2024-11-09 09:50:28 字数 1489 浏览 0 评论 0原文

你能帮我解决这个问题吗?当我添加地理编码器以使用纬度和经度获取城市名称时,应用程序开始崩溃。

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

Can you help me with that.. application started to crash when I added the geocoder to get the name of the city by using latitude and longitude..

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

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

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

发布评论

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

评论(2

懒的傷心 2024-11-16 09:50:28

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

另外 lala.setText(( CharSequence) myList); 可以做同样的事情(抛出异常),因为'myList 在这里可以为 null(如果 Geocoder 失败)。

编辑
要处理地理编码器中的异常,请考虑以下清单

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

EDIT2
改进的代码

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

lm.getLastKnownLocation can return null, and then tou try to getLongitude() you'll get NullPointerException

Also lala.setText((CharSequence) myList); can do the same (throw Exception) cause' myList can be null here (if Geocoder is failed).

EDIT
To deal with exception in Geocoder consider the following checklist

  1. Make sure your latitude and longitude are not null
  2. Make sure what -90 <= latitude <= 90 and -180 <= longitude <= 180
  3. Make sure you are having available network connection

EDIT2
Improved code

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();
    }
}
她说她爱他 2024-11-16 09:50:28
Criteria criteria = new Criteria();
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
int la = (int) (location.getLatitude() * 1E6);
int lo = (int) (location.getLongitude() * 1E6);
Criteria criteria = new Criteria();
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
int la = (int) (location.getLatitude() * 1E6);
int lo = (int) (location.getLongitude() * 1E6);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文