Java 我想从特定地址分别获取经度和纬度

Java 我想从特定地址分别获取经度和纬度,java,android,location,Java,Android,Location,我遵循安卓地理编码的一个例子来获取地址教程中的经纬度,它起作用了,但我想分别获取经纬度坐标,但无法 这是我的密码: public class MainActivity extends AppCompatActivity { Button addressButton; TextView addressTV; TextView latLongTV; static TextView txtLatitude; TextView txtLongitude;

我遵循安卓地理编码的一个例子来获取地址教程中的经纬度,它起作用了,但我想分别获取经纬度坐标,但无法

这是我的密码:

public class MainActivity extends AppCompatActivity {
    Button addressButton;
    TextView addressTV;
    TextView latLongTV;
    static TextView txtLatitude;
    TextView txtLongitude;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        addressTV = (TextView) findViewById(R.id.addressTV);
        latLongTV = (TextView) findViewById(R.id.latLongTV);
     txtLatitude = (TextView) findViewById(R.id.txtLatitude);
       txtLatitude= (TextView) findViewById(R.id.txtLongitude);

        addressButton = (Button) findViewById(R.id.addressButton);
        addressButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                EditText editText = (EditText) findViewById(R.id.addressET);
                String address = editText.getText().toString();

                GeocodingLocation locationAddress = new GeocodingLocation();
                locationAddress.getAddressFromLocation(address,
                        getApplicationContext(), new GeocoderHandler());
            }
        });

    }

    private class GeocoderHandler extends Handler {
        @Override
        public void handleMessage(Message message) {
            String locationAddress,latitude ,longitude ;

            switch (message.what) {
                case 1:
                    Bundle bundle = message.getData();
                    locationAddress = bundle.getString("address");
                    latitude = bundle.getString("latitude");
                    longitude = bundle.getString("longitude");
                    break;
                default:
                    locationAddress = null;
                    latitude = null;
                    longitude= null;
            }
            latLongTV.setText(locationAddress);
          txtLatitude.setText(latitude);
//        txtLongitude.setText(longitude);
        }
    }

    private static class GeocodingLocation {


            private static final String TAG = "GeocodingLocation";

            public void getAddressFromLocation(final String locationAddress,
                                               final Context context, final Handler handler) {
                Thread thread = new Thread() {
                    @Override
                    public void run() {
                        Geocoder geocoder = new Geocoder(context, Locale.getDefault());
                        String result = null;
                        String strLatitude = null, strLongitude = null;
                        try {
                            List<Address> addressList = geocoder.getFromLocationName(locationAddress, 1);
                            if (addressList != null && addressList.size() > 0) {
                                Address address = addressList.get(0);
                                double latitude = address.getLatitude();
                                double longitude = address.getLongitude();
                                StringBuilder sb = new StringBuilder();
                                sb.append(address.getLatitude()).append("\n");
                                sb.append(address.getLongitude()).append("\n");
                                result = sb.toString();
                               strLatitude = String.valueOf(latitude);
                               strLongitude= String.valueOf(longitude);
                            }
                        } catch (IOException e) {
                            Log.e(TAG, "Unable to connect to Geocoder", e);
                        } finally {
                            Message message = Message.obtain();
                            message.setTarget(handler);
                            if (result != null) {
                                message.what = 1;
                                Bundle bundle = new Bundle();
                                result = "Address: " + locationAddress +
                                        "\n\nLatitude and Longitude :\n" + result;
                                bundle.putString("address", result);
                                bundle.putString("Latitude", strLatitude);
                                bundle.putString("Longitude", strLongitude);
                                message.setData(bundle);
                            } else {
                                message.what = 1;
                                Bundle bundle = new Bundle();
                                result = "Address: " + locationAddress +
                                        "\n Unable to get Latitude and Longitude for this address location.";
                                bundle.putString("address", result);
                                message.setData(bundle);
                            }
                            message.sendToTarget();
                        }
                    }
                };
                thread.start();
            }
        }
