逆ジオコーディングを実装したいと考えております。
そこで、参考になるサイトを見つけたので流用したのですが、エラーがでてうまく動きません。
【エラーメッセージ】
Notice: Undefined offset: 0 ファイル改装 on line 31
調べてみたのですが、うまくいかずに手に負えない状態です。
どなたか力をお貸し下さいませんでしょうか。
php
1<?php 2 3/* 4** 緯度経度取得及び設定 5*/ 6$latlngs = array(); 7 8$latlngs = [ 9 ['35.7202869', '139.9249688'], 10 ['35.71148684139082', '139.76839363574982'], 11 ['35.72546314632721', '139.7471398115158'], 12 ['35.738426818811796', '139.73446369171143'], 13 ['35.76239773757285', '139.74768161773682'] 14]; 15 16/* 17** jsonデータ取得 18*/ 19$GMADs = array(); 20 21foreach ($latlngs as $latlng) { 22 $GMADs[] = json_decode(@file_get_contents('http://maps.google.com/maps/api/geocode/json?latlng=' . $latlng[0] . ',' . $latlng[1] . '&sensor=false&language=ja'), true); 23} 24 25/* 26** 住所取得 27*/ 28$addresses = array(); 29 30foreach ($GMADs as $GMAD) { 31 $addresses[] = $GMAD['results'][0]['formatted_address']; 32} 33 34print('<pre>'); 35print_r($addresses); 36print('</pre>'); 37 ?> 38 39 40<!DOCTYPE html> 41<html> 42 <head> 43 <meta charset="UTF-8"> 44 <meta name="robots" content="noindex,nofollow"> 45 46 <!-- ビューポートの設定 --> 47 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 48 49 <!-- スタイルシートの読み込み --> 50 <link href="geolocation-api.css" rel="stylesheet"> 51 52 <title>[getCurrentPosition()]のサンプルデモ (Geolocation API)</title> 53 54 </head> 55<body> 56 57 58 59<h1 style="text-align:center;color:#d36015;">[getCurrentPosition()]のサンプルデモ (Geolocation API)</h1> 60<p>Geolocation APIの[getCurrentPosition()]を利用して、ユーザーの現在位置を取得するサンプルデモです。</p> 61 62<HR style="margin: 3em 0 ;"> 63 64<h2>取得したデータ</h2> 65 66 <p>下記の位置情報を取得することができました。</p> 67 68 <dl id="result"></dl> 69 70<h2>地図</h2> 71 72 <p>Google Mapsに、位置情報を反映させたものです。</p> 73 74 <div class="map-wrapper"> 75 <div id="map-canvas"></div> 76 </div> 77 78 79<HR style="margin: 3em 0 ;"> 80 81<p style="text-align:center"><a href="https://syncer.jp/how-to-use-geolocation-api">配布元: Syncer</a></p> 82 83 84<!-- JavaScriptの読み込み --> 85<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCoSPFxiO5fyFolM7lcTMIEN593fEFyO78"></script> 86<script src="get-current-position.js"></script> 87 88</body> 89</html> 90