简体   繁体   中英

MapBox How to pass the user coordinates in fragment textView dynamically depending on user changing location

I am using mapbox map in a fragment. I managed to put some code that opens a map in a fragment and finds user location. I also wrote a toast that displays the user longitude and latitude (this might be another problem because toast only displays latitude). However, my struggle starts when I try to create textView which will update automatically, my approach can be seen in the code below. If anyone could suggest how the code should be updated or suggest an approach to go around it, it would be greatly appreciated.

    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() should have a format text as the first param, followed by the arguments referenced by the format specifiers in the format string. Try something like this:

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

add new_location string in your R.strings.xml like this: <string name="new_location">New latitude: %1$s New longitude: %2$s</string>

more info about String.format() is in here: https://developer.android.com/reference/java/util/Formatter#format(java.lang.String,%20java.lang.Object...)

About the textview: it should be declared after inflating the layout in onCreateView() and update textView in onMapReady(),after you get the lastLocation onCreateView should be:

@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;
    }

and onMapReady(), before your toast, add the below line

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM