mirror of
https://github.com/imjasonh/staticmaps
synced 2026-07-16 12:31:46 +00:00
Add DistanceMatrix API
This commit is contained in:
parent
c986c5df38
commit
2caece1694
3 changed files with 84 additions and 0 deletions
63
distance.go
Normal file
63
distance.go
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
package maps
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c Client) DistanceMatrix(orig, dest []Location, opts *DistanceMatrixOpts) (*DistanceMatrixResponse, error) {
|
||||||
|
var d DistanceMatrixResponse
|
||||||
|
if err := c.doDecode(baseURL+distancematrix(orig, dest, opts), &d); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &d, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func distancematrix(orig, dest []Location, opts *DistanceMatrixOpts) string {
|
||||||
|
p := url.Values{}
|
||||||
|
p.Set("origins", encodeLocations(orig))
|
||||||
|
p.Set("destinations", encodeLocations(dest))
|
||||||
|
opts.update(p)
|
||||||
|
return "distancematrix/json?" + p.Encode()
|
||||||
|
}
|
||||||
|
|
||||||
|
type DistanceMatrixOpts struct {
|
||||||
|
Language, Units string
|
||||||
|
Mode, Avoid *string
|
||||||
|
DepartureTime time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *DistanceMatrixOpts) update(p url.Values) {
|
||||||
|
if o == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if o.Mode != nil {
|
||||||
|
p.Set("mode", *o.Mode)
|
||||||
|
}
|
||||||
|
if o.Language != "" {
|
||||||
|
p.Set("language", o.Language)
|
||||||
|
}
|
||||||
|
if o.Avoid != nil {
|
||||||
|
p.Set("avoid", *o.Avoid)
|
||||||
|
}
|
||||||
|
if o.Units != "" {
|
||||||
|
p.Set("units", o.Units)
|
||||||
|
}
|
||||||
|
if !o.DepartureTime.IsZero() {
|
||||||
|
p.Set("departure_time", fmt.Sprintf("%d", o.DepartureTime.Unix()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type DistanceMatrixResponse struct {
|
||||||
|
Status string `json:"status"`
|
||||||
|
OriginAddresses []string `json:"origin_addresses"`
|
||||||
|
DestinationAddresses []string `json:"destination_addresses"`
|
||||||
|
Rows []struct {
|
||||||
|
Elements []struct {
|
||||||
|
Status string `json:"status"`
|
||||||
|
Duration Duration `json:"duration"`
|
||||||
|
Distance Distance `json:"distance"`
|
||||||
|
} `json:"elements"`
|
||||||
|
} `json:"rows"`
|
||||||
|
}
|
||||||
4
maps.go
4
maps.go
|
|
@ -107,3 +107,7 @@ func encodeLatLngs(ll []LatLng) string {
|
||||||
func Float64(f float64) *float64 {
|
func Float64(f float64) *float64 {
|
||||||
return &f
|
return &f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func String(s string) *string {
|
||||||
|
return &s
|
||||||
|
}
|
||||||
|
|
|
||||||
17
maps_test.go
17
maps_test.go
|
|
@ -159,3 +159,20 @@ func TestGeocode(t *testing.T) {
|
||||||
}
|
}
|
||||||
t.Logf("%v", r)
|
t.Logf("%v", r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDistanceMatrix(t *testing.T) {
|
||||||
|
c := NewClient("")
|
||||||
|
orig := []Location{Address("Vancouver, BC"), Address("Seattle")}
|
||||||
|
dst := []Location{Address("San Francisco"), Address("Victoria, BC")}
|
||||||
|
opts := &DistanceMatrixOpts{
|
||||||
|
Mode: String(ModeBicycling),
|
||||||
|
Language: "fr-FR",
|
||||||
|
}
|
||||||
|
t.Logf("%s", baseURL+distancematrix(orig, dst, opts))
|
||||||
|
r, err := c.DistanceMatrix(orig, dst, opts)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
t.Logf("%v", r)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue