-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1bb12ed
commit 5be895a
Showing
3 changed files
with
102 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Package bing is a geo-golang based Microsoft Bing geocode/reverse geocode client | ||
package bing | ||
|
||
import ( | ||
"fmt" | ||
"github.com/codingsince1985/geo-golang" | ||
"strings" | ||
) | ||
|
||
type baseUrl string | ||
|
||
type geocodeResponse struct { | ||
ResourceSets []struct { | ||
Resources []struct { | ||
Point struct { | ||
Coordinates []float64 | ||
} | ||
Address struct { | ||
FormattedAddress string | ||
} | ||
} | ||
} | ||
} | ||
|
||
func Geocoder(key string) geo.Geocoder { | ||
return geo.Geocoder{ | ||
baseUrl("http://dev.virtualearth.net/REST/v1/Locations*key=" + key), | ||
&geocodeResponse{}, | ||
} | ||
} | ||
|
||
func (b baseUrl) GeocodeUrl(address string) string { | ||
return strings.Replace(string(b), "*", "?q="+address+"&", 1) | ||
} | ||
|
||
func (b baseUrl) ReverseGeocodeUrl(l geo.Location) string { | ||
return strings.Replace(string(b), "*", fmt.Sprintf("/%f,%f?", l.Lat, l.Lng), 1) | ||
} | ||
|
||
func (r *geocodeResponse) Location() (l geo.Location) { | ||
if len(r.ResourceSets[0].Resources) > 0 { | ||
c := r.ResourceSets[0].Resources[0].Point.Coordinates | ||
l = geo.Location{c[0], c[1]} | ||
} | ||
return | ||
} | ||
|
||
func (r *geocodeResponse) Address() (address string) { | ||
if len(r.ResourceSets[0].Resources) > 0 { | ||
address = r.ResourceSets[0].Resources[0].Address.FormattedAddress | ||
} | ||
return | ||
} | ||
|
||
func (r *geocodeResponse) ResponseObject() geo.ResponseParser { | ||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package bing_test | ||
|
||
import ( | ||
"github.com/codingsince1985/geo-golang" | ||
"github.com/codingsince1985/geo-golang/bing" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
const key = "YOUR_KEY" | ||
|
||
var geocoder = bing.Geocoder(key) | ||
|
||
func TestGeocode(t *testing.T) { | ||
location, err := geocoder.Geocode("Melbourne VIC") | ||
if err != nil || location.Lat != -37.82429885864258 || location.Lng != 144.97799682617188 { | ||
t.Error("TestGeocode() failed", err, location) | ||
} | ||
} | ||
|
||
func TestReverseGeocode(t *testing.T) { | ||
address, err := geocoder.ReverseGeocode(-37.816742, 144.964463) | ||
if err != nil || !strings.HasSuffix(address, "Melbourne, VIC 3000") { | ||
t.Error("TestReverseGeocode() failed", err, address) | ||
} | ||
} | ||
|
||
func TestReverseGeocodeWithNoResult(t *testing.T) { | ||
_, err := geocoder.ReverseGeocode(-37.816742, 164.964463) | ||
if err != geo.NoResultError { | ||
t.Error("TestReverseGeocodeWithNoResult() failed", err) | ||
} | ||
} |