How can I get the nearest city to geo-coordinates with Go?

huangapple go评论96阅读模式
英文:

How can I get the nearest city to geo-coordinates with Go?

问题

如何使用Go语言从坐标(例如49.014,8.4043)获取地理位置(例如最近的城市)?

我尝试使用golang-geo

package main

import (
	"log"

	"github.com/kellydunn/golang-geo"
)

func main() {
	p := geo.NewPoint(49.014, 8.4043)
	geocoder := new(geo.GoogleGeocoder)
	geo.HandleWithSQL()
	res, err := geocoder.ReverseGeocode(p)
	if err != nil {
		log.Println(err)
	}
	log.Println(string(res))
}

但它返回的是Schloßplatz 23, 76131 Karlsruhe, Germany。我只想要Karlsruhe(即只有城市)。

如何只获取城市信息?

英文:

How do I get a geolocation (e.g. the nearest city) from coordinates (e.g. 49.014,8.4043) with Go?

I tried to use golang-geo:

package main

import (
	"log"

	"github.com/kellydunn/golang-geo"
)

func main() {
	p := geo.NewPoint(49.014, 8.4043)
	geocoder := new(geo.GoogleGeocoder)
	geo.HandleWithSQL()
	res, err := geocoder.ReverseGeocode(p)
	if err != nil {
		log.Println(err)
	}
	log.Println(string(res))
}

but it gives Schloßplatz 23, 76131 Karlsruhe, Germany. I would like
Karlsruhe (so: only the city).

How do I get only the city?

答案1

得分: 5

你要提取的数据并不直接从库中返回。但是,你可以发送请求并解析JSON响应,以提取城市而不是完整地址:

package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/kellydunn/golang-geo"
)

type googleGeocodeResponse struct {
	Results []struct {
		AddressComponents []struct {
			LongName  string   `json:"long_name"`
			Types     []string `json:"types"`
		} `json:"address_components"`
	}
}

func main() {
	p := geo.NewPoint(49.014, 8.4043)
	geocoder := new(geo.GoogleGeocoder)
	geo.HandleWithSQL()
	data, err := geocoder.Request(fmt.Sprintf("latlng=%f,%f", p.Lat(), p.Lng()))
	if err != nil {
		log.Println(err)
	}
	var res googleGeocodeResponse
	if err := json.Unmarshal(data, &res); err != nil {
		log.Println(err)
	}
	var city string
	if len(res.Results) > 0 {
		r := res.Results[0]
	outer:
		for _, comp := range r.AddressComponents {
			// See https://developers.google.com/maps/documentation/geocoding/#Types
			// for address types
			for _, compType := range comp.Types {
				if compType == "locality" {
					city = comp.LongName
					break outer
				}
			}
		}
	}
	fmt.Printf("City: %s\n", city)
}

希望对你有帮助!

英文:

The data you are looking to extract is not returned directly from the library. You can, however, perform a request and parse the JSON response yourself to extract the city, rather than the full address:

package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/kellydunn/golang-geo"
)

type googleGeocodeResponse struct {
	Results []struct {
		AddressComponents []struct {
			LongName  string   `json:"long_name"`
			Types     []string `json:"types"`
		} `json:"address_components"`
	}
}

func main() {
	p := geo.NewPoint(49.014, 8.4043)
	geocoder := new(geo.GoogleGeocoder)
	geo.HandleWithSQL()
	data, err := geocoder.Request(fmt.Sprintf("latlng=%f,%f", p.Lat(), p.Lng()))
	if err != nil {
		log.Println(err)
	}
	var res googleGeocodeResponse
	if err := json.Unmarshal(data, &res); err != nil {
		log.Println(err)
	}
	var city string
	if len(res.Results) > 0 {
		r := res.Results[0]
	outer:
		for _, comp := range r.AddressComponents {
            // See https://developers.google.com/maps/documentation/geocoding/#Types
            // for address types
			for _, compType := range comp.Types {
				if compType == "locality" {
					city = comp.LongName
					break outer
				}
			}
		}
	}
	fmt.Printf("City: %s\n", city)
}

答案2

得分: 2

这个库的Geocoder接口的文档中提到(重点在于):

> ... 反向地理编码应该接受一个指向Point的指针,并返回最接近它的街道地址

所以你要么从街道地址中解析出城市名(这本身就是一个挑战),要么找一个能明确提供城市信息的不同的地理编码库。

英文:

The documentation for the Geocoder interface from that library says (emphasis mine):

> ... Reverse geocoding should accept a pointer to a Point, and return the street address that most closely represents it.

So you'll have to either parse the city name from the street address (which is its own challenge) or find a different geocoder library that provides a city explicitly.

答案3

得分: 1

golang-geo的作者在这里。

对于那些关注这个stackoverflow问题的人,我已经在问题#31的回答中回答了@moose的主要问题。

简而言之,对于这个问题的答案是,虽然Google Geocoding API支持获取不同精度级别的模糊概念,但截至目前,golang-geo尚未实现这一功能。

英文:

Author of golang-geo here.

For those following along with this stack overflow question, I've answered @moose 's primary question in issue #31 here.

The tl;dr answer to this question is that while the Google Geocoding APIs do support some fuzzy notion of getting different levels of precision, it has yet to be implemented in golang-geo to date.

huangapple
  • 本文由 发表于 2015年3月12日 06:37:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/28998709.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定