- Returns a Longitude and Latitude for a given string query
- Returns an address for a Longitude and Longitude
- Returns directions between two or more points. (JSON or XML)
Get a free API Key at http://mapquestapi.com.
Google Maps Geocoding API has a limitation that prohibits querying their geocoding API unless you will be displaying the results on a Google Map. Google directions is limited to 2 requests per second. MapQuest's geocoding API does not have these restrictions.
- go get "github.com/jasonwinn/geocoder"
- import "github.com/jasonwinn/geocoder"
You'll want to set an api key for the Mapquest API to go into production.
// this is the testing key used in `go test`
SetAPIKey("Fmjtd%7Cluub256alu%2C7s%3Do5-9u82ur")
To retrieve just the latitude and longitude of the best match for a particular query, use the Geocode method:
query := "Seattle WA"
lat, lng, err := geocoder.Geocode(query)
if err != nil {
panic("THERE WAS SOME ERROR!!!!!")
}
// 47.6064, -122.330803
To retrieve a full geocoding result including all matches as well as additional location information per match like the street, city and state, use the FullGeocode method:
query := "Seattle WA"
result, err := geocoder.FullGeocode(query)
if err != nil {
panic("THERE WAS SOME ERROR!!!!!")
}
// GeocodingResult instance
address, err := geocoder.ReverseGeocode(47.6064, -122.330803)
if err != nil {
panic("THERE WAS SOME ERROR!!!!!")
}
address.Street // 542 Marion St
address.City // Seattle
address.State // WA
address.PostalCode // 98104
address.County // King
address.CountryCode // US
directions := NewDirections("Amsterdam,Netherlands", []string{"Antwerp,Belgium"})
results, err := directions.Get()
if err != nil {
panic("THERE WAS SOME ERROR!!!!!")
}
route := results.Route
time := route.Time
legs := route.Legs
distance := route.Distance
// or get distance with this shortcut
//
// use "k" to return result in km
// use "m" to return result in miles
distance, err := directions.Distance("k")
if err != nil {
panic("THERE WAS SOME ERROR!!!!!")
}