1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

leafletを鍛える。その43

1
Posted at

概要

leafletを鍛えてみた。
geolonia、見つけたのでやってみた。

参考にしたページ

写真

image.png

サンプルコード

import { openReverseGeocoder } from "https://cdn.skypack.dev/@geolonia/open-reverse-geocoder@latest";


const map = L.map("map", {
	center: [35.7021028, 139.755583],
	zoom: 13
});
const baseLayer = L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
	attribution: "&copy; <a href='https://www.openstreetmap.org/copyright'>OpenStreetMap</a> contributors'"
});
baseLayer.addTo(map);

function runReverseGeocode() {
    const center = map.getCenter()
    openReverseGeocoder([center.lng, center.lat]).then(res => {
        if (res && res.code) 
        {
            document.querySelector('#address').innerText = `${res.prefecture}${res.city}${res.oaza || ''}${res.chome || ''}${res.koaza || ''}${res.chiban || ''}`
        } 
        else 
        {
            document.querySelector('#address').innerText = '地図を動かしてください。'
        }
    }).catch(error => {
        alert(error)
        document.querySelector('#address').innerText = '地図を動かしてください。'
    })
}

map.on('moveend', () => {
    runReverseGeocode()
})
map.on('load', () => {
    runReverseGeocode()
})




成果物

以上。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?