前提・実現したいこと
googlemaps.github.ioのGeocode関数を用いて、
Geometry.Location.Lat
とGeometry.Location.Lng
の値を取得したいです。
発生している問題
該当ソースコードのmain.go
において、
resp.Geometry.Location.Lat
をすればいいのかなと思いましたが、
以下のエラーが出ました。
resp.Geometry undefined (type []maps.GeocodingResult has no field or method Geometry)
該当のソースコード
↓ main.go
go
1package main 2 3import ( 4 "fmt" 5 6 "golang.org/x/net/context" 7 "googlemaps.github.io/maps" 8) 9 10func main() { 11 c, err := maps.NewClient(maps.WithAPIKey("API_KEY")) 12 if err != nil { 13 fmt.Errorf("fatal error: %s", err) 14 } 15 16 address := &maps.GeocodingRequest{ 17 Address: "東京駅", 18 } 19 20 resp, err := c.Geocode(context.Background(), address) 21 if err != nil { 22 fmt.Printf("Geocode err: %v\n", err) 23 return 24 } 25 fmt.Printf("OK. resp: %+v\n", resp) 26 fmt.Printf("OK. resp: %+v\n", resp.Geometry.Location.Lat) 27}
↓ googlemaps.github.io/maps/geocoding.go(一部引用)
go
1package maps 2 3import ( 4 "errors" 5 "net/url" 6 "strings" 7 8 "golang.org/x/net/context" 9) 10 11var geocodingAPI = &apiConfig{ 12 host: "https://maps.googleapis.com", 13 path: "/maps/api/geocode/json", 14 acceptsClientID: true, 15} 16 17// Geocode makes a Geocoding API request 18func (c *Client) Geocode(ctx context.Context, r *GeocodingRequest) ([]GeocodingResult, error) { 19 if r.Address == "" && len(r.Components) == 0 && r.LatLng == nil { 20 return nil, errors.New("maps: address, components and LatLng are all missing") 21 } 22 23 var response struct { 24 Results []GeocodingResult `json:"results"` 25 commonResponse 26 } 27 28 if err := c.getJSON(ctx, geocodingAPI, r, &response); err != nil { 29 return nil, err 30 } 31 32 if response.Status == "ZERO_RESULTS" { 33 return []GeocodingResult{}, nil 34 } 35 36 if err := response.StatusError(); err != nil { 37 return nil, err 38 } 39 40 return response.Results, nil 41} 42 43// GeocodingResult is a single geocoded address 44type GeocodingResult struct { 45 AddressComponents []AddressComponent `json:"address_components"` 46 FormattedAddress string `json:"formatted_address"` 47 Geometry AddressGeometry `json:"geometry"` 48 Types []string `json:"types"` 49 PlaceID string `json:"place_id"` 50} 51 52// AddressGeometry is the location of a an address 53type AddressGeometry struct { 54 Location LatLng `json:"location"` 55 LocationType string `json:"location_type"` 56 Bounds LatLngBounds `json:"bounds"` 57 Viewport LatLngBounds `json:"viewport"` 58 Types []string `json:"types"` 59} 60 61// LatLng represents a location on the Earth. 62type LatLng struct { 63 Lat float64 `json:"lat"` 64 Lng float64 `json:"lng"` 65}
補足情報(FW/ツールのバージョンなど)
OS: Windows 10 Home
開発環境: LiteIDE X(ver. X32.2)
その他
初歩的な質問で申し訳ございませんが、
ご回答のほどよろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/04 12:14