▽1
●
a-kuma3 ●1200ポイント ベストアンサー |
Google Maps API の Geocoder クラスというのを使うと住所から緯度、経度などを取得できます。
https://developers.google.com/maps/documentation/javascript/reference?hl=ja#Geocoder
「google maps api 住所 緯度 経度」という感じでググると、サンプルソースを置いてあるところが見つかります。
などなど
質問にあるコードの断片は、google.maps.Marker への引数でしょうか。
エラー処理などをバッサリと省き、エッセンスだけを抜き出すと、こんな感じになります(php の埋め込みはありますが、基本 javascript です)。
var map = ... var geocoder = new google.maps.Geocoder(); geocoder.geocode({ address: "<?php echo post_custom('address') ?>" // カスタムフィールド address の値 }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { // 緯度、経度(検索結果のひとつ目) var latlng = results[0].geometry.location; // 緯度、経度を指定して Marker を作成 var marker = new google.maps.Marker({ position: latlng, map: map }); ... } });
ちょっと気を付けなければいけないのは、以下の 2点。
Web API も用意されてます。
https://developers.google.com/maps/documentation/geocoding/intro?hl=ja#GeocodingRequests
こんなリクエストをと送ると、XML で京都駅の位置情報などが取得できます(%E4%BA%AC%E9%83%BD%E9%A7%85 は、京都駅を URL Encode したものです)。
http://maps.google.com/maps/api/geocode/xml?address=%E4%BA%AC%E9%83%BD%E9%A7%85
応答はこんな感じ。
<GeocodeResponse> <status>OK</status> <result> ... <geometry> <location> <lat>34.9858490</lat> <lng>135.7587667</lng> </location> ... </geometry> </result> </GeocodeResponse>
# 先のマニュアルにあるように、JSON 推奨ですが。
こちらを使えば、file_get_contents() を使って、PHP 側で取得することも可能です。
複数の検索結果が返ってきたり、実在する住所でも検索結果が返ってこないこともある、というところは javascript の API と同じです。