解決課題
googlemapのgeocoder apiを使って検索した場所の表示と緯度、経度を取得し
その後、ヒュべニの公式を使い2点間の距離を測るプログラムを作成したいのですが
緯度、経度をそれぞれのdiv要素に取得したいのですがその部分で詰まってしまいました。
コードはgoogleapiのgeocoderページのサンプルコードを参考として改変しています。
実行環境:googleChrome
プログラムのコード
<!DOCTYPE html> <html> <head> <title>Geocoding service</title> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <meta charset="utf-8"> <style> html, body { height: 100%; margin: 0; padding: 0; } #map { height: 500px; width: 50%; } #floating-panel { position: absolute; top: 0px; left: 50%; z-index: 5; background-color: #fff; padding: 5px; border: 1px solid #999; text-align: center; font-family: 'Roboto','sans-serif'; line-height: 30px; padding-left: 10px; } #addressExplain { position: relative; top : 0px; left: 0px; font-size: 'sans-serif'; } </style> </head> <body> <div id="floating-panel"> <p id="addressExplain">住所を入力</p> <input id="address" type="textbox" value="東京"> <input id="submit" type="button" value="Geocode"> </div> <div id="mySchool"> <p>産技短の緯度</p> <input id="shonaiCitLatitude" type="text" value="38.876127" disabled="true"> <p>経度は</p> <input id="shonaiCitLongitude" type="text" value="139.834633" disabled="true"> </div> <div id="searchPlace"> <p>検索地の緯度</p> <input id="searchPlaseLatitude" type="text" value=""> <p>経度は</p> <input id="searchPlaceLongitude" type="text" value=""> </div> <div id="map"></div> <script src="https://maps.googleapis.com/maps/api/js"></script> <script> function initMap() { var myLatLng = {lat: 35.658581, lng: 139.745433}; var map = new google.maps.Map(document.getElementById('map'), { zoom: 15, center: myLatLng }); var marker = new google.maps.Marker({ position: myLatLng, map: map }); var geocoder = new google.maps.Geocoder(); document.getElementById('submit').addEventListener('click', function() { geocodeAddress(geocoder, map); }); } function geocodeAddress(geocoder, resultsMap) { var address = document.getElementById('address').value; var latitude = document.getElementById('searchPlaseLatitude').value; var longitude = document.getElementById('searchPlaceLongitude').value; geocoder.geocode({'address': address}, function(results, status) { if (status === google.maps.GeocoderStatus.OK) { resultsMap.setCenter(results[0].geometry.location); var marker = new google.maps.Marker({ map: resultsMap, position: results[0].geometry.location }); // blockに緯度を渡す latitude = geocoder.geocode.results[0].geometry.location.lat; // blockに経度を渡す longitude = geocoder.geocode.results[0].geometry.location.lng; } else { alert('Geocode was not successful for the following reason: ' + status); } }); } initMap(); </script> </body> </html>
エラー
geocoderSample.html:93 Uncaught TypeError: Cannot read property '0' of undefined at geocoderSample.html:93 at geocoder.js:5 at geocoder.js:3 at YU (geocoder.js:2) at Zm.<anonymous> (geocoder.js:3) at common.js:55 at nn.f (common.js:14) at Zm.j (common.js:55) at Object.c [as _dbvt6f] (common.js:48) at GeocodeService.Search?4s東京&7sUS&9sja&callback=_xdc_._dbvt6f&token=99712:1
回答1件
あなたの回答
tips
プレビュー