【问题标题】:I want to fetch Longitude and Latitude separately from a particular address我想从特定地址分别获取经度和纬度
【发布时间】:2017-05-23 07:08:39
【问题描述】:

我按照 Android Geocoding 中的一个示例获取地址教程的纬度经度,它有效,但我想分别获取经度和纬度坐标但不能。

这是我的代码:

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

【问题讨论】:

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

标签: java android location


【解决方案1】:

根据this answer,捆绑键区分大小写。您使用以下方式设置字符串:

bundle.putString("Latitude", strLatitude);
bundle.putString("Longitude", strLongitude);

但是然后得到它们:

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

注意,L 是大写字母,l 是小写字母。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    相关资源
    最近更新 更多