let map = L.map('map').fitWorld(); L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors', maxZoom: 18, tileSize: 512, zoomOffset: -1 }).addTo(map); map.locate({setView: true, maxZoom: 16}); let threshold = 50; //50メートル以内に設定 let id,marker,circle; // マーカーと緯度経度の設定 let a = L.marker([38.89232, 139.82221]).addTo(map); a = L.latLng(38.89232, 139.82221); let b = L.marker([38.89348, 139.81946]).addTo(map); b = L.latLng(38.89348, 139.81946); let c = L.marker([38.8936, 139.82063]).addTo(map); c = L.latLng(38.8936, 139.82063); function onLocationFound(e) { let radius = e.accuracy/2; if (marker) map.removeLayer(marker); //マーカー削除 marker = L.marker(e.latlng).addTo(map); //マーカー追加 marker.bindPopup('現在地').openPopup(); if (circle) map.removeLayer(circle); circle = L.circle(e.latlng, radius).addTo(map); map.setView(e.latlng); // 現在地と目的地の距離を計測 let d = a.distanceTo(e.latlng); let g = b.distanceTo(e.latlng); let f = c.distanceTo(e.latlng); // 50メートル以内に近づいた時の処理 if (d < threshold) { alert("おはよう"); } else if(g < threshold){ alert("こんにちわ"); } else if (f < threshold){ alert("こんばんわ"); } }; // 位置情報取得できなかったときの処理 function onLocationError(e) { alert(e.message); } // 位置情報を持ってくる関数 function watchFound(e) { onLocationFound({ latlng: L.latLng([ e.coords.latitude, e.coords.longitude]), accuracy: e.coords.accuracy}); } // id = navigator.geolocation.watchPosition(watchFound,onLocationError); let $start = document.getElementById('start'); // スタートボタンクリック時の位置情報を定期的に取得 $start.addEventListener("click", function(e) { this.setAttribute("disabled", true); id = navigator.geolocation.watchPosition(watchFound,onLocationError); }); // 位置情報取得中止 let $stop = document.getElementById('stop') $stop.addEventListener("click", function(e) { $start.removeAttribute("disabled"); navigator.geolocation.clearWatch(id); }); // マップクリックしたら経度、緯度の表示 let popup = L.popup(); function onMapClick(e) { popup .setLatLng(e.latlng) .setContent("You clicked the map at " + e.latlng.toString()) .openOn(map); } map.on('click', onMapClick); // setViewまたはcenterで最初に表示される中心位置を設定できる // zoomで最初に表示される近さを設定できる // .bindPopupメソッドでHTMLコンテンツを含むポップアップを表示する // .popup()で表示されるコンテンツを設定できる // .latlngでその場所の経度、を緯度調べられる // distanceTo(<LatLng> otherLatLng)で距離が図れる // LatLng.distanceTo(otherLatLng)で書いたらうまく行った // let map = L.map('map').setView([38.89232, 139.82221], 15); // L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { // attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' // }).addTo(map);