Skip to content

Commit 5e8fdc1

Browse files
mapbox geocoding and reverse geocoding
1 parent 033ef73 commit 5e8fdc1

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/codingsince1985/geo-golang/mapquest/nominatim"
3030
"github.com/codingsince1985/geo-golang/mapquest/open"
3131
"github.com/codingsince1985/geo-golang/opencage"
32+
"github.com/codingsince1985/geo-golang/mapbox"
3233
)
3334

3435
const addr = "Melbourne VIC"
@@ -52,6 +53,9 @@ func main() {
5253

5354
// Bing
5455
try(bing.Geocoder("BING_KEY"))
56+
57+
// Mapbox
58+
try(mapbox.Geocoder("YOUR_ACCESS_TOKEN"))
5559
}
5660

5761
func try(geocoder geo.Geocoder) {
@@ -86,6 +90,10 @@ Address of (-37.816742,144.964463) is 40-44 Elizabeth St, Melbourne VIC 3000, Au
8690
// Bing
8791
Melbourne VIC location is {-37.82429885864258 144.97799682617188}
8892
Address of (-37.816742,144.964463) is 46 Elizabeth St, Melbourne, VIC 3000
93+
94+
// Mapbox
95+
Melbourne VIC location is {-37.8142 144.9632}
96+
Address of (-37.816742,144.964463) is Bankwest ATM, 43-55 Elizabeth St, 3000 Melbourne, Australia
8997
```
9098
License
9199
==

mapbox/geocoder.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Package mapbox is a geo-golang based Mapbox geocode/reverse geocode client
2+
package mapbox
3+
4+
import (
5+
"fmt"
6+
"github.com/codingsince1985/geo-golang"
7+
"strings"
8+
)
9+
10+
type baseURL string
11+
12+
type geocodeResponse struct {
13+
Query []interface{}
14+
Features []struct {
15+
Place_Name string
16+
Center [2]float64
17+
}
18+
}
19+
20+
// Geocoder constructs Mapbox geocoder
21+
func Geocoder(token string) geo.Geocoder {
22+
return geo.Geocoder{
23+
baseURL("https://api.mapbox.com/geocoding/v5/mapbox.places/*.json?access_token=" + token),
24+
&geocodeResponse{},
25+
}
26+
}
27+
28+
func (b baseURL) GeocodeURL(address string) string {
29+
return strings.Replace(string(b), "*", address, 1)
30+
}
31+
32+
func (b baseURL) ReverseGeocodeURL(l geo.Location) string {
33+
return strings.Replace(string(b), "*", fmt.Sprintf("%+f,%+f", l.Lng, l.Lat), 1)
34+
}
35+
36+
func (r *geocodeResponse) Location() (l geo.Location) {
37+
if len(r.Features) > 0 {
38+
g := r.Features[0]
39+
l = geo.Location{g.Center[1], g.Center[0]}
40+
}
41+
return
42+
}
43+
44+
func (r *geocodeResponse) Address() (address string) {
45+
if len(r.Features) > 0 {
46+
address = r.Features[0].Place_Name
47+
}
48+
return
49+
}
50+
51+
func (r *geocodeResponse) ResponseObject() geo.ResponseParser {
52+
return r
53+
}

mapbox/geocoder_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package mapbox_test
2+
3+
import (
4+
"github.com/codingsince1985/geo-golang"
5+
"github.com/codingsince1985/geo-golang/mapbox"
6+
"strings"
7+
"testing"
8+
)
9+
10+
const token = "YOUR_ACCESS_TOKEN"
11+
12+
var geocoder = mapbox.Geocoder(token)
13+
14+
func TestGeocode(t *testing.T) {
15+
location, err := geocoder.Geocode("Melbourne VIC")
16+
if err != nil || location.Lat != -37.8142 || location.Lng != 144.9632 {
17+
t.Error("TestGeocode() failed", err, location)
18+
}
19+
}
20+
21+
func TestReverseGeocode(t *testing.T) {
22+
address, err := geocoder.ReverseGeocode(-37.8142, 144.9632)
23+
if err != nil || !strings.HasSuffix(address, "Melbourne, Victoria 3000, Australia") {
24+
t.Error("TestReverseGeocode() failed", err, address)
25+
}
26+
}
27+
28+
func TestReverseGeocodeWithNoResult(t *testing.T) {
29+
_, err := geocoder.ReverseGeocode(-37.8142, 164.9632)
30+
if err != geo.ErrNoResult {
31+
t.Error("TestReverseGeocodeWithNoResult() failed", err)
32+
}
33+
}

0 commit comments

Comments
 (0)