当用户长时间点击 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/