使用Google Maps API v3查找三个点之间的大圆距离

时间:2013-06-17 14:06:32

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

很遗憾,我无法找到任何代码/示例如何在Google地图上的三(3)个点之间绘制大圆距离 * * ??

这是两点的javascript。我怎样才能让它与三点合作?

var geocoder;
var map;
var addresses;
var results;
var dist;

function initialize() {
    geocoder = new google.maps.Geocoder();
    address1 = document.getElementById("distancefrom").value;
    address2 = document.getElementById("distanceto").value;
    (distance(address1, address2));
}

function distance(add1, add2) {
    if (!geocoder)
        return 'Error, no geocoder';

    addresses = new Array(2);
    addresses[0] = add1;
    addresses[1] = add2;
    results = new Array(2);
    results[0] = new Array(2);
    results[1] = new Array(2);

    results[0][0] = 0; results[0][1] = 0; results[1][0] = 0; results[1][1] = 0.87;
    geocoded(1);
}

function geocoded(i) {
    geocoder.geocode( { 'address': addresses[i] }, function(res, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            results[i][0] = parseFloat(res[0].geometry.location.lat());
            results[i][1] = parseFloat(res[0].geometry.location.lng());
            i--;

            if (i >= 0)
                geocoded(i);
            else {
                var latlng1 = new google.maps.LatLng(results[0][0], results[0][1]);
                var latlng2 = new google.maps.LatLng(results[1][0], results[1][1]);

                dist = google.maps.geometry.spherical.computeDistanceBetween(latlng1, latlng2)/1000;
                var dmi = 0.539957*dist;
                document.getElementById('totaldistancemiles').value = dmi.toFixed(2);
                document.getElementById('totaldistancekm').value = dist.toFixed(2);
                showMap(latlng1,latlng2);
            }
        }
    });
}

function showMap(location1,location2) {
    latlng = new google.maps.LatLng((location1.lat()+location2.lat())/2,
    (location1.lng()+location2.lng())/2);
    faddress1 = document.getElementById("distancefrom").value;
    faddress2 = document.getElementById("distanceto").value;
    var mapOptions = 
    {
        zoom: 1,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.HYBRID
    };

    map = new google.maps.Map(document.getElementById("map"), mapOptions);
    var flightPlanCoordinates = [
        location1,
        location2
    ];
    var flightPath = new google.maps.Polyline({
        path: flightPlanCoordinates,
        strokeColor: "#FF0000",
        strokeOpacity: .5,
        geodesic: true,
        strokeWeight: 3
    });

    flightPath.setMap(map);

    var flightPath = new google.maps.Polyline({
        path: flightPlanCoordinates,
        strokeColor: "#000000",
        strokeOpacity: .5,
        strokeWeight: 0
    });
    flightPath.setMap(map);
    var igreen= 'https://dl.dropboxusercontent.com/u/85343999/igreen.gif';
    var marker = new google.maps.Marker({
        map: map, 
        icon: igreen, 
        position: location1
    });
    var latlngbounds;
    latlngbounds = new google.maps.LatLngBounds();
    latlngbounds.extend( location1);
    latlngbounds.extend( location2);
    map.fitBounds(latlngbounds);
    var infowindow = new google.maps.InfoWindow();
    infowindow.setContent(faddress1);
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map, marker);
    });

    var marker2 = new google.maps.Marker({
        map: map, 
        icon: igreen, 
        position: location2
    });

    var infowindow2 = new google.maps.InfoWindow();
    infowindow2.setContent(faddress2);
    google.maps.event.addListener(marker2, 'click', function() {
        infowindow2.open(map, marker2);
    });
}

1 个答案:

答案 0 :(得分:0)

以下代码使用Google geometry库来计算点之间的距离。这种情况下,位置存储在数组中,但可以从文本框等中获取。距离以米为单位。

<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false"></script>
<script type="text/javascript">
var coords = [
      [35.733972, -5.881999],
      [35.734077, -5.881033],
      [35.736898, -5.877771],
];
function calcDistance() { 
  var first = new google.maps.LatLng(coords[0][0],coords[0][1]);
  var second = new google.maps.LatLng(coords[1][0],coords[1][1]);
  var third = new google.maps.LatLng(coords[2][0],coords[2][1]);
  var distance1 = google.maps.geometry.spherical.computeDistanceBetween(first, second);
  var distance2 = google.maps.geometry.spherical.computeDistanceBetween(first, third);
  var distance3 = google.maps.geometry.spherical.computeDistanceBetween(second, third);
  showDistance(distance1,distance2,distance3);
}   
 function showDistance(distance1,distance2,distance3) {        
   document.write("Distance1 "+distance1 + " metres <br>"); 
   document.write("Distance2 "+distance2 + " metres <br>"); 
   document.write("Distance3 "+distance3 + " metres <br>"); 

}