public类MainActivity扩展了AppCompatActivity{
按钮地址按钮;
文本视图地址电视;
TextView LATTV;
静态文本视图txtLatitude;
文本视图TXT;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addressTV=(TextView)findViewById(R.id.addressTV);
latLongTV=(TextView)findViewById(R.id.latLongTV);
txtLatitude=(TextView)findViewById(R.id.txtLatitude);
txtLatitude=(TextView)findViewById(R.id.txtLatitude);
addressButton=(按钮)findViewById(R.id.addressButton);
addressButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
EditText EditText=(EditText)findViewById(R.id.addressET);
字符串地址=editText.getText().toString();
GeocodingLocation locationAddress=新的GeocodingLocation();
locationAddress.getAddressFromLocation(地址,
getApplicationContext(),新的GeocoderHandler());
}
});
}
私有类GeocoderHandler扩展处理程序{
@凌驾
公共无效handleMessage(消息消息){
字符串位置地址、纬度、经度;
开关(消息。什么){
案例1:
Bundle=message.getData();
locationAddress=bundle.getString(“地址”);
纬度=bundle.getString(“纬度”);
经度=bundle.getString(“经度”);
打破
违约:
locationAddress=null;
纬度=零;
经度=零;
}
latLongTV.setText(位置地址);
txtLatitude.setText(纬度);
//setText(经度);
}
}
私有静态类地理编码定位{
私有静态最终字符串TAG=“GeocodingLocation”;
public void getAddressFromLocation(最终字符串位置地址,
最终上下文,最终处理程序){
线程线程=新线程(){
@凌驾
公开募捐{
Geocoder Geocoder=新的地理编码器(上下文,Locale.getDefault());
字符串结果=null;
字符串strLatitude=null,strLongitude=null;
试一试{
List addressList=geocoder.getFromLocationName(locationAddress,1);
if(addressList!=null&&addressList.size()>0){
地址=地址列表。获取(0);
双纬度=地址。getLatitude();
double longitude=address.getLongitude();
StringBuilder sb=新的StringBuilder();
sb.append(address.getLatitude()).append(“\n”);
sb.append(address.getLongitude()).append(“\n”);
结果=sb.toString();
strLatitude=字符串.valueOf(纬度);
strlongitute=字符串.valueOf(经度);
}
}捕获(IOE异常){
Log.e(标签“无法连接到地理编码器”,e);
}最后{
Message=Message.get();
message.setTarget(处理程序);
如果(结果!=null){
message.what=1;
Bundle=新Bundle();
结果=“地址:”+位置地址+
“\n\n纬度和经度:\n”+结果;
bundle.putString(“地址”,结果);
bundle.putString(“纬度”,strlatude);
普通话串(“经度”,标准长度);
message.setData(bundle);
}否则{
message.what=1;
Bundle=新Bundle();
结果=“地址:”+位置地址+
“\n无法获取此地址位置的纬度和经度。”;
bundle.putString(“地址”,结果);
message.setData(bundle);
}
message.sendToTarget();
}
}
};
thread.start();
}
}
根据,捆绑密钥区分大小写。您可以使用以下设置字符串:

bundle.putString("Latitude", strLatitude);
bundle.putString("Longitude", strLongitude);
但是,你可以通过以下方式获得:

latitude = bundle.getString("latitude");
longitude = bundle.getString("longitude");
请注意,您使用大写字母L放置,但使用小写字母L获取。根据,捆绑键区分大小写。您可以使用以下字符设置字符串:

bundle.putString("Latitude", strLatitude);
bundle.putString("Longitude", strLongitude);
但是,你可以通过以下方式获得:

latitude = bundle.getString("latitude");
longitude = bundle.getString("longitude");

请注意,您使用大写字母
L
,但使用小写字母
L

它到底是如何不起作用的?它是否崩溃了,如果是,在哪里?您得到的是无效值、零值还是根本没有值?请给我们一个提示。它到底是如何不起作用的?它崩溃了吗?如果是,在哪里?您在哪里