How to make a form which searches an item around a specific radius using google maps API?

前端 未结 2 2425
无人共我
无人共我 2021-02-20 14:34

I am working on a website in which I want to make a circle on google map either around current location or some manual address.

  • Users will have option to decid

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-20 14:47

    I recommend using a worker thread to do the searching to free up the UI thread by doing the searching in the background. This is also useful if the user moves the circle or expands/contracts it as the previous search/render of matching markers can be abandoned,

    importScripts("Tier3Toolbox.js");
    
    var currVintage = 0;
    var inBounds = false;
    var facFilter = [];
    var imageProlog = "
    " + "
    "; var facilityTable, lineBreak; self.addEventListener('message', function(e) { var data = e.data; switch (data.cmd) { case 'init': initThread(data.load); break; case 'initFilter': for (var i=0; i diff) return true; return false; } var rectangleCheck = function(msg) { if (facilityTable[facIndex].searchLat > msg.SWLat && facilityTable[facIndex].searchLat < msg.NELat && facilityTable[facIndex].searchLng > msg.SWLng && facilityTable[facIndex].searchLng < msg.NELng) return true; return false; } var GEOFENCER = [circleCheck,rectangleCheck]; var geoFencer = GEOFENCER[msg.checker]; setTimeout(checkLoop,0); return this; }

    The "Toolbox" functions referred to are : -

        function Tier3Toolbox() 
    {
        return this;
    }
    
    Tier3Toolbox.EARTH_RADIUS = 6378137; /* Equitorial Radius instead of 6371000 */
    Tier3Toolbox.toRad = 
        function (num) {
            return num * Math.PI / 180;
        };  
    Tier3Toolbox.calculateDistance =
        function(lat1, lon1, lat2, lon2){
            var dLat = this.toRad(lat2 - lat1);
            var dLon = this.toRad(lon2 - lon1);
            var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(this.toRad(lat1)) * 
                    Math.cos(this.toRad(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
            var distance = this.EARTH_RADIUS * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
            return distance;
        }
    
    Tier3Toolbox.prototype.callAJAX = 
        function(url, method, callback, serverArgs) 
        {
            var callback = callback;
            var xmlhttp;
            var target = url;
            var args = (serverArgs != undefined) ? serverArgs : "";
            var postArgs = "";
            var callbackArgs = new Array();
    
            for (i = 4; i < arguments.length; i++) {
                callbackArgs[i - 3] = arguments[i];
            }
    
            if (window.XMLHttpRequest) {
                xmlhttp = new XMLHttpRequest();
            } else {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
    
            callbackArgs[0] = xmlhttp;
    
            if (method.toUpperCase() == "GET") {
                target = target + "?" + args;
            } 
    
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4) {
                    if (xmlhttp.status == 200) {
                        callback.apply(this, callbackArgs)
                    } else {
                        throw new Error("Error making Ajax call to " + target + " Status = " + xmlhttp.status);
                    }
                }
            };
    
            xmlhttp.open(method, url, true);
            if (method.toUpperCase() == "POST") {
                xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                postArgs = args;
            }
            xmlhttp.send(postArgs);
        }
    
    Tier3Toolbox.reportError =      
        function(error) 
        {
            var header  = error.header  || "Error";
            var message = error.message || "";
            var topWindow=window.top.document.open();
            topWindow.write("

    " + header + "


    "); topWindow.write("

    Please contact Server Support for assistance.


    "); topWindow.write('

    ' + message + "

    "); topWindow.close(); return; }

    In you mainline you need to add listeners like: -

        google.maps.event.addDomListener(radarCircle,    'center_changed', reScope);
        google.maps.event.addDomListener(radarCircle,    'radius_changed', reScope);
        google.maps.event.addDomListener(radarRectangle, 'bounds_changed', reScope);
    
    
        function createFacilityMarkers(xmlhttp){
            facFinder = new Worker("facfinder.js");
            facFinder.addEventListener('message', workerInit, false);
    
            facFinder.postMessage({'cmd' : 'init', 'load' : xmlhttp.responseText});
         }
    
        function reScope() {
    
        var searchReq = {'cmd':'search', 'scopeVintage':scopeVintage};
        if (radarShape.getCenter) {
            searchReq.checker = 0;
            var currCenter = radarCircle.getCenter();
            searchReq.centerLat = currCenter.lat();
            searchReq.centerLng = currCenter.lng();         
            searchReq.radius = radarCircle.getRadius();
        } else {
            searchReq.checker = 1;
            searchReq.SWLat = radarShape.getBounds().getSouthWest().lat();
            searchReq.SWLng = radarShape.getBounds().getSouthWest().lng();
            searchReq.NELat = radarShape.getBounds().getNorthEast().lat();
            searchReq.NELng = radarShape.getBounds().getNorthEast().lng();
        }
    
        facFinder.postMessage(searchReq);
    }
    

    HTH

    Cheers Richard

提交回复
热议问题