質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Go

Go(golang)は、Googleで開発されたオープンソースのプログラミング言語です。

Google マップ

Google Mapは、Google社がオンラインで提供している地図・ローカル検索サービスです。GIS(Geographic Information System:地理情報システム)の中の「WebGIS」に該当します。地図・航空写真・地形の表示方式があり、それぞれユーザーが縮尺を調整して表示させることができます。地域の情報サービスを検索する機能やルート検索の機能も搭載されています。

Q&A

解決済

1回答

333閲覧

googlemaps.github.ioのGeocode関数から特定の値を取得する方法

tororo_umai

総合スコア7

Go

Go(golang)は、Googleで開発されたオープンソースのプログラミング言語です。

Google マップ

Google Mapは、Google社がオンラインで提供している地図・ローカル検索サービスです。GIS(Geographic Information System:地理情報システム)の中の「WebGIS」に該当します。地図・航空写真・地形の表示方式があり、それぞれユーザーが縮尺を調整して表示させることができます。地域の情報サービスを検索する機能やルート検索の機能も搭載されています。

0グッド

0クリップ

投稿2018/04/02 04:26

前提・実現したいこと

googlemaps.github.ioのGeocode関数を用いて、
Geometry.Location.LatGeometry.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)

その他

初歩的な質問で申し訳ございませんが、
ご回答のほどよろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

respはスライスなのでフィールドを持ちえません。
そのようなアクセスの仕方は出来ません。
(という事をエラーメッセージがちゃんと教えてくれています。)
スライスの要素にアクセスするなら

go

1geoResult := resp[0]

というように要素を取り出してください。

投稿2018/04/03 11:01

nobonobo

総合スコア3367

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tororo_umai

2018/04/04 12:14

ご回答くださりありがとうございます。 教えてくださったように、変数に入れると取得できました。 エラーメッセージの意味をちゃんと理解できてなかったです。 フィールドとスライスの使い方や違いについて、もう一度学びなおします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問