gogeocode

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2025 License: MIT Imports: 7 Imported by: 0

README

GoGeocode

simple go package for using the geocoding api at [geocode.maps.co]

API Key

You can sign up for an api key at: [https://geocode.maps.co/join/]

Geocode

Geocoding takes an address or similar location description, then returns a precise location description

package main
import "github.com/ruesier/gogeocode"

func main() {
    client := gogeocode.Client{
        ApiKey: "GEOCODE_API_KEY"
    }

    response, err := client.Geocode("The Statue of Liberty")
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("The Statue of Liberty is at Lat: %s, Long: %s", response[0].Latitude, response[0].Longitude)
}

Output: The Statue of Liberty is at Lat: 40.689253199999996, Long: -74.04454817144321

Reverse Geocode

Reverse Geocoding takes a latitude and longitude then returns Addresses that are at that point.

package main

func main() {
    client := gogeocode.Client{
        ApiKey: "GEOCODE_API_KEY"
    }

    response, err := client.Reverse(40.689253199999996, -74.04454817144321)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(respose.Address.Tourism)
}

Output: Statue of Liberty

Documentation

Overview

Package gogeocode provides utilities for utilizing the geocoding API provided through https://geocode.maps.co.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAuthorization = errors.New("Geocode invalid API Key")
	ErrThrottle      = errors.New("Geocode failed due to exceeding rquest limit")
	ErrTraffic       = errors.New("Geocode failed due to high traffic on geocode server")
	ErrFlooding      = errors.New("Geocode has detected api key abuse, contact: https://maps.co/contact/ to resolve")
)

Functions

This section is empty.

Types

type Address

type Address struct {
	Tourism        string `json:"tourism"`
	HouseNumber    string `json:"house_number"`
	Road           string `json:"road"`
	Neighbourhood  string `json:"neighbourhood"`
	Suburb         string `json:"suburb"`
	County         string `json:"county"`
	City           string `json:"city"`
	State          string `json:"state"`
	ISO3166_2_lvl4 string `json:"ISO3166-2-lvl4"`
	PostCode       string `json:"postcode"`
	Country        string `json:"country"`
	CountryCode    string `json:"country_code"`
}

Address is apart of the Revese response

type Client

type Client struct {
	ApiKey string
}

Client is used to call geocoding api

func (Client) AddressGeocode

func (c Client) AddressGeocode(street, city, county, state, country, postalcode string) ([]*Response, error)

AddressGeocode geocodes a specific address.

func (Client) AddressGeocodeWithContext

func (c Client) AddressGeocodeWithContext(ctx context.Context, street, city, county, state, country, postalcode string) ([]*Response, error)

AddressGeocodeWithContext performs same action as AddressGeocode with provided context

func (Client) Geocode

func (c Client) Geocode(query string) ([]*Response, error)

Geocode takes a string description of a location and returns precise location data. Possible queries include addresses or famous place names.

func (Client) GeocodeWithContext

func (c Client) GeocodeWithContext(ctx context.Context, query string) ([]*Response, error)

GeocodeWithContext performs the same request as Geocode using the given context.

func (Client) Reverse

func (c Client) Reverse(lat, long float64) (*Response, error)

Reverse takes a latitude and longitude and returns nearest address

func (Client) ReverseWithContext

func (c Client) ReverseWithContext(ctx context.Context, lat, long float64) (*Response, error)

type Response

type Response struct {
	PlaceID     uint64   `json:"place_id"`
	Licence     string   `json:"licence"`
	OSMType     string   `json:"osm_type"`
	OSMID       uint64   `json:"osm_id"`
	BoundingBox []string `json:"boundingbox"`
	Latitude    string   `json:"lat"`
	Longitude   string   `json:"lon"`
	DisplayName string   `json:"display_name"`
	Class       string   `json:"class"`
	Type        string   `json:"type"`
	Importance  float64  `json:"importance"`
	// Address is only included when calling Reverse
	Address Address `json:"address"`
}

Response contains all possible fields returned by the API

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL