gpt4 book ai didi

java - 如何将文本设置为来自另一个类的 Google map 当前位置

转载 作者:行者123 更新时间:2023-11-30 04:59:16 27 4
gpt4 key购买 nike

我创建了一个单独的 Activity 来创建 Google map 并在用户当前位置设置标记。在另一个 fragment 类中,我想将 TextView 的文本设置为 map Activity 中的位置。

CheckInFragment

mLocation = (TextView) v.findViewById(R.id.checkin_location_text);
mLocation.setText();

MapsActivity

private void handleNewLocation(Location location){
//SET THESE TWO VARIABLES TO THE TEXT vv
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();

LatLng latLng = new LatLng(currentLatitude, currentLongitude);

MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("Check In Location");

mMap.addMarker(options);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
float zoomLevel = 16.0f;
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoomLevel));

}

最佳答案

你可以试试下面的代码来获取位置的字符串值:

Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> addressList = geocoder.getFromLocation(location.getLatitude(),
location.getLongitude(), 1);

String fullAddress = "";

if (addressList != null && addressList.size() > 0) {

if (addressList.get(0).getAddressLine(0) != null) {
fullAddress += addressList.get(0).getAddressLine(0) + " ";
}
if (addressList.get(0).getSubAdminArea() != null) {
fullAddress += addressList.get(0).getSubAdminArea() + " ";
}

} else {
Log.d("Address:", "Couldn't find Address");
}
} catch (IOException e) {
e.printStackTrace();
}

然后您可以在调用您的 fragment 类时在包中传递 fullAddress 字符串值,例如:

YourFragment fragment = new YourFragment();
Bundle args = new Bundle();
args.putString("location_string", fullAddress);
fragment.setArguments(args);

最后在 Fragment 类的 onCreate 方法中获取如下字符串值:

if (getArguments() != null)
{
_location = getArguments().getString("location_string");
}

希望这有帮助!!!

关于java - 如何将文本设置为来自另一个类的 Google map 当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58518287/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com