前提・実現したいこと
Google maps APi で取得した場所名をInputタグにテキストで表示させたい
Google maps APIのプレイスを利用して、位置情報を取得します。
(いったん東京タワーを検索します。)
取得した場所名を
【HTML】
発生している問題・エラーメッセージ
なし
該当のソースコード
【HTML】 <p> Spot Name <input type="text" id="name" value=""> </p> 【JavaScript】 const getMap = (function() { function codeAddress(address) { // google.maps.Geocoder()コンストラクタのインスタンスを生成 const geocoder = new google.maps.Geocoder(); // 地図表示に関するオプション const mapOptions = { zoom: 25, mapTypeId: google.maps.MapTypeId.ROADMAP }; // 地図を表示させるインスタンスを生成 const map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); //マーカー変数用意 var marker; // geocoder.geocode()メソッドを実行 geocoder.geocode( { 'address': address}, function(results, status) { // ジオコーディングが成功した場合 if (status == google.maps.GeocoderStatus.OK) { // 変換した緯度・経度情報を地図の中心に表示 map.setCenter(results[0].geometry.location); //☆表示している地図上の緯度経度 document.getElementById("lat").value=results[0].geometry.location.lat(); document.getElementById("lng").value=results[0].geometry.location.lng(); // マーカー設定 marker = new google.maps.Marker({ map: map, position: results[0].geometry.location }); } // ジオコーディングが成功しなかった場合 else { console.log('Geocode was not successful for the following reason: ' + status); } }); // マップをクリックで位置変更 map.addListener('click', function(e) { getClickLatLng(e.latLng, map); }); function getClickLatLng(lat_lng, map) { //☆表示している地図上の緯度経度 document.getElementById('lat').value=lat_lng.lat(); document.getElementById('lng').value=lat_lng.lng(); document.getElementById('name').value=lat_lng.name(); // マーカーを設置 marker.setMap(null); marker = new google.maps.Marker({ position: lat_lng, map: map }); // 座標の中心をずらす map.panTo(lat_lng); } } //inputのvalueで検索して地図を表示 return { getAddress: function() { // ボタンに指定したid要素を取得 const button = document.getElementById("map_button"); // ボタンが押された時の処理 button.onclick = function() { // フォームに入力された住所情報を取得 const address = document.getElementById("address").value; // 取得した住所を引数に指定してcodeAddress()関数を実行 codeAddress(address); } //読み込まれたときに地図を表示 window.onload = function(){ // フォームに入力された住所情報を取得 const address = document.getElementById("address").value; // 取得した住所を引数に指定してcodeAddress()関数を実行 codeAddress(address); } } }; })(); getMap.getAddress();
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
JavaScript
HTML5
CSS3
あなたの回答
tips
プレビュー