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

自闭症网瘾萝莉.ら 提交于 2019-12-06 09:27:15

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)
}

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!