我正在片段中使用mapbox地图。我设法放置了一些代码,以便在一个片段中打开地图并找到用户位置。我还编写了一个烤面包,显示用户的经度和纬度(这可能是另一个问题,因为烤面包仅显示纬度)。但是,当我尝试创建将自动更新的textView时,我的奋斗开始了,我的方法可以在下面的代码中看到。如果有人可以建议应如何更新代码或提出解决方法,将不胜感激。
private View root;
private MapView mapView;
private TextView textView;
private MapboxMap map;
private PermissionsManager permissionsManager;
private LocationEngine locationEngine;
private LocationLayerPlugin locationLayerPlugin;
private Location originLocation;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView (LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// Below I try to write something that will update my textView
textView = (TextView) (root).findViewById(R.id.longitude_display_text);
textView.setText("this is where I try to pass longitude of a user");
root = inflater.inflate(R.layout.second_page, container, false);
mapView =(MapView) (root).findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
return root;
}
@Override
public void onMapReady(MapboxMap mapboxMap) {
map = mapboxMap;
enableLocation();
locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
locationEngine.setInterval(50000);
locationEngine.activate();
Location lastLocation = locationEngine.getLastLocation();
//Toast below only gives latitude , not sure if this because I should format number to give less digits,
//I tried formatting the number to give less digits but it did not work
Toast.makeText(requireContext(), String.format(
String.valueOf(locationEngine.getLastLocation().getLatitude()) , String.valueOf(locationEngine.getLastLocation().getLongitude())),
Toast.LENGTH_LONG).show();
}
String.format()应该以格式文本作为第一个参数,后跟格式字符串中格式说明符引用的参数。试试这样的东西:
Toast.makeText(fragment.getContext(), String.format(fragment.getString(R.string.new_location),
String.valueOf(result.getLastLocation().getLatitude()),
String.valueOf(result.getLastLocation().getLongitude())),
Toast.LENGTH_SHORT).show();
在R.strings.xml中添加new_location字符串,如下所示:<string name="new_location">New latitude: %1$s New longitude: %2$s</string>
有关String.format()的更多信息在这里:https://developer.android.com/reference/java/util/Formatter#format(java.lang.String,%20java.lang.Object...)
关于textview:在获得lastLocation之后,应在onCreateView()中扩大布局并在onMapReady()中更新textView之后声明它。onCreateView应该是:
@Nullable
@Override
public View onCreateView (LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
root = inflater.inflate(R.layout.second_page, container, false);
mapView =(MapView) (root).findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
// Below I try to write something that will update my textView
textView = (TextView) (root).findViewById(R.id.longitude_display_text);
textView.setText("this is where I try to pass longitude of a user");
return root;
}
和onMapReady(),在您的吐司之前,添加以下行
textView.setText(String.format(fragment.getString(R.string.new_location),
String.valueOf(result.getLastLocation().getLatitude()),
String.valueOf(result.getLastLocation().getLongitude()));