mirror of
https://github.com/imjasonh/staticmaps
synced 2026-07-10 12:11:46 +00:00
Add Elevation API
This commit is contained in:
parent
42ad906d08
commit
d2aa07c3bd
3 changed files with 117 additions and 0 deletions
73
elevation.go
Normal file
73
elevation.go
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
package maps
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func (c Client) Elevation(ll []LatLng) (*ElevationResponse, error) {
|
||||
var r ElevationResponse
|
||||
if err := c.doDecode(baseURL+elevation(ll), &r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func (c Client) ElevationPolyline(p string) (*ElevationResponse, error) {
|
||||
var r ElevationResponse
|
||||
if err := c.doDecode(baseURL+elevationpoly(p), &r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func (c Client) ElevationPath(ll []LatLng, samples int) (*ElevationResponse, error) {
|
||||
var r ElevationResponse
|
||||
if err := c.doDecode(baseURL+elevationpath(ll, samples), &r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func (c Client) ElevationPathPoly(p string, samples int) (*ElevationResponse, error) {
|
||||
var r ElevationResponse
|
||||
if err := c.doDecode(baseURL+elevationpathpoly(p, samples), &r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func elevation(ll []LatLng) string {
|
||||
p := url.Values{}
|
||||
p.Set("locations", encodeLatLngs(ll))
|
||||
return "elevation/json?" + p.Encode()
|
||||
}
|
||||
|
||||
func elevationpoly(poly string) string {
|
||||
p := url.Values{}
|
||||
p.Set("locations", "enc:"+poly)
|
||||
return "elevation/json?" + p.Encode()
|
||||
}
|
||||
|
||||
func elevationpath(ll []LatLng, s int) string {
|
||||
p := url.Values{}
|
||||
p.Set("path", encodeLatLngs(ll))
|
||||
p.Set("samples", fmt.Sprintf("%d", s))
|
||||
return "elevation/json?" + p.Encode()
|
||||
}
|
||||
|
||||
func elevationpathpoly(poly string, s int) string {
|
||||
p := url.Values{}
|
||||
p.Set("path", "enc:"+poly)
|
||||
p.Set("samples", fmt.Sprintf("%d", s))
|
||||
return "elevation/json?" + p.Encode()
|
||||
}
|
||||
|
||||
type ElevationResponse struct {
|
||||
Results []struct {
|
||||
Elevation float64 `json:"elevation"`
|
||||
Location LatLng `json:"location"`
|
||||
Resolution float64 `json:"resolution"`
|
||||
}
|
||||
Status string `json:"status"`
|
||||
}
|
||||
8
maps.go
8
maps.go
|
|
@ -86,6 +86,14 @@ func encodeLocations(ls []Location) string {
|
|||
return strings.Join(s, "|")
|
||||
}
|
||||
|
||||
func encodeLatLngs(ll []LatLng) string {
|
||||
s := make([]string, len(ll))
|
||||
for i, l := range ll {
|
||||
s[i] = l.Location()
|
||||
}
|
||||
return strings.Join(s, "|")
|
||||
}
|
||||
|
||||
func Float64(f float64) *float64 {
|
||||
return &f
|
||||
}
|
||||
|
|
|
|||
36
maps_test.go
36
maps_test.go
|
|
@ -102,3 +102,39 @@ func TestTimeZone(t *testing.T) {
|
|||
}
|
||||
t.Logf("%v", r)
|
||||
}
|
||||
|
||||
func TestElevation(t *testing.T) {
|
||||
c := NewClient("")
|
||||
ll := []LatLng{{39.7391536, -104.9847034}}
|
||||
t.Logf("%s", baseURL+elevation(ll))
|
||||
r, err := c.Elevation(ll)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
t.Logf("%v", r)
|
||||
|
||||
p := "gfo}EtohhU"
|
||||
t.Logf("%s", baseURL+elevationpoly(p))
|
||||
r, err = c.ElevationPolyline(p)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
t.Logf("%v", r)
|
||||
|
||||
samples := 3
|
||||
ll = []LatLng{{36.578581, -118.291994}, {36.23998, -116.83171}}
|
||||
t.Logf("%s", baseURL+elevationpath(ll, samples))
|
||||
r, err = c.ElevationPath(ll, samples)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
t.Logf("%v", r)
|
||||
|
||||
p = "gfo}EtohhUxD@bAxJmGF"
|
||||
t.Logf("%s", baseURL+elevationpathpoly(p, samples))
|
||||
r, err = c.ElevationPathPoly(p, samples)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
t.Logf("%v", r)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue