在Android中将位置数据从片段传递到活动

时间:2016-07-14 10:55:15

标签: java android google-maps android-fragments

当用户长时间点击mapFragment时,我想将有关点击位置的数据发送到另一个活动,即。我想将lat,long,city,zip code,country ...传递给LocationDetailsActivity。

我成功地通过了lat而且很长,但我被其他一切困住了。我是编码和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);

        }
    });

感谢任何帮助:)

3 个答案:

答案 0 :(得分:2)

您可以在startActivity内移动longClickListener代码,也可以全局定义所有变量,以便在clickListener

之外使用它们
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();


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


        }

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


}
});

答案 1 :(得分:2)

试试这个:

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

答案 2 :(得分:2)

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) {
                moveToLoginActivity(address,arg0.latitude,arg0.longitude); 
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
});


function void moveToLoginActivity(Address address, float latitude, float longitude){

    Address address = allAddresses.get(0);
    addressline = allAddresses.get(0).getAddressLine(0);
    String city = allAddresses.get(0).getLocality();
    String  = allAddresses.get(0).getAdminArea();
    String country = allAddresses.get(0).getCountryName();
    String postalCode = allAddresses.get(0).getPostalCode();


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