Created
February 8, 2014 02:34
-
-
Save tigawa/8875806 to your computer and use it in GitHub Desktop.
GoogleMap上にマークをつける方法及び、二つの地点の距離をm単位で返す関数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (var k = 0; k < data.length; k++) { | |
var target_p = new google.maps.LatLng(data[k][3], data[k][4]); | |
var m = getDistance( current_p, target_p); | |
if( m <= 1000 ){ | |
mark( target_p ); | |
} | |
} | |
function mark( target_p ){ | |
var marker = new google.maps.Marker({ | |
position: target_p, | |
map: map}); | |
marker.setMap(map); | |
} | |
/** | |
* 2つの経度緯度から距離(単位:メートル)を算出する。 | |
* ※ fromとtoはLatLngクラス | |
*/ | |
function getDistance(from, to) { | |
if (!from || !to) { | |
return 0; | |
} | |
var R = 6371; // Radius of the Earth in km | |
var dLat = (to.lat() - from.lat()) * Math.PI / 180; | |
var dLon = (to.lng() - from.lng()) * Math.PI / 180; | |
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + | |
Math.cos(from.lat() * Math.PI / 180) * Math.cos(to.lat() * Math.PI / 180) * Math.sin(dLon / 2) * Math.sin(dLon / 2); | |
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); | |
var d = R * c; | |
//単位はキロメートル。メートルにするときは1000掛けること | |
return (d * 1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment