2012-04-11 25 views

risposta

15

uso point1.distanceTo(point2)

var Geographic = new OpenLayers.Projection("EPSG:4326"); 
var Mercator = new OpenLayers.Projection("EPSG:900913"); 


function distanceBetweenPoints(latlng1, latlng2){ 
     var point1 = new OpenLayers.Geometry.Point(latlng1.lon, latlng1.lat).transform(Geographic, Mercator); 
     var point2 = new OpenLayers.Geometry.Point(latlng2.lon, latlng2.lat).transform(Geographic, Mercator);  
     return point1.distanceTo(point2); 
    } 
3

È possibile utilizzare tale metodo se utilizzano openlayers3

instanciate un oggetto ol.geom.LineString tra i due punti e calcolare la lunghezza del riga:

 this.distanceBetweenPoints = function(latlng1, latlng2){ 
      var line = new ol.geom.LineString([latlng1, latlng2]); 
      return Math.round(line.getLength() * 100)/100; 
     }; 

È possibile ottenere un valore leggibile utilizzando una formazione:

 this.formatDistance = function(length) { 
      if (length >= 1000) { 
       length = (Math.round(length/1000 * 100)/100) + 
       ' ' + 'km'; 
      } else { 
       length = Math.round(length) + 
       ' ' + 'm'; 
      } 
      return length; 
     } 
Problemi correlati