8
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?

ポッキー何本分?目的地までの距離をポッキー換算するアプリを作ってみた

Posted at

1. ポッキーはお好きですか?

皆さんポッキーは好きですか?
私は片手で食べやすく作業しながら食べれるので好きです。

面積を表すのに「東京ドーム◯個分」という風に表すことがあると思います。
今回は目的地までの直線距離をポッキー何本分かで表すアプリを作ってみました。

2. できたもの

こちらで遊べるので、よければ触ってみてください。

下のように現在地と目的地を入力するとポッキー何本分かで示してくれます!
すごくいいですね!

Videotogif (16).gif

3. 技術的ポイント

2点間の距離計算について
地球は球体なので、単純な引き算では正確な距離が出ません。ハーバーサイン公式を使って、2つの緯度経度から大圏距離(球面上の最短距離)を計算しています。

function haversineDistance(lat1, lon1, lat2, lon2) {
    const R = 6371000; // 地球の半径(メートル)
    
    const phi1 = lat1 * Math.PI / 180;
    const phi2 = lat2 * Math.PI / 180;
    const deltaPhi = (lat2 - lat1) * Math.PI / 180;
    const deltaLambda = (lon2 - lon1) * Math.PI / 180;
    
    const a = Math.sin(deltaPhi / 2) ** 2 +
              Math.cos(phi1) * Math.cos(phi2) * Math.sin(deltaLambda / 2) ** 2;
    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    
    return R * c; // メートル単位で返す
}

4. まとめ

今回は目的地までの直線距離をポッキーで表示してみました。
わかりにくいので皆さんはメートルで調べましょう!

8
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
8
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?