google地图通过js计算当前位置与其余多个点之间的最近距离

  var childs = <?= json_encode($childs, JSON_UNESCAPED_UNICODE) ?>;

  var latlng = new google.maps.LatLng(value.google_lnglat.lat, value.google_lnglat.lng); //value.google_lnglat.lat, value.google_lnglat.lng 当前位置经纬度

  function getFlatternDistance(lat1, lng1) {
            var julichangdu = new Array();
            childs && $.each(childs, function (idx, dest) { //其余所有点的内容

                var lat2 = dest['google_lnglat'][1];
                var lng2 = dest['google_lnglat'][0]
                var f = getRad((Number(lat1) + Number(lat2)) / 2);
                var g = getRad((Number(lat1) - Number(lat2)) / 2);
                var l = getRad((Number(lng1) - Number(lng2)) / 2);

                var sg = Math.sin(g);
                var sl = Math.sin(l);
                var sf = Math.sin(f);

                var s, c, w, r, d, h1, h2;
                var a = EARTH_RADIUS;
                var fl = 1 / 298.257;

                sg = sg * sg;
                sl = sl * sl;
                sf = sf * sf;

                s = sg * (1 - sl) + (1 - sf) * sl;
                c = (1 - sg) * (1 - sl) + sf * sl;

                w = Math.atan(Math.sqrt(s / c));
                r = Math.sqrt(s * c) / w;
                d = 2 * w * a;
                h1 = (3 * r - 1) / 2 / c;
                h2 = (3 * r + 1) / 2 / s;
                julichangdu[dest['destid']] = d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg));
            });
            var d = 0;
            var di = 0;
            console.log(julichangdu)
            for (var p in julichangdu) {
                console.log(di)
                console.log(d)
                if (di == 0) {
                    di = julichangdu[p];
                    d = p;
                } else {
                    if (di > julichangdu[p]) {
                        di = julichangdu[p];
                        d = p;
                    }
                }
            }
            return d;
        }

你可能感兴趣的:(google地图)