Here's my problem, I'm trying to create a route between 2 waypoints with osm bonus pack, but I got the straight line bug and this debugger error: "E/BONUSPACK: OSRMRoadManager::getRoad: request failed".
I have followed these instructions without success.
Here's my code:
public class DashboardFragment extends Fragment implements View.OnClickListener {
private DashboardViewModel dashboardViewModel;
private static Context context;
MapView map = null;
private MyLocationNewOverlay mLocationOverlay;
Button calcButton;
EditText departure;
EditText arrival;
String startPoint_str;
String endPoint_str;
Geocoder geocoder;
List<Address> addresses;
List<Address> addresses2;
public static Road[] mRoads;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
dashboardViewModel =
ViewModelProviders.of(this).get(DashboardViewModel.class);
View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
final TextView textView = root.findViewById(R.id.text_dashboard);
dashboardViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
createMap(root);
return root;
}
public void createMap(View root) {
Context ctx = getActivity().getApplicationContext();
Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
calcButton = (Button) root.findViewById(R.id.CalcButton);
departure = (EditText) root.findViewById(R.id.StartPointTxt);
arrival = (EditText) root.findViewById(R.id.EndPointTxt);
geocoder = new Geocoder(ctx);
map = root.findViewById(R.id.map);
map.setTileSource(TileSourceFactory.MAPNIK);
map.setMultiTouchControls(true);
IMapController mapController = map.getController();
map.getZoomController().setVisibility(CustomZoomButtonsController.Visibility.SHOW_AND_FADEOUT);
mapController.setZoom(15.0);
GeoPoint startPoint = new GeoPoint(48.0833, -1.6833);
mapController.setCenter(startPoint);
this.mLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(ctx), map);
this.mLocationOverlay.enableMyLocation();
map.getOverlays().add(this.mLocationOverlay);
calcButton.setOnClickListener(this);
}
public void onClick(View v) {
{
startPoint_str = departure.getText().toString();
endPoint_str = arrival.getText().toString();
try {
addresses = geocoder.getFromLocationName(startPoint_str, 1);
addresses2 = geocoder.getFromLocationName(endPoint_str, 1);
} catch (IOException e) {
e.printStackTrace();
}
if(addresses.size() > 0 && addresses2.size() > 0) {
GeoPoint StartPoint = new GeoPoint(addresses.get(0).getLatitude(), addresses.get(0).getLongitude());
GeoPoint EndPoint = new GeoPoint(addresses2.get(0).getLatitude(), addresses2.get(0).getLongitude());
ArrayList<GeoPoint> waypoints = new ArrayList<GeoPoint>();
mRoads = null;
waypoints.add(StartPoint);
waypoints.add(EndPoint);
RoadManager roadManager = new OSRMRoadManager(getActivity());
Marker startMarker = new Marker(map);
startMarker.setPosition(StartPoint);
startMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
map.getOverlays().add(startMarker);
Marker endMarker = new Marker(map);
endMarker.setPosition(EndPoint);
endMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
map.getOverlays().add(endMarker);
new UpdateRoadTask().execute(waypoints);
map.invalidate();
}
}
}
private class UpdateRoadTask extends AsyncTask<Object, Void, Road[]> {
protected Road[] doInBackground(Object... params) {
@SuppressWarnings("unchecked")
ArrayList<GeoPoint> waypoints = (ArrayList<GeoPoint>) params[0];
RoadManager roadManager = new OSRMRoadManager(getActivity());
return roadManager.getRoads(waypoints);
}
@Override
protected void onPostExecute(Road[] roads) {
mRoads = roads;
if (roads == null)
return;
if (roads[0].mStatus == Road.STATUS_TECHNICAL_ISSUE)
Toast.makeText(map.getContext(), "Technical issue when getting the route", Toast.LENGTH_SHORT).show();
else if (roads[0].mStatus > Road.STATUS_TECHNICAL_ISSUE) //functional issues
Toast.makeText(map.getContext(), "No possible route here", Toast.LENGTH_SHORT).show();
Polyline[] mRoadOverlays = new Polyline[roads.length];
List<Overlay> mapOverlays = map.getOverlays();
for (int i = 0; i < roads.length; i++) {
Polyline roadPolyline = RoadManager.buildRoadOverlay(roads[i]);
mRoadOverlays[i] = roadPolyline;
String routeDesc = roads[i].getLengthDurationText(getActivity(), -1);
roadPolyline.setTitle(getString(R.string.app_name) + " - " + routeDesc);
roadPolyline.setInfoWindow(new BasicInfoWindow(org.osmdroid.bonuspack.R.layout.bonuspack_bubble, map));
roadPolyline.setRelatedObject(i);
mapOverlays.add(1, roadPolyline);
}
}
}}
Yes it's ugly, sorry ^^'
The device is connected to internet and the app have all the permission needed.
Here's the request url, I'm doing this request in an AsyncTask.
The problem was that OSRM road manager was sending http requests.
Android Pie only accept https requests.
I solved this by allowing http requests in my app with this method.