Skip to content
GuidesBlogPlaygroundDashboard

Optimize route through waypoints

client.Optimize.New(ctx, params) (*OptimizeResult, error)
POST/api/v1/optimize

Optimize route through waypoints

ParametersExpand Collapse
params OptimizeNewParams
OptimizeRequest param.Field[OptimizeRequest]

Body param: Route optimization (Travelling Salesman) request. Finds the most efficient order to visit a set of waypoints. Minimum 2 waypoints, maximum 50. For large inputs, the request may be processed asynchronously.

Format param.Field[string]optional

Query param: Response format: json (default), geojson, csv, ndjson

ReturnsExpand Collapse
type OptimizeResult interface{…}

Optimization response — either a completed FeatureCollection with the optimized route, or an async job reference to poll.

One of the following:
type OptimizeCompletedResult struct{…}

Completed optimization result as a GeoJSON FeatureCollection. Each Feature is a waypoint in optimized visit order. Top-level fields provide summary statistics.

Features []OptimizeCompletedResultFeature

Waypoints in optimized visit order

GeoJSON Geometry object per RFC 7946. Coordinates use [longitude, latitude] order. 3D coordinates [lng, lat, elevation] are used for elevation endpoints.

Coordinates GeoJsonGeometryCoordinatesUnion

Coordinates array. Nesting depth varies by geometry type: Point = [lng, lat], LineString = [[lng, lat], ...], Polygon = [[[lng, lat], ...], ...], etc.

One of the following:
type GeoJsonGeometryCoordinatesPoint []float64

[longitude, latitude] or [longitude, latitude, elevation]

type GeoJsonGeometryCoordinatesLineStringOrMultiPoint [][]float64

Array of [lng, lat] positions

type GeoJsonGeometryCoordinatesPolygonOrMultiLineString [][][]float64

Array of linear rings / line strings

type GeoJsonGeometryCoordinatesMultiPolygon [][][][]float64

Array of polygons

Type GeoJsonGeometryType

Geometry type

One of the following:
const GeoJsonGeometryTypePoint GeoJsonGeometryType = "Point"
const GeoJsonGeometryTypeLineString GeoJsonGeometryType = "LineString"
const GeoJsonGeometryTypePolygon GeoJsonGeometryType = "Polygon"
const GeoJsonGeometryTypeMultiPoint GeoJsonGeometryType = "MultiPoint"
const GeoJsonGeometryTypeMultiLineString GeoJsonGeometryType = "MultiLineString"
const GeoJsonGeometryTypeMultiPolygon GeoJsonGeometryType = "MultiPolygon"
Properties OptimizeCompletedResultFeaturesProperties
CostS float64

Travel time in seconds from the previous waypoint to this one (0 for the first waypoint)

CumulativeCostS float64

Cumulative travel time in seconds from the start to this waypoint

WaypointIndex int64

Position of this waypoint in the optimized visit order (0-based)

Type OptimizeCompletedResultFeaturesType
Optimization string

Optimization method used (e.g. nearest_neighbor, 2opt)

Roundtrip bool

Whether the route returns to the starting waypoint

TotalCostS float64

Total travel time for the optimized route in seconds

Type OptimizeCompletedResultType
type OptimizeProcessingResult struct{…}

Async optimization in progress. Poll GET /api/v1/optimize/{job_id} until the status changes to completed or failed.

JobID string

Job ID for polling the result

Status OptimizeProcessingResultStatus

Always processing

Optimize route through waypoints

package main

import (
  "context"
  "fmt"

  "github.com/plazafyi/plaza-go"
  "github.com/plazafyi/plaza-go/option"
)

func main() {
  client := githubcomplazafyiplazago.NewClient(
    option.WithAPIKey("My API Key"),
  )
  optimizeResult, err := client.Optimize.New(context.TODO(), githubcomplazafyiplazago.OptimizeNewParams{
    OptimizeRequest: githubcomplazafyiplazago.OptimizeRequestParam{
      Waypoints: githubcomplazafyiplazago.F([]githubcomplazafyiplazago.OptimizeRequestWaypointParam{githubcomplazafyiplazago.OptimizeRequestWaypointParam{
        Lat: githubcomplazafyiplazago.F(48.856600),
        Lng: githubcomplazafyiplazago.F(2.352200),
      }, githubcomplazafyiplazago.OptimizeRequestWaypointParam{
        Lat: githubcomplazafyiplazago.F(48.860600),
        Lng: githubcomplazafyiplazago.F(2.337600),
      }, githubcomplazafyiplazago.OptimizeRequestWaypointParam{
        Lat: githubcomplazafyiplazago.F(48.858400),
        Lng: githubcomplazafyiplazago.F(2.294500),
      }}),
    },
  })
  if err != nil {
    panic(err.Error())
  }
  fmt.Printf("%+v\n", optimizeResult)
}
{
  "features": [
    {
      "geometry": {
        "coordinates": [
          2.3522,
          48.8566
        ],
        "type": "Point"
      },
      "properties": {
        "cost_s": 0,
        "cumulative_cost_s": 0,
        "waypoint_index": 0
      },
      "type": "Feature"
    }
  ],
  "optimization": "optimization",
  "roundtrip": true,
  "total_cost_s": 0,
  "type": "FeatureCollection"
}
Returns Examples
{
  "features": [
    {
      "geometry": {
        "coordinates": [
          2.3522,
          48.8566
        ],
        "type": "Point"
      },
      "properties": {
        "cost_s": 0,
        "cumulative_cost_s": 0,
        "waypoint_index": 0
      },
      "type": "Feature"
    }
  ],
  "optimization": "optimization",
  "roundtrip": true,
  "total_cost_s": 0,
  "type": "FeatureCollection"
}