Routing between two points in Mapbox (Android)
You've got your lat/long order mixed up whenever you use Point.fromLngLat()
.
For example, dstPosition = Point.fromLngLat(dstLat, dstLng);
is the wrong order.
Is there a log message in your logcat when you try the code you posted above? Search for Mbgl
. Is response.body()
null? Is the route size == 0? Basically, it'd be helpful if you could explain more when you say I am unable to create a route between them
.
By the way, you don't have to use the Mapbox Navigation SDK for Android if you want to get a route between two points. The Mapbox Java SDK can do that.
- https://docs.mapbox.com/android/java/overview/directions
- https://docs.mapbox.com/android/java/examples/show-directions-on-a-map
- https://docs.mapbox.com/android/java/examples/dashed-directions-line
Also, I'd move the onMapReady()
stuff into the onStyleLoaded()
callback.
@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
map = mapboxMap;
originPosition = Point.fromLngLat(originLng, originLat);
dstPosition = Point.fromLngLat(dstLng, dstLat);
MarkerOptions options = new MarkerOptions();
options.title("Source");
options.position(originLatLng);
MarkerOptions options1 = new MarkerOptions();
options1.title("Destination");
options1.position(dstLatLng);
mapboxMap.addMarker(options);
mapboxMap.addMarker(options1);
navigationMapRoute = new NavigationMapRoute(null, mapView, map, R.style.NavigationMapRoute);
getRoute(originPosition, dstPosition);
}
});
}
Routing between points with MapBox
Yes, you could use another engine for routing and just plot it as an RMAnnotation
with an RMShape
in the MapBox SDK.
How to plot an optimized route between multiple markers in MapBox Android Studio
You don't make a loop and call getRoute()
multiple times. You should call getRoute()
once and pass it all of the points. You set the origin and destination as you have done (in this case destination
should be the last point in the list). All the intermediate points are set by calling addWaypoint()
.
If you want to return to the start point then you should set the destination
= origin
.
Mapbox draw direction via two markers
If I understand correctly, you wish to use Mapbox's direction and routing layer to get the walking route between the two points and display it. To do so you need to set the origin and destination points and call the query()
function of direction
. You also need to add a routes control to the map. The revised code is as follows.
// example origin and destination
var start = {lat: 22.3077423, lng: 114.2287582};
var finish = {lat: 22.3131334, lng: 114.2205973};
var map = L.mapbox.map('map', 'mapbox.streets', {
zoomControl: false }).setView([start.lat, start.lng], 14);
map.attributionControl.setPosition('bottomleft');
var directions = L.mapbox.directions({
profile: 'mapbox.walking'
});
// Set the origin and destination for the direction and call the routing service
directions.setOrigin(L.latLng(start.lat, start.lng));
directions.setDestination(L.latLng(finish.lat, finish.lng));
directions.query();
var directionsLayer = L.mapbox.directions.layer(directions).addTo(map);
var directionsRoutesControl = L.mapbox.directions.routesControl('routes', directions)
.addTo(map);
You may not need to add the origin / destination markers yourself, as origin / destination markers would be displayed as part of the directions control.
Related Topics
How to Make Playground Execution Time Is as Fast as If We Run in iOS Application
Uibezierpath Subclass Initializer
Extending a Delegate from a Base Class
Uitableviewcell Initwithstyle:Uitableviewcellstylesubtitle Is Not Working
Uibezierpath + Cashapelayer - Animate a Circle Filling Up
iOS App Crashes on Phone But Works Fine on Simulator
Sorting Struct Array in Swift 4
How to Authorize Twitter with Swifter
Modifing One Variable from Another View Controller Swift
How to Deal with Dynamic Sections and Rows in iOS Uitableview
Storyboard Localization in Swift 4.0
Creating a Rtsp Client for Live Audio and Video Broadcasting in Objective C
iOS 10.3 - How to Change App Icon Programmatically
Iad Interstitials Not Showing Consistently? and Not at All on the Simulator
How to Make Designable Textfield Code Class in Swift
Nsexceptionallowsinsecurehttploads Not Working for Ip Addresses
Pausing Timer When App Is in Background State Swift
Why Does It Take Such a Long Time for UI to Be Updated from Background Thread