GoLang package that provides an easy way to use the Google Geocoding API.
See more information about the Google Geocoding API at the following link: https://developers.google.com/maps/documentation/geocoding/start
You can use go get:
go get github.com/kelvins/geocoder
Example of usage:
package main
import (
"fmt""github.com/kelvins/geocoder"
)
funcmain() {
// This example should work without need of an API Key,// but when you will use the API in your project you need// to set your API KEY as explained here:// https://developers.google.com/maps/documentation/geocoding/get-api-key// geocoder.ApiKey = "YOUR_API_KEY"// See all Address fields in the documentationaddress:= geocoder.Address{
Street: "Central Park West",
Number: 115,
City: "New York",
State: "New York",
Country: "United States",
}
// Convert address to location (latitude, longitude)location, err:=geocoder.Geocoding(address)
iferr!=nil {
fmt.Println("Could not get the location: ", err)
} else {
fmt.Println("Latitude: ", location.Latitude)
fmt.Println("Longitude: ", location.Longitude)
}
// Set the latitude and longitudelocation= geocoder.Location{
Latitude: 40.775807,
Longitude: -73.97632,
}
// Convert location (latitude, longitude) to a slice of addressesaddresses, err:=geocoder.GeocodingReverse(location)
iferr!=nil {
fmt.Println("Could not get the addresses: ", err)
} else {
// Usually, the first address returned from the API// is more detailed, so let's work with itaddress=addresses[0]
// Print the address formatted by the geocoder packagefmt.Println(address.FormatAddress())
// Print the formatted address from the APIfmt.Println(address.FormattedAddress)
// Print the type of the addressfmt.Println(address.Types)
}
}Results:
Latitude: 40.7758882
Longitude: -73.9764703
115, Central Park West, Manhattan, 10023, New York, New York County, New York, United States
115 Central Park West, New York, NY 10023, USA
street_address
You can access the full documentation here:
This project was created under the MIT license. You can read the license here:
Feel free to contribute by commenting, suggesting, creating issues or sending pull requests. Any help is welcome.
- Create an issue (optional)
- Fork the repo
- Make your changes
- Commit your changes (
git commit -am 'Some cool feature') - Push to the branch (
git push origin master) - Create a new Pull Request
With a little more effort you can use goroutines with channels:
package main
import (
"fmt""time""github.com/kelvins/geocoder"
)
// Use the channels system to call the Geocoding functionfuncgeocodingChannel(address geocoder.Address, locationChanchan geocoder.Location, errChanchanerror) {
fmt.Println("Entering geocodingChannel function...")
deferfmt.Println("Exiting geocodingChannel function...")
deferclose(locationChan)
deferclose(errChan)
location, err:=geocoder.Geocoding(address)
locationChan<-locationerrChan<-err
}
// Use the channels system to call the GeocodingReverse functionfuncgeocodingReverseChannel(location geocoder.Location, addressesChanchan []geocoder.Address, errChanchanerror) {
fmt.Println("Entering geocodingReverseChannel function...")
deferfmt.Println("Exiting geocodingReverseChannel function...")
deferclose(addressesChan)
deferclose(errChan)
addresses, err:=geocoder.GeocodingReverse(location)
addressesChan<-addresseserrChan<-err
}
funcmain() {
// See all Address fields in the documentationaddress:= geocoder.Address{
Street: "Central Park West",
Number: 115,
City: "New York",
State: "New York",
Country: "United States",
}
// Make the channelslocationChan:=make(chan geocoder.Location)
errChan:=make(chanerror)
// Call the go routinegogeocodingChannel(address, locationChan, errChan)
// Do whatever you need to doforindex:=0; index<5; index++ {
fmt.Println(index)
time.Sleep(50*time.Millisecond)
}
// Get the resultslocation:=<-locationChanerr:=<-errChan// Check if the results are validiferr!=nil {
fmt.Println("Could not get the location: ", err)
} else {
fmt.Println("Latitude: ", location.Latitude)
fmt.Println("Longitude: ", location.Longitude)
}
// Set the latitude and longitudelocation= geocoder.Location{
Latitude: 40.775807,
Longitude: -73.97632,
}
// Make the channelsaddressesChan:=make(chan []geocoder.Address)
errChan=make(chanerror)
// Call the go routinegogeocodingReverseChannel(location, addressesChan, errChan)
// Do whatever you need to doforindex:=0; index<5; index++ {
fmt.Println(index)
time.Sleep(50*time.Millisecond)
}
// Get the resultsaddresses:=<-addressesChanerr=<-errChan// Check if the results are validiferr!=nil {
fmt.Println("Could not get the addresses: ", err)
} else {
fmt.Println(addresses[0].FormattedAddress)
}
}