1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

緯度,経度およびZoomからOpenStreetMapのタイルを得る

Last updated at Posted at 2023-10-14

緯度,経度およびZoomからOpenStreetMapのタイルを得るサービスを作りました。
https://elix.jp/app/geotilefinder/

image.png

緯度経度はGoogleMapsの任意の場所で右クリックするとコピーできます。
image.png

また、providerを変更できるようになっています。
image.png

緯度経度およびzoomからタイルを得る処理はこんな感じです。

function latLngToTileXY(lat, lng, zoom) {
    var lat_rad = lat * Math.PI / 180;
    var n = Math.pow(2, zoom);
    var xtile = parseInt((lng + 180.0) / 360.0 * n);
    var ytile = parseInt((1.0 - Math.log(Math.tan(lat_rad) + 1.0 / Math.cos(lat_rad)) / Math.PI) / 2.0 * n);
    return [xtile, ytile, zoom];
}

追記:
ついでにタイルの範囲を出力するようにしました。
image.png

ソースコードはほとんどGithub copilotが書いてくれました

// タイル座標とズームからタイル左下と右上の緯度経度を求める
function tileXYToLatLng(xtile, ytile, zoom) {
    var n = Math.pow(2, zoom);
    var lng_deg = xtile / n * 360.0 - 180.0;
    var lat_rad = Math.atan(Math.sinh(Math.PI * (1 - 2 * (ytile+1) / n)));
    var lat_deg = lat_rad * 180.0 / Math.PI;
    var lng_deg2 = (xtile+1) / n * 360.0 - 180.0;
    var lat_rad2 = Math.atan(Math.sinh(Math.PI * (1 - 2 * ytile / n)));
    var lat_deg2 = lat_rad2 * 180.0 / Math.PI;
    iResultSW.value = lat_deg + "," + lng_deg;
    iResultNE.value = lat_deg2 + "," + lng_deg2;
    return [lat_deg, lng_deg, lat_deg2, lng_deg2];
}

Github copilotに「ありがとう」を伝えるすべはないものか・・・。
chatGPTやAlexaなら直接「ありがとう」でつたわるんだけどなー。

1
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?