Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prop to specify whether the directions API should return multiple routes, and if so allow for specification of which route to render. #188

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/MapViewDirections.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class MapViewDirections extends Component {
precision = 'low',
timePrecision = 'none',
channel,
preferredRoute
} = props;

if (!apikey) {
Expand Down Expand Up @@ -174,7 +175,7 @@ class MapViewDirections extends Component {
}

return (
this.fetchRoute(directionsServiceBaseUrl, origin, waypoints, destination, apikey, mode, language, region, precision, timePrecisionString, channel)
this.fetchRoute(directionsServiceBaseUrl, origin, waypoints, destination, apikey, mode, language, region, precision, timePrecisionString, channel, preferredRoute)
.then(result => {
return result;
})
Expand Down Expand Up @@ -227,7 +228,7 @@ class MapViewDirections extends Component {
});
}

fetchRoute(directionsServiceBaseUrl, origin, waypoints, destination, apikey, mode, language, region, precision, timePrecision, channel) {
fetchRoute(directionsServiceBaseUrl, origin, waypoints, destination, apikey, mode, language, region, precision, timePrecision, channel, preferredRoute) {

// Define the URL to call. Only add default parameters to the URL if it's a string.
let url = directionsServiceBaseUrl;
Expand All @@ -239,6 +240,9 @@ class MapViewDirections extends Component {
if(channel){
url+=`&channel=${channel}`;
}
if (preferredRoute) {
url += `&alternatives=${true}`
}
}

return fetch(url)
Expand All @@ -252,7 +256,7 @@ class MapViewDirections extends Component {

if (json.routes.length) {

const route = json.routes[0];
const route = preferredRoute && preferredRoute < json.routes.length ? json.routes[preferredRoute] : json.routes[0];

return Promise.resolve({
distance: route.legs.reduce((carry, curr) => {
Expand Down Expand Up @@ -354,4 +358,4 @@ MapViewDirections.propTypes = {
channel: PropTypes.string,
};

export default MapViewDirections;
export default MapViewDirections;