google

package module
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package google provides functions for measuring distances and durations using the Google Distance Matrix API and polylines from Google Maps Distance API. A Google Maps client and request are required. The client uses an API key for authentication.

  • Matrix API: At a minimum, the request requires the origins and destinations to estimate.

  • Distance API: At minimum, the request requires the origin and destination. But it is recommended to pass in waypoints encoded as a polyline with "enc:" as a prefix to get a more precise polyline for each leg of the route.

Deprecated: This package is deprecated and will be removed in the next major release. It is used with the router engine which was replaced by github.com/nextmv-io/sdk/measure/google.

Example

Minimal example of how to create a client and matrix request, assuming the points are in the form longitude, latitude. Please note that this example does not define an output as it requires API authentication and is here for illustrative purposes only.

package main

import (
	"fmt"

	"github.com/nextmv-io/sdk/route/google"
	"googlemaps.github.io/maps"
)

func main() {
	points := [][2]float64{
		{-74.028297, 4.875835},
		{-74.046965, 4.872842},
		{-74.041763, 4.885648},
	}
	coords := make([]string, len(points))
	for p, point := range points {
		coords[p] = fmt.Sprintf("%f,%f", point[1], point[0])
	}
	r := &maps.DistanceMatrixRequest{
		Origins:      coords,
		Destinations: coords,
	}
	c, err := maps.NewClient(maps.WithAPIKey("<YOUR-API-KEY>"))
	if err != nil {
		panic(err)
	}

	// Distance and duration matrices can be constructed with the functions
	// provided in the package.
	dist, dur, err := google.DistanceDurationMatrices(c, r)
	if err != nil {
		panic(err)
	}

	// Once the measures have been created, you may estimate the distances and
	// durations by calling the Cost function.
	for p1 := range points {
		for p2 := range points {
			fmt.Printf(
				"(%d, %d) = [%f, %f]\n",
				p1, p2, dist.Cost(p1, p2), dur.Cost(p1, p2),
			)
		}
	}
	// This is the expected output.
	// (0, 0) = [0.000000, 0.000000]
	// (0, 1) = [6526.000000, 899.000000]
	// (0, 2) = [4889.000000, 669.000000]
	// (1, 0) = [5211.000000, 861.000000]
	// (1, 1) = [0.000000, 0.000000]
	// (1, 2) = [2260.000000, 302.000000]
	// (2, 0) = [3799.000000, 638.000000]
	// (2, 1) = [2260.000000, 311.000000]
	// (2, 2) = [0.000000, 0.000000]
}
Output:

Example (Polylines)

Making a request to retrieve polylines works similar. In this example we make a request to retrieve polylines by creating a DirectionsRequest. The polylines function returns a polyline from start to end and a slice of polylines for each leg, given through the waypoints. All polylines are encoded in Google's polyline format. Please note that this example does not define an output as it requires API authentication and is here for illustrative purposes only.

package main

import (
	"github.com/nextmv-io/sdk/route/google"
	"googlemaps.github.io/maps"
)

func main() {
	points := [][2]float64{
		{-74.028297, 4.875835},
		{-74.046965, 4.872842},
		{-74.041763, 4.885648},
	}
	coords := make([]string, len(points))
	rPoly := &maps.DirectionsRequest{
		Origin:      coords[0],
		Destination: coords[len(coords)-1],
		Waypoints:   coords[1 : len(coords)-1],
	}
	c, err := maps.NewClient(maps.WithAPIKey("<YOUR-API-KEY>"))
	if err != nil {
		panic(err)
	}
	fullPoly, polyLegs, err := google.Polylines(c, rPoly)
	if err != nil {
		panic(err)
	}
	_, _ = fullPoly, polyLegs
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DistanceDurationMatrices deprecated

func DistanceDurationMatrices(c *maps.Client, r *maps.DistanceMatrixRequest) (
	route.ByIndex,
	route.ByIndex,
	error,
)

DistanceDurationMatrices makes requests to the Google Distance Matrix API and returns route.ByIndex types to estimate distances (in meters) and durations (in seconds). It receives a Google Maps Client and Request. The coordinates passed to the request must be in the form latitude, longitude. The resulting distance and duration matrices are saved in memory. To find out how to create a client and request, please refer to the go package docs. This function takes into consideration the usage limits of the Distance Matrix API and thus may transform the request into multiple ones and handle them accordingly. You can find more about usage limits here in the official google maps documentation for the distance matrix, usage and billing.

Deprecated: This package is deprecated and will be removed in the next major release. It is used with the router engine which was replaced by github.com/nextmv-io/sdk/measure/google.

func Polylines deprecated

func Polylines(
	c *maps.Client,
	orgRequest *maps.DirectionsRequest,
) (string, []string, error)

Polylines requests polylines for the given points. The first parameter returns a polyline from start to end and the second parameter returns a list of polylines, one per leg.

Deprecated: This package is deprecated and will be removed in the next major release. It is used with the router engine which was replaced by github.com/nextmv-io/sdk/measure/google.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL