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

前端 未结 2 2149
别那么骄傲
别那么骄傲 2021-02-20 14:31

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:41

    Let's try to give you some first steps, I would no code the whole app, but rather give you some guide lines in to how to solve the small subproblems that you have:

    Adding a circle in a map

    Well, for this you have many different options for input, but the most important part is the addCircle function:

    function addCircle(center){
        circle = new google.maps.Circle({
            map: map, //The existing map
            center: center,
            radius: 200, //This will be modified afterwards
            zindex: 100
        });
    }
    

    The center could come from a click for example:

    // Area is wherever you want to attach the click, either a polygon, a map...
    google.maps.event.addListener(area, "click", function(event) {
            addCircle(event.latLng);
    });
    

    OR by getting the position of a certain address (This is documented as well), OR whatever method (drag and drop the circle, drag the marker blablabla)

    Adding a dynamic radius

    Well, if we know that the radius of the Circle is given in meters, then is very easy to give the addCircle function the correct radius. For example, 20km -> 20 000 meters. So you just have to be able to access radius when calling addCircle (it can be an argument, a global variable... your choice).

    We are done with the drawing part, lets now search within that circle.

    Getting only the markers inside the circle

    There is a prerequisite here, to have all the markers of your map. You may have an array of places that you get from a database or maybe you get the markers from Google Maps API (Place search for example).

    After that you will have to compute the distance between those markers and your given center, and check if the distance is smaller than your radius (with computeDistanceBetween is very easy), so you will know which markers are valid for you.

    const markers = [//array of my valid markers with position];
    markers.filter( (marker) => 
        google.maps.geometry.spherical.computeDistanceBetween(marker.getPosition(), center.getPosition()) < radius; // Filter the markers which distance is bigger than radius;
    

    The rest of the job should be as easy, place the markers in the map and do whatever you like with this information.

    EXTRAS

    As a further help there are a pair of examples/answers that may be useful for you:

    Full Google Map API example, very easy step by step guide.

    Radius search using places, a good answer in to how to do radius search.

    Example of radius search, open F12 and debug the code if you like, but it is easy to follow.

    EDIT**: I did not realize that 2 of these link where also pointed out in the comments.

    0 讨论(0)
  • 2021-02-20 14:55

    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 = "<div style='height:5em; width:5em; display:inline-block;vertical-align:middle;'>" +
                      "<img style='height:100%; width: 100%; max-height:100%; max-width:100%' src='";
    var imageEpilog = "' ></div>";
    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<data.filterTable.length; i++) {
            facFilter[data.filterTable[i].locTypeId] = {'icon':data.filterTable[i].icon};
          }
          break;
        case 'filter':
          facFilter = [];
          for (var i=0; i<data.filterTable.length; i++) {
            if (data.filterTable[i].facSelected)
              facFilter[data.filterTable[i].locTypeId] = {'icon':data.filterTable[i].icon};
          }
          break;
        case 'search':
          var searchVintage = ++currVintage;
          var tableSearch = new searcher(searchVintage, data);
          break;
        case 'reset':
          reset();
          self.postMessage({'reset': true});
          break;
        case 'stop':
          self.postMessage({'success' : true});
          self.close(); 
          break;
        default:
          self.postMessage({'success' : false, 'msg' : data.msg});
      };
    }, false);
    
    function initThread(msg) 
    {
        facilityTable = JSON.parse(msg);
        reset();
    
        self.postMessage({'success' : true, 
                          'cnt'     : facilityTable.length
                        });     
    }   
    
    function reset() 
    {
        for (var i=0; i<facilityTable.length; i++) {
            facilityTable[i].visible=false
        }
        currVintage = 0;
    }   
    
    function searcher(searchVintage, msg)
    {
        var myVintage = searchVintage;
        var facIndex  = -1;
        var msg       = msg;
    
        var checkLoop = function()
        {
            if (myVintage != currVintage)
                return;
    
            if (++facIndex == facilityTable.length)
                return;
    
            inBounds = geoFencer.call(this, msg);
    
            if (inBounds) {
                var facMatch = 0;
                var bubbleHTML = "";
                for (var i=0; i<facilityTable[facIndex].facilities.length; i++){
                    var currFac = facilityTable[facIndex].facilities[i];
                    if (facFilter[currFac.locTypeId] != undefined) {
                        if (facMatch != 0) {
                            lineBreak = (facMatch / 3);
                            if (lineBreak == lineBreak.toFixed(0)) {
                                bubbleHTML += "<br />";
                            }
                        }
                        facMatch++;
                        bubbleHTML += imageProlog + facFilter[currFac.locTypeId].icon + imageEpilog;
    
                    }
                }
                if (facMatch == 0) {
                    inBounds = false;
                }
            }
    
            if (inBounds != facilityTable[facIndex].visible) {
                self.postMessage({'match'       : inBounds,
                                  'facIndex'    : facIndex,
                                  'scopeVintage': msg.scopeVintage,
                                  'bubbleHTML'  : bubbleHTML,
                                  'success'     : true
                                }); 
                facilityTable[facIndex].visible = inBounds;
            }
    
            setTimeout(checkLoop,0);
        }
    
        var circleCheck = function(msg) 
        {
            var diff = Tier3Toolbox.calculateDistance(
                            msg.centerLat,
                            msg.centerLng,
                            facilityTable[facIndex].searchLat,
                            facilityTable[facIndex].searchLng);
    
            if (msg.radius > 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("<!DOCTYPE html><html><body style='height: 100%;'><hr><h1>" + header + "</h1><hr>");
            topWindow.write("<h2>Please contact Server Support for assistance.</h2><br />");
            topWindow.write('<p style="color:red">' + message + "</p></body></html>");
            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

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