geolocate

package module
v0.0.0-...-7206ade Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2016 License: MIT Imports: 6 Imported by: 4

README

GeoLocate

Build Status GoDoc

request := Request{
	CellTowers: []CellTower{
		{250, 2, 7743, 22517, -78, 0, 0},
		{250, 2, 7743, 39696, -81, 0, 0},
		{250, 2, 7743, 22518, -91, 0, 0},
		{250, 2, 7743, 27306, -101, 0, 0},
		{250, 2, 7743, 29909, -103, 0, 0},
		{250, 2, 7743, 22516, -104, 0, 0},
		{250, 2, 7743, 20736, -105, 0, 0},
	},
	WifiAccessPoints: []WifiAccessPoint{
		{"2:18:E4:C8:38:30", -22, 0, 0, 0},
	},
}

google, err := New(Google, googleAPIKey)
if err != nil {
	log.Fatal(err)
}
resp, err := google.Get(request)
if err != nil {
	t.Error(err)
}
fmt.Println("Google:", resp)

mozilla, err := New(Mozilla, "test")
if err != nil {
	log.Fatal(err)
}
resp, err = mozilla.Get(request)
if err != nil {
	t.Error(err)
}
fmt.Println("Mozilla:", resp)

yandex, err := New(Yandex, yandexAPIKey)
if err != nil {
	log.Fatal(err)
}
resp, err = yandex.Get(request)
if err != nil {
	t.Error(err)
}
fmt.Println("Yandex:", resp)

Documentation

Overview

Example (Google)
package main

import (
	"fmt"
	"log"

	"github.com/mdigger/geolocate"
)

// Your API Keys
const googleAPIKey = "<API Key>"

func main() {
	request := geolocate.Request{
		CellTowers: []geolocate.CellTower{
			{250, 2, 7743, 22517, -78, 0, 0},
			{250, 2, 7743, 39696, -81, 0, 0},
			{250, 2, 7743, 22518, -91, 0, 0},
			{250, 2, 7743, 27306, -101, 0, 0},
			{250, 2, 7743, 29909, -103, 0, 0},
			{250, 2, 7743, 22516, -104, 0, 0},
			{250, 2, 7743, 20736, -105, 0, 0},
		},
		WifiAccessPoints: []geolocate.WifiAccessPoint{
			{"2:18:E4:C8:38:30", -22, 0, 0, 0},
		},
	}
	google, err := geolocate.New(geolocate.Google, googleAPIKey)
	if err != nil {
		log.Fatal(err)
	}
	resp, err := google.Get(request)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Google:", resp)
}
Example (Mozilla)
package main

import (
	"fmt"
	"log"

	"github.com/mdigger/geolocate"
)

func main() {
	request := geolocate.Request{
		CellTowers: []geolocate.CellTower{
			{250, 2, 7743, 22517, -78, 0, 0},
			{250, 2, 7743, 39696, -81, 0, 0},
			{250, 2, 7743, 22518, -91, 0, 0},
			{250, 2, 7743, 27306, -101, 0, 0},
			{250, 2, 7743, 29909, -103, 0, 0},
			{250, 2, 7743, 22516, -104, 0, 0},
			{250, 2, 7743, 20736, -105, 0, 0},
		},
		WifiAccessPoints: []geolocate.WifiAccessPoint{
			{"2:18:E4:C8:38:30", -22, 0, 0, 0},
		},
	}
	mozilla, err := geolocate.New(geolocate.Mozilla, "test")
	if err != nil {
		log.Fatal(err)
	}
	resp, err := mozilla.Get(request)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Mozilla:", resp)
}
Example (Yandex)
package main

import (
	"fmt"
	"log"

	"github.com/mdigger/geolocate"
)

// Your API Keys
const yandexAPIKey = "<API Key>"

func main() {
	request := geolocate.Request{
		CellTowers: []geolocate.CellTower{
			{250, 2, 7743, 22517, -78, 0, 0},
			{250, 2, 7743, 39696, -81, 0, 0},
			{250, 2, 7743, 22518, -91, 0, 0},
			{250, 2, 7743, 27306, -101, 0, 0},
			{250, 2, 7743, 29909, -103, 0, 0},
			{250, 2, 7743, 22516, -104, 0, 0},
			{250, 2, 7743, 20736, -105, 0, 0},
		},
		WifiAccessPoints: []geolocate.WifiAccessPoint{
			{"2:18:E4:C8:38:30", -22, 0, 0, 0},
		},
	}
	yandex, err := geolocate.New(geolocate.Yandex, yandexAPIKey)
	if err != nil {
		log.Fatal(err)
	}
	resp, err := yandex.Get(request)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Yandex:", resp)
}

