Home > Net >  How to create and start a navigation route in a Java fragment using Mapbox?
How to create and start a navigation route in a Java fragment using Mapbox?

Time:06-26

Good Day, so, for my university project, I'm working on an Uber Eats app of sorts, and currently, I am trying to send in an origin point and a destination point, and then the Mapbox Navigation Drop-In UI will pop up with its turn by turn directions until the destination has been reached or the user closes the app. Now, the problem is that most of the documentation for the new Mapbox versions is in Kotlin, and all the tutorials I find online are using the old versions of Mapbox, which doesn't work on API versions 31 and above. When I try following those tutorials, I get the error below:

Targeting S  (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.

So now this leads me to ask the community for help, as I've tried quite a lot of stuff, and haven't managed to figure it out

Firstly, here are the implementations that I am using for my map fragment

    implementation 'com.mapbox.maps:android:10.5.0'
    implementation "com.mapbox.navigation:ui-dropin:2.6.0-alpha.2"
    implementation 'com.squareup.retrofit2:retrofit:2.7.2'

and here is my XML file for the fragment

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_marginBottom="69dp"
    tools:context=".Volunteer_Map_Fragment">

    <com.mapbox.navigation.dropin.NavigationView
        android:id="@ id/navigationView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:accessToken="@string/mapbox_access_token" />

</FrameLayout>

And this is what I currently have in my onCreateView() function in the fragment Java file:


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_volunteer__map_, container, false);
    
        // Points
        Point origin = Point.fromLngLat(28.075418, -26.1640314);
        Point destination = Point.fromLngLat(28.02479119803452, -26.1888766);
    
        NavigationView navigationView = view.findViewById(R.id.navigationView);
    
        // Now set the origin and destination and request a route
    
        return view;
    }

I would greatly appreciate it if anyone could help lead me in the right direction for creating and starting a route using the origin and destination points

Thank you.

Here is my whole map fragment code if needed:

package com.voltero;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.mapbox.geojson.Point;
import com.mapbox.navigation.dropin.NavigationView;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link Volunteer_Map_Fragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class Volunteer_Map_Fragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public Volunteer_Map_Fragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Volunteer_Map_Fragment.
     */
    // TODO: Rename and change types and number of parameters
    public static Volunteer_Map_Fragment newInstance(String param1, String param2) {
        Volunteer_Map_Fragment fragment = new Volunteer_Map_Fragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_volunteer__map_, container, false);

        // Points
        Point origin = Point.fromLngLat(28.075418, -26.1640314);
        Point destination = Point.fromLngLat(28.02479119803452, -26.1888766);

        NavigationView navigationView = view.findViewById(R.id.navigationView);

        // Now set the origin and destination and request a route

        return view;
    }

}

CodePudding user response:

You're using Drop-In UI to start navigation given a set of points. This capability to start Drop-In UI directly in ActiveGuidance mode is not yet supported by Mapbox. However, we are working on it. If your interest is in using Drop-In UI, you would have to start it from Free Drive mode and long press to select a destination. Example to integrate Drop-In UI can be found here https://github.com/mapbox/mapbox-navigation-android-examples/blob/main/app-preview[…]/navigation/examples/preview/dropinui/NavigationViewActivity.kt. This is a very simple code and need not have any Java implementations. However, if you want to really be able to specify an origin/destination pair you would have to use standalone components. Example can be found here https://github.com/mapbox/mapbox-navigation-android-examples/blob/main/app/src/mai[…]/navigation/examples/turnbyturn/TurnByTurnExperienceActivity.kt.

  • Related