GoogleMaps Polyline Length in V3

后端 未结 7 1186
半阙折子戏
半阙折子戏 2020-12-10 23:30

There doesn\'t seem to be a length funciton for the Polyline in Google Maps anymore in V3.

Has anyone found a workaround for this functionality gap?

相关标签:
7条回答
  • 2020-12-11 00:04

    here's the prototypes for the required function - hope this helps any further:

    google.maps.Polygon.prototype.Distance = function() {
       var dist = 0;
       for (var i=1; i < this.getPath().getLength(); i++) {
          dist += this.getPath().getAt(i).distanceFrom(this.getPath().getAt(i-1));
       }
       return dist;
    }
    
    google.maps.LatLng.prototype.distanceFrom = function(newLatLng) {
        //var R = 6371; // km (change this constant to get miles)
        var R = 6378100; // meters
        var lat1 = this.lat();
        var lon1 = this.lng();
        var lat2 = newLatLng.lat();
        var lon2 = newLatLng.lng();
        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;
        return d;
    }
    

    source

    0 讨论(0)
提交回复
热议问题