Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Gps android-获取纬度和经度_Java_Android_Geolocation - Fatal编程技术网

Java Gps android-获取纬度和经度

Java Gps android-获取纬度和经度,java,android,geolocation,Java,Android,Geolocation,你能帮我一下吗。。当我添加地理编码器以使用纬度和经度获取城市名称时,应用程序开始崩溃 double latitude, longitude; private TextView lala; LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); Location location = lm.getLastKnownLocation(LocationMana

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

    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);
双纬度、经度;
私人文本视图拉拉;
LocationManager lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
Location Location=lm.getLastKnownLocation(LocationManager.GPS\U提供程序);
longitude=location.getLongitude();
纬度=位置。getLatitude();
final LocationListener LocationListener=新LocationListener(){
已更改位置上的公共无效(位置){
longitude=location.getLongitude();
纬度=位置。getLatitude();
}
公共无效onProviderDisabled(字符串arg0){
//TODO自动生成的方法存根
}
已启用公共void onProviderEnabled(字符串arg0){
//TODO自动生成的方法存根
}
状态已更改的公共void(字符串arg0、整数arg1、捆绑包arg2){
//TODO自动生成的方法存根
}
};
lm.RequestLocationUpdate(LocationManager.GPS_提供程序,2000,10,locationListener);
lala=(TextView)findViewById(R.id.textView1);
Geocoder myLocation=新的地理编码器(getApplicationContext(),Locale.getDefault());
列表myList=null;
试一试{
myList=myLocation.getFromLocation(纬度,经度,1);
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
lala.setText((字符序列)myList);

lm.getLastKnownLocation
可以返回null,然后尝试
get经度()
您将得到
NullPointerException

lala.setText((CharSequence)myList)
也可以这样做(抛出异常),因为“我的列表在这里可以为空(如果
Geocoder
失败)

编辑 要处理地理编码器中的异常,请考虑以下检查表

  • 确保您的纬度和经度不为空

  • 确保-90
    lm.getLastKnownLocation
    可以返回null,然后尝试
    get经度()
    您将得到
    NullPointerException

    lala.setText((CharSequence)myList)
    也可以这样做(抛出异常),因为“我的列表在这里可以为空(如果
    Geocoder
    失败)

    编辑 要处理地理编码器中的异常,请考虑以下检查表

  • 确保您的纬度和经度不为空

  • 确保你是对的。。但是你能告诉我怎么做才能让Geocoder不失败。。对不起,我是android和java的初学者..好的。。现在我有了纬度:59.412。。经度:24.689。。我的网络连接工作正常。。也许我的代码有问题是的,我又发现了一个错误-你正在将列表强制转换为无法完成的CharSequence。看我的第二次编辑。好的……你是我的救世主。。谢谢你说得对。。但是你能告诉我怎么做才能让Geocoder不失败。。对不起,我是android和java的初学者..好的。。现在我有了纬度:59.412。。经度:24.689。。我的网络连接工作正常。。也许我的代码有问题是的,我又发现了一个错误-你正在将列表强制转换为无法完成的CharSequence。看我的第二次编辑。好的……你是我的救世主。。非常感谢。
    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();
        }
    }
    
    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);