Use optimitazion api with leaflet

Hi there,

I’m developing an angular app that use leaflet geoJSON to display point on map.

I’ve tried with direction service and I’ve not found any problem, all works like a sharm, but when I try to planning some operation with optimitazion service, even if response works well, infact it return me a json, but I need geoJSON.

Is possibile to have a response from optimitazion service directly in geoJSON?

thanks in advance.
Good day.

No that’s not possible (currently). The optimization result offers a lot of information, which could be cast into geojson, true. It’s just not supported.

If all you need is the geometry, you can add a

{
....,
    "options": {
        "g": true
    }
}

to the POST request and convert the encoded polyline to a GeoJSON directly with e.g. Mapbox’s polyline utility.

If you also need all the properties you’ll have to parse the JSON and pull what you need.

Hi there,

thanks for your hint.

take a look to my code (Angular):

const apiUrl = 'https://api.openrouteservice.org/optimization';

  let jobsBody = [];
  let vehiclesBody = [];
  for (let i = 0; i < 6; i++) {
    const jobsLocations = [
      [12.0202443, 42.039349],
      [14.5911201, 40.8209326],
      [12.5902917, 43.0957988],
      [12.5454813, 42.89363],
      [12.6587053, 42.9745784],
      [12.7339929, 43.3330812]
    ]

    const vehiclesLocations = [12.8059101, 43.4353756]

    jobsBody = [...jobsBody, {
      id: i,
      service: 300,
      location: jobsLocations[i],
      skills: [1],
      time_windows: [[32400, 36000]]
    }]

    if (i < 2) {
      vehiclesBody = [...vehiclesBody, {
        id: i,
        profile: 'driving-car',
        start: vehiclesLocations,
        end: vehiclesLocations,
        capacity: [4],
        skills: [1, 14],
        time_window: [28800, 43200]
      }]
    }

  }

  const body = {
    jobs: jobsBody,
    vehicles: vehiclesBody,
    options: {
      g:
        true
    }
  }

  this.http.post<any>(apiUrl, body).subscribe(data => {
    let step = [];
    let featureObj = [];

    data.routes.map(route => {
      let location = [];
      route.steps.forEach(place => location = [...location, place.location])
      step = [...step, location]
    });

    for (const index in step) {
      if (index) {
        featureObj = [...featureObj, {
          type: 'Feature',
          geometry: {
            type: 'LineString',
            coordinates: step[index]
          },
          properties: {
            color: this.getRandomColor()
          }
        }]
      }
    }

    console.log(featureObj)
    this.layers = [...this.layers, L.geoJSON({
      type: 'FeatureCollection',
      features: featureObj
    }, {
      onEachFeature(feature, layer) {
        if (layer instanceof L.Polyline) layer.setStyle({ color: feature.properties.color })
      }
    })]

  })

and to my map:

I’m noticing that the routing pass throug steps but not respect street directions, cut the streets over.

Where I’m failing?

thanks a lot.

Sorry we generally don’t look at code, since that implies that smth is wrong on your side, not ours (unless it’s one of our own libraries).

Please refer to the appropriate forum, SO or similar.

Hi there,

Now I’m enable to display the current optimization route.

now is possibile to get directions from optimization route, I need to retrive the street name that vehicle use to delivery something.

I’ve seen that optimization has coordinates key in geojson, but inside it there are a bunch of coordinates, I need to use it like a body to do to the post to direction, or there is another way?

thank a lot.

You can either use the "g":true parameter in the options as nils mentioned before and decode the polyline

Or you have to do seperate routing requests with the /directions api for the suggested routes.

btw. if the blue pins in your picture are your inputs, and the dark blue line is the “optimized” route, there is something wrong with the response, the decoding or the displaying in leaflet.

Best regards.