Index

Examples

Constants

View Source
const (
	Mozilla = "https://location.services.mozilla.com/v1/geolocate"
	Google  = "https://www.googleapis.com/geolocation/v1/geolocate"
	Yandex  = "http://api.lbs.yandex.net/geolocation"
)

The URL of the location services.

Variables

View Source
var (
	// the maximum waiting time for a response
	RequestTimeout = time.Second * 10
	// do not use definition of by IP address
	IgnoreIPMethod = true
)
View Source
var (
	// invalid data format of the request or a bad key
	ErrBadRequest = errors.New(http.StatusText(http.StatusBadRequest))
	// over the limit of queries
	ErrForbidden = errors.New(http.StatusText(http.StatusForbidden))
	// information not found
	ErrNotFound = errors.New(http.StatusText(http.StatusNotFound))
)

Error returned when a data request is standard service of geolocation.

Functions

This section is empty.

Types

type CellTower

type CellTower struct {
	// The mobile country code.
	MobileCountryCode uint16 `json:"mobileCountryCode"`
	// The mobile network code.
	MobileNetworkCode uint16 `json:"mobileNetworkCode"`
	// The location area code for GSM and WCDMA networks. The tracking area code
	// for LTE networks.
	LocationAreaCode uint16 `json:"locationAreaCode"`
	// The cell id or cell identity.
	CellId uint32 `json:"cellId"`
	// The signal strength for this cell network, either the RSSI or RSCP.
	SignalStrength int16 `json:"signalStrength,omitempty"`
	// The number of milliseconds since this networks was last detected.
	Age uint32 `json:"age,omitempty"`
	// The timing advance value for this cell network.
	TimingAdvance uint8 `json:"timingAdvance,omitempty"`
}

type Fallbacks

type Fallbacks struct {
	// If no exact cell match can be found, fall back from exact cell position
	// estimates to more coarse grained cell location area estimates, rather
	// than going directly to an even worse GeoIP based estimate.
	LAC bool `json:"lacf"`
	// If no position can be estimated based on any of the provided data points,
	// fall back to an estimate based on a GeoIP database based on the senders
	// IP address at the time of the query.
	IP bool `json:"ipf"`
}

type Locator

type Locator interface {
	Get(req Request) (*Response, error)
}

Locator describes the interface supported by all types of location services.

func New

func New(serviceUrl, apiKey string) (locator Locator, err error)

New returns a new initialized the geolocation service.

type Point

type Point struct {
	Lat float64 `json:"lat"`
	Lon float64 `json:"lng"`
}

type Request

type Request struct {
	// The mobile country code stored on the SIM card (100-999).
	HomeMobileCountryCode uint16 `json:"homeMobileCountryCode,omitempty"`
	// The mobile network code stored on the SIM card (0-32767).
	HomeMobileNetworkCode uint16 `json:"homeMobileNetworkCode,omitempty"`
	// The mobile radio type. Supported values are lte, gsm, cdma, and wcdma.
	RadioType string `json:"radioType,omitempty"`
	// The clear text name of the cell carrier / operator.
	Carrier string `json:"carrier,omitempty"`
	// Should the clients IP address be used to locate it, defaults to true.
	ConsiderIp bool `json:"considerIp"`
	// Array of cell towers
	CellTowers []CellTower `json:"cellTowers,omitempty"`
	// Array of wifi access points
	WifiAccessPoints []WifiAccessPoint `json:"wifiAccessPoints,omitempty"`
	// Client IP Address
	IPAddress string `json:"ipaddress,omitempty"`
	// The fallback section is a custom addition to the GLS API.
	Fallbacks *Fallbacks `json:"fallbacks,omitempty"`
}

type Response

type Response struct {
	// The user’s estimated latitude and longitude, in degrees.
	Location Point `json:"location"`
	// The accuracy of the estimated location, in meters.
	Accuracy float64 `json:"accuracy"`
}

type WifiAccessPoint

type WifiAccessPoint struct {
	// The BSSID of the WiFi network.
	MacAddress string `json:"macAddress"`
	// The received signal strength (RSSI) in dBm.
	SignalStrength int16 `json:"signalStrength,omitempty"`
	// The number of milliseconds since this network was last detected.
	Age uint32 `json:"age,omitempty"`
	// The WiFi channel, often 1 - 13 for networks in the 2.4GHz range.
	Channel uint8 `json:"channel,omitempty"`
	// The current signal to noise ratio measured in dB.
	SignalToNoiseRatio uint16 `json:"signalToNoiseRatio,omitempty"`
}

Jump to

Keyboard shortcuts

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