java - 将位置数据从 fragment 传递到 Android 中的 Activity

标签 java android google-maps android-fragments

当用户长时间点击 mapFragment 时,我想将有关点击位置的数据发送到另一个 Activity ,即。我想将纬度、经度、城市、邮政编码、国家...传递给 LocationDetailsActivity。

我成功地通过了纬度和经度测试,但我遇到了其他所有问题。我是编码和 Android 的初学者,所以我使用分步示例,到目前为止我找不到任何分步示例。

代码:

    if (myLocation != null) {
        latitude = myLocation.getLatitude();
        longitude = myLocation.getLongitude();
    }

    mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {

        @Override
        public void onMapLongClick(LatLng arg0) {

            Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
            try {
                List<Address> allAddresses = geocoder.getFromLocation(arg0.latitude, arg0.longitude, 1);
                  if (allAddresses.size() > 0) {
                    Address address = allAddresses.get(0);
                      String addressline = allAddresses.get(0).getAddressLine(0);
                      String city = allAddresses.get(0).getLocality();
                      String state = allAddresses.get(0).getAdminArea();
                      String country = allAddresses.get(0).getCountryName();
                      String postalCode = allAddresses.get(0).getPostalCode();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }


            Intent intent = new Intent(getActivity(), LocationDetailsActivity.class);
            intent.putExtra("latitude", arg0.latitude);
            intent.putExtra("longitude", arg0.longitude);
            intent.putExtra("city",);
            intent.putExtra("zip",);
            intent.putExtra("country",);
            startActivity(intent);

        }
    });

感谢任何帮助:)

最佳答案

试试这个:

mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {

        @Override
        public void onMapLongClick(LatLng arg0) {

            Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
            try {
                List<Address> allAddresses = geocoder.getFromLocation(arg0.latitude, arg0.longitude, 1);
                  if (allAddresses.size() > 0) {
                    Address address = allAddresses.get(0);

                     Intent intent = new Intent(getActivity(), LocationDetailsActivity.class);
                     intent.putExtra("latitude", arg0.latitude);
                     intent.putExtra("longitude", arg0.longitude);
                     intent.putExtra("city", allAddresses.get(0).getLocality());
                     intent.putExtra("zip", allAddresses.get(0).getPostalCode());
                     intent.putExtra("state", allAddresses.get(0).getAdminArea());
                     intent.putExtra("country", allAddresses.get(0).getCountryName());
                     startActivity(intent);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

关于java - 将位置数据从 fragment 传递到 Android 中的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38372335/

相关文章:

java - 如何在启动时使用 cxf 将基本授权添加到 wsdl?

java - Swing 自定义边框

安卓 GCM onMessage

android - android 4.4 上的自定义字体渲染有空心透明文本

angularjs - Angular 谷歌地图搜索框指令: events don't fire when trying to restrict the search (with autocomplete option)

javascript - 如何识别 map 上的谷歌地图标记

使用正则表达式解析字符串时的 Java 8 lambda 表达式回调类型

java - 放大 Canvas 中的点,所有内容都基于某个点

android - AsyncTask 一开始挂起

android - 我的应用程序中的谷歌地图应用程序在该位置上有一个标签