google api v3距离计算

时间:2014-12-19 19:15:55

标签: javascript google-maps google-maps-api-3

我需要计算从我的位置到页面上其他5个标记的距离,并在点击按钮后打印它"按最小距离排序"并按最小距离对其进行排序。

<div class="row"id="googleMap">
<div class="col-md-9" id="wrapper">
<article></article>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script>
var map;
function success(position) {
    var mapcanvas = document.createElement('div');
    mapcanvas.id = 'mapcontainer';
    mapcanvas.style.height = '550px';
    mapcanvas.style.width = '960px';

    document.querySelector('article').appendChild(mapcanvas);

    var coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

    var options = {
      zoom: 8,
      center: coords,
      mapTypeControl: false,
      navigationControlOptions: {
          style: google.maps.NavigationControlStyle.SMALL
      },
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("mapcontainer"), options);

    var marker = new google.maps.Marker({
      position: coords,
      map: map,
      title: "My location"
    });

    if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(success);
} else {
    error('Geo Location is not supported');
}
//other locations
var locations = [
    ['Banja Luka', 44.766666699999990000, 17.183333299999960000, 4],
    ['Tuzla', 44.532841000000000000, 18.670499999999947000, 5],
    ['Zenica', 44.203439200000000000, 17.907743200000027000, 3],
    ['Sarajevo', 43.850000000000000000, 18.250000000000000000, 2],
    ['Mostar', 43.333333300000000000, 17.799999999999954000, 1]
];

var infowindow = new google.maps.InfoWindow();

var marker, i;
for (i = 0; i < locations.length; i++) {
    marker = new google.maps.Marker({
      position: new google.maps.LatLng(locations[i][1], locations[i][2]),
      map: map
    });

    google.maps.event.addListener(marker, 'click', (function (marker, i) {
      return function () {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
      }
    })(marker, i));
}

}//kraj funckcije


//centriranje mape
var position = {
    coords: {
        latitude: 44.766666699999990000,
        longitude: 17.183333299999960000
    }
}

success(position);
//here to print by smalest distance to largest 
function Lokacije() {

        div = document.getElementById('NajblizeLokacije');
        div.style.display = "block";

    }

</script>
</div>
<div class="col-md-3 well zaMape">
<button onclick="Lokacije()" id="singlebutton" name="singlebutton" class="btn btn-warning">Sort by smallest distance krv</button> 
    <div id="NajblizeLokacije">closest location</div>
</div>
</div>   

1 个答案:

答案 0 :(得分:1)

要计算线距,您可以使用如下算法:

<html>
<script>
    function distance(lat1, lon1, lat2, lon2, unit) {
        var radlat1 = Math.PI * lat1/180
        var radlat2 = Math.PI * lat2/180
        var radlon1 = Math.PI * lon1/180
        var radlon2 = Math.PI * lon2/180
        var theta = lon1-lon2
        var radtheta = Math.PI * theta/180
        var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
        dist = Math.acos(dist)
        dist = dist * 180/Math.PI
        dist = dist * 60 * 1.1515
        if (unit=="K") { dist = dist * 1.609344 }
        if (unit=="N") { dist = dist * 0.8684 }
        return dist
    }

    alert(distance(-19.946595, -44.045269, -28.465452, -49.019373, 'K')); // 20.37 km

</script>

要计算道路距离,请看上面的答案。

编辑:

你可以这样做:

    var myLocationLat = YOUR_LAT;
var myLocationLng = YOUR_LNG;
var marker1Lat = ...;
var marker1Lng = ...;
var marker2Lat = ...;
var marker2Lng = ...;

// distance in kilometers
var myLocToMarker1 = distance(myLocationLat,myLocationLng,marker1Lat,marker1Lng,'K');
var myLocToMarker2 = distance(myLocationLat,myLocationLng,marker2Lat,marker2Lng,'K');

然后,你计算出最小和最大的距离;