Skip to content

Commit

Permalink
Bing support
Browse files Browse the repository at this point in the history
  • Loading branch information
codingsince1985 committed Feb 16, 2015
1 parent 1bb12ed commit 5be895a
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ A geocoding service developed in Go's way, idiomatic and elegant, not just in go
This product is designed to open to any Geocoding service. Based on it,
+ [Google Maps](https://developers.google.com/maps/documentation/geocoding/)
+ MapQuest
- [Nominatim](http://open.mapquestapi.com/nominatim/)
- [Open](http://open.mapquestapi.com/geocoding/)
- [Nominatim Search](http://open.mapquestapi.com/nominatim/)
- [Open Geocoding](http://open.mapquestapi.com/geocoding/)
+ [OpenCage](http://geocoder.opencagedata.com/api.html)
+ [HERE](https://developer.here.com/rest-apis/documentation/geocoder)
+ [Bing](https://msdn.microsoft.com/en-us/library/ff701715.aspx)

clients are implemented in ~50 LoC each.

It allows you to switch from one service to another by changing only 1 line. Just like this.
It allows you to switch from one service to another by changing only 1 line, or enjoy all the free quota (requests/sec, day, month...) from them at the same time. Just like this.

```go
package main

import (
"fmt"
"github.com/codingsince1985/geo-golang"
"github.com/codingsince1985/geo-golang/bing"
"github.com/codingsince1985/geo-golang/google"
"github.com/codingsince1985/geo-golang/here"
"github.com/codingsince1985/geo-golang/mapquest/nominatim"
Expand All @@ -46,6 +48,9 @@ func main() {

// HERE
try(here.Geocoder("HERE_APP_ID", "HERE_APP_CODE", RADIUS))

// Bing
try(bing.Geocoder("BING_KEY"))
}

func try(geocoder geo.Geocoder) {
Expand All @@ -55,3 +60,7 @@ func try(geocoder geo.Geocoder) {
fmt.Printf("Address of (%f,%f) is %s\n\n", lat, lng, address)
}
```

License
=
geo-goang is distributed under the terms of the MIT license. See LICENSE for details.
57 changes: 57 additions & 0 deletions bing/geocoder.go
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
}
33 changes: 33 additions & 0 deletions bing/geocoder_test.go
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)
}
}

0 comments on commit 5be895a

Please sign in to comment.