46
48

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 3 years have passed since last update.

地球上の2点間の直線距離を求める

Last updated at Posted at 2015-08-01

※新しい内容をこちらに移植しました。 → 【JavaScript】地球上の2地点間の距離を取得するアルゴリズム比較

#概要

  • GPSを使って地球上の2点間(新宿と現地点)の直線距離を求める
  • GeoLocationAPIを利用してみた。
  • 球面三角法を利用

参考

【JavaScript】地球上の2地点間の距離を取得するアルゴリズム比較
地球を測ろう
測量計算サイト

#ソース

index.js


//2点間を計算する関数
var getSuccess = function(pos) {
    //現在地 lat:緯度,lng:経度
    var geo = {
        lat: pos.coords.latitude,
        lng: pos.coords.longitude
    };
    //現在地の緯度経度
    var lat1 = geo.lat;
    var lng1 = geo.lng;

    /*新宿の座標*/
    var lat2 = 35.689487;
    var lng2 = 139.691706;

    //距離の計算//
    function getDistance(lat1, lng1, lat2, lng2) {

       function radians(deg){
          return deg * Math.PI / 180;
       }

       return 6378.14 * Math.acos(Math.cos(radians(lat1))* 
        Math.cos(radians(lat2))*
        Math.cos(radians(lng2)-radians(lng1))+
        Math.sin(radians(lat1))*
        Math.sin(radians(lat2)));
    }
    /*結果*/
    alert(getDistance(lat1,lng1,lat2,lng2));
};

//GPSエラーの場合
var geoError = function() {
    alert('Getting location failed.');//
};

/*GeoLocationAPIで現在地の座標を取得する*/
document.getElementById("start").onclick = function(){
    navigator.geolocation.getCurrentPosition(getSuccess, geoError, { enableHighAccuracy: true });
};

追記 2018/09/10

【JavaScript】地球上の2地点間の距離を取得するアルゴリズム比較

46
48
1

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
46
48

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?