谷歌地图api v2 - 在地图中找到最近/最近的标记/点?

时间:2011-08-24 14:18:27

标签: javascript web-applications google-maps

您好我正在尝试做似乎几乎不可能的事情......我正在尝试获得三个最近的位置标记对象......这甚至可能吗? ..它不一定是100%准确...但至少要做一些......我在网上发现并试过的是:

function find_closest_marker( lat1, lon1 ) {

        for(var y = 1; y < 4; y++){

            var pi = Math.PI;
            var R = 6371; //equatorial radius

            for(var i=0;i<markers.length; i++ ) {
                //alert(markers.length);
                var closest = -1;
                var distances = [];

                var lat2 = points[i].lat().toFixed(5);
                var lon2 = points[i].lng().toFixed(5);

                var chLat = lat2-lat1;
                var chLon = lon2-lon1;


                var dLat = chLat*(pi/180);
                var dLon = chLon*(pi/180);

                var rLat1 = lat1*(pi/180);
                var rLat2 = lat2*(pi/180);

                var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                        Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(rLat1) * Math.cos(rLat2); 
                var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
                var d = R * c;


                distances[i] = d;
                if ( closest == -1 || d < distances[closest] ) {
                    closest = i;
                }
            }

            var markerImg = "";
            if(markers[closest].markerType == 1){
                markerImg="/files/billeder/../Templates/Designs/Ideal2011/images/Map/dot_red.jpg"
            } else if(markers[closest].markerType == 2){
                markerImg="/files/billeder/../Templates/Designs/Ideal2011/images/Map/dot_lblue.jpg"
            }else if(markers[closest].markerType == 3){
                markerImg="/files/billeder/../Templates/Designs/Ideal2011/images/Map/dot_dblue.jpg"
            }else if(markers[closest].markerType == 4){
                markerImg="/files/billeder/../Templates/Designs/Ideal2011/images/Map/dot_green.jpg"
            }

            $('.nearestPlace'+y).html(
                "<img src='"+markerImg+"' alt='"+markers[closest].address+"' />"+
                "<div class='CompanyName'><strong>"+markers[closest].title+"</strong></div>"+
                markers[closest].address+"<br/>"+
                markers[closest].postby+"<br/>"+
                "Tlf.: "+markers[closest].phone+"<br/>"+
                markers[closest].fax+
                markers[closest].web+
                markers[closest].email
            );
            //markers.pop(markers[closest]);
            //points.pop(points[closest]);
            //markers[closest] = "";
            //points[closest]= "";
            //alert(closest);
            //markers.slice(closest,closest+1);
            //points.slice(closest,closest+1);
            //alert(markers[closest].title)
            delete markers[closest];
            delete points[closest]
        }
    }

如何从代码中的一些评论中看到我尝试了很多不同的东西!!不属于他们似乎对我有用!它给出了第一个位置(甚至不是正确的位置)然后只是休息,对第二个和第三个没有任何作用......

任何人都可以看到错误吗?或者甚至可能更好知道或者可以写一个代码来获得所需的功能?

lat1,lon1 - 当前点!

我在不大的区域有300个标记,所以脚本找不到3个最近的标记。

1 个答案:

答案 0 :(得分:0)

如果遍历标记,则可以在每个标记上调用此方法,然后将结果存储在可以对数组进行排序的数组中。这将是一项代价高昂的行动......

    /**
     * This will give you the distance in kilometers between 2 positions
     *
     * @param lat1 - The first positions latitude
     * @param lng2 - The first positions longitude
     * @param lat2 - The second positions latitide
     * @param lng2 - The second positions longitude
     *
     * @return int- The distance in kilometers between the 2 places
     */
    distanceFrom : function(lat1,lng1, lat2, lng2) {
        var R = 6371; // km
        var dLat = this._toRad((lat2-lat1));
        var dLon = this._toRad((lng2-lng1));
        var lat1 = this._toRad(lat1);
        var lat2 = this._toRad(lat2);

        var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        var d = R * c;

        return d;
    }