前提
map上に移動しているルートの表示と同一のmap上にクリックした場所にピンを立てるアプリを作成したいのですが
BingMaps GO! と BmapQuery.js に記載されているコードを組み合わせて、上記の機能を実装しようとしましたが、
ルートの取得を開始する("Start Tracking"をクリック)と以下のエラーが表示され現在地の取得ができずルートを表示することができません。
上記の2つのコードを組み合わせてmap上をクリックした場所にピンを立てることはできました。
(リンク先に参考にしたコードが記載されております。)
恐れ入りますが、どなたかアドバイスをいただけないでしょうか?
実現したいこと
現在地を取得しmap上に移動したルートの表示とクリックした場所にピンを立てたいです
発生している問題・エラーメッセージ
Uncaught TypeError: Cannot read properties of null (reading 'entities') at BmapQuery.js:82:71 Uncaught TypeError: Cannot read properties of null (reading 'setView') at BmapQuery.js:83:37
該当のソースコード
HTML
1<!DOCTYPE html> 2<html lang="en"> 3<head> 4<meta charset="UTF-8"> 5<title>New</title> 6<style>html,body{height:100%;}body{padding:0;margin:0;background:#333;}h1{padding:0;margin:0;font-size:50%;color:white;}p{margin:0}button{width:60px;height:60px;background-color: #0B6125;color:white;}#inputForm {position:absolute;top:600px;left:250px;padding:10px;background-color:white;border:1px solid #000;border-radius:10px;}</style> 7</head> 8<body> 9 10 11<!-- MAP[START] --> 12<div id="myMap" style="width:100%;height:80%;"></div> 13 14<button id="start_tracking">Start Tracking</button> 15<button id="stop_tracking">Stop Tracking</button> 16<button id="clear_map">Map Clear</button> 17 18<div id="inputForm" style="display:none;"> 19 <table> 20 <tr><td colspan="2"></td></tr> 21 <tr><td>Title</td><td><input id="titleTbx" type="text" /></td></tr> 22 <tr><td>Description</td><td><input id="descriptionTbx" type="text" /></td></tr> 23 <tr><td colspan="2"><input type="button" value="Save" onclick="saveData()" style="float:right;"/></td></tr> 24 </table> 25</div> 26<!-- MAP[END] --> 27 28<div id="speed">0</div> 29 30<script src='https://www.bing.com/api/maps/mapcontrol?callback=GetMap&key=[Your BingMapsAPI key]' async defer></script> 31<script src="BmapQuery.js"></script> 32<script> 33 let map, infobox, currentPushpin; 34 function GetMap() { 35 map1 = new Microsoft.Maps.Map('#myMap', {}); 36 37 // Instance 38 const map = new Bmap("#myMap"); 39 Microsoft.Maps.Events.addHandler(map1, 'click', mapClicked); 40 41 // Tracking Event & Draw 42 //Start 43 document.getElementById("start_tracking").onclick=function(){ 44 map.startTrackingDraw("#ff0000", 3, "#speed", true); 45 }; 46 //Stop 47 document.getElementById("stop_tracking").onclick=function(){ 48 map.stopTrackingDraw(); 49 console.log("TrackData:", map1.getTrackingData() ); //option: Get tracking data[Array]. 50 console.log("TrackTime:", map1.getTrackingTime() ); //option: Get tracking Time data[Array]. 51 }; 52 //Clear Map1. 53 document.getElementById("clear_map").onclick=function(){ 54 map.clearMap(); //Clear Map1. 55 map.clearTrackingData(); //Delete track&Speed data 56 } 57 } 58 59 60 function mapClicked(e) { 61 //Create a pushpin. 62 currentPushpin = new Microsoft.Maps.Pushpin(e.location); 63 //Add a click event to the pushpin. 64 Microsoft.Maps.Events.addHandler(currentPushpin, 'click', pushpinClicked); 65 //Add the pushpin to the map. 66 map1.entities.push(currentPushpin); 67 //Open up an input form here the user can enter in details for pushpin. 68 document.getElementById('inputForm').style.display = ''; 69 } 70 function saveData() { 71 //Get the data from form and add it to the pushpin 72 currentPushpin.metadata = { 73 title: document.getElementById('titleTbx').value, 74 description: document.getElementById('descriptionTbx').value 75 76 }; 77 //Optionally save this data somewhere (like a database or local storage). 78 localStorage.setItem(document.getElementById('titleTbx').value,document.getElementById('descriptionTbx').value); 79 80 //Clear the fields in the form and then hide the form. 81 document.getElementById('titleTbx').value = ''; 82 document.getElementById('descriptionTbx').value = ''; 83 document.getElementById('inputForm').style.display = 'none'; 84 } 85 function pushpinClicked(e) { 86 if (e.target.metadata) { 87 infobox.setOptions({ 88 location: e.target.getLocation(), 89 title: e.target.metadata.title, 90 description: e.target.metadata.description, 91 visible: true 92 }); 93 } 94 } 95 96</script> 97</body> 98</html>
BmapQuery.jsはGitHubに上がっているファイルをそのまま使用しています。
補足情報(FW/ツールのバージョンなど)
参考にしたコード
・BingMaps GO!のPushpin項目のPushpin:Add Metadata
・BmapQuery.jsのPolyline & Tracking項目のRealtimeTracking(LogData & Draw)
あなたの回答
tips
プレビュー