开发者

Google maps api v2 - find closest/nearest markers/points in the map ?

Hi i am trying do what seems to be almost impossible ... i am trying to get three nearest locations marker objects ... is this even possible ? .. it does not have to be 100% accurate ... but to do at least something ... what i found on the net and tried was this :

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]
        }
    }

How you can see from some comments in the code i have tried many different things !! non of them seems to work for me ! It gives the first location (not even correct one) and then just breaks , does nothing for second and third ...

Can anyone see mistakes in this ? OR MAYBE EVEN BETTER KNOW OR CAN WRITE A CODE TO GET DESIRED FUNCTIONALITY ?

lat1, lon1 - of the current point !

i have 300 markers in not big area so there should not be difficult for script to find 3 nearest.


if you iterate through your markers you can call this method on each and then store the results in an array that you can sort the array. This is going to be a costly operation...

    /**
     * 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;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