Java MapBox如何根据用户更改位置在fragment textView中动态传递用户坐标

Java MapBox如何根据用户更改位置在fragment textView中动态传递用户坐标,java,android,textview,fragment,mapbox,Java,Android,Textview,Fragment,Mapbox,我正在片段中使用mapbox贴图。我设法把一些代码放在一个片段中打开一个地图并找到用户的位置。我还编写了一个toast,它显示用户的经度和纬度。这可能是另一个问题,因为toast只显示纬度。然而,当我尝试创建自动更新的textView时,我的斗争就开始了,我的方法可以在下面的代码中看到。如果有人能建议代码应该如何更新,或者建议一种绕过它的方法,我们将不胜感激 private View root; private MapView mapView; private TextV

我正在片段中使用mapbox贴图。我设法把一些代码放在一个片段中打开一个地图并找到用户的位置。我还编写了一个toast,它显示用户的经度和纬度。这可能是另一个问题,因为toast只显示纬度。然而,当我尝试创建自动更新的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中添加新的位置字符串,如下所示: 新纬度:%1$s新经度:%2$s

有关String.format的详细信息,请参见:

关于textview:在获取最后位置后,应该在onCreateView中膨胀布局并在OnMaReady中更新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;
    }
4月1日,在你的祝酒词之前,加上下面的一行

textView.setText(String.format(fragment.getString(R.string.new_location),
                        String.valueOf(result.getLastLocation().getLatitude()),
                        String.valueOf(result.getLastLocation().getLongitude()));

您好,斯旺科德,谢谢您的回复,敬酒信息现在运行良好。我还有一个问题,关于如何在onMapReady上更新textView?我已经编辑了答案,检查一下,如果不起作用,发布layout.xml文件谢谢!它将带坐标的字符串传递给textView,并显示在屏幕上。但是,它只在应用程序打开后更新字符串,我想知道是否可以根据用户更改位置来更新字符串?您应该从实现LocationEngineCallback的回调内部更新textview。请参阅Mapbox中的此示例: