Cách lấy khoảng cách bằng mét của 2 vị trí trong google map.
Kết quả:
Location 1:-33.8668283734,151.20648918209997
Location 2:-34.8668283734,151.20648918209997
Range Meter(m):111195(m)
Source 1 file html
<?xml version="1.0"?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Google 2 location Distance Meter</title>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places,visualization&v=3.exp"
type="text/javascript"></script>
<script type="text/javascript">
var location1 = new google.maps.LatLng(-33.8668283734, 151.2064891821);
var location2 = new google.maps.LatLng(-34.8668283734, 151.2064891821);
function initialize() {
var range=distance(location1,location2);
//alert(range);
var outputDiv = document.getElementById('outputDiv');
var html='Location 1:'+location1.lat()+','+location1.lng()+'<br/>';
html=html+'Location 2:'+location2.lat()+','+location2.lng()+'<br/>';
html=html+'Range Meter(m):'+range+'(m)';
outputDiv.innerHTML = html;
}
//Fucntion Distance
function distance(location1,location2) {
var lat1=location1.lat();
var lon1=location1.lng();
var lat2=location2.lat();
var lon2=location2.lng();
var R = 6371; // km (change this constant to get miles)
var dLat = (lat2-lat1) * Math.PI / 180;
var dLon = (lon2-lon1) * Math.PI / 180;
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1 * Math.PI / 180 ) * Math.cos(lat2 * 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;
//if (d>1) return Math.round(d)+"km";
//else if (d<=1)
return Math.round(d*1000);//meter
//return d;
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="outputDiv"></div>
</body>
</html>
Source file https://drive.google.com/file/d/0B785Epf6q5XlLTR0QnMyaDNpb2M/edit?usp=sharing
Chủ Nhật, 5 tháng 1, 2014
Đăng ký:
Đăng Nhận xét (Atom)
0 nhận xét:
Đăng nhận xét