0

I would like to load dynamically markers (trees) to get displayed around the center of a google map. I call the script xml_trees.php on our server passing the southwest and the nordeast coordinates of the map bounds to get only the trees inside this bounding box. This works fine.

When I'm outside in the field with a tablet displaying my actual gps position and updating the viewport according the gps position, there are to many calls to the database, since even minimal position changes fire the event. Is there a way to reduce the calls so that the call of the xml_trees.php would only be called, when a changed position exceeds a certain distance (e.g. 10m)?

function getTreesAroundMe(){
    google.maps.event.addListener(map, 'center_changed', function() { // instead of 'bounds_changed'
                  if (map.getZoom()>18)  {
                      var bounds =  map.getBounds();
                      var ne = bounds.getNorthEast();
                      var sw = bounds.getSouthWest();
                      dlurl = "./mapdata/xml_trees.php?minx=" + sw.lng() + "&miny="+sw.lat()+"&maxx=" + ne.lng()+"&maxy="+ne.lat();
              getTrees(dlurl,true);
                  }
    });
}
0

There is a solution of course. Use the spherical namespace inside the google maps api and within it use the computeDistanceBetween method to calculate the distance between the new center of the map after your movement and the previous center of the map that you got to store in a global variable inside your your javascript application or module.

Lets suppose you have a general GPS for outdoors with an accuracy of 5-6m. Then I would suggest you set up a limit say 150m to fetch new data. In that scenario on each center_changed event, you would monitor the distance between the former and the current center of the map and this should effectively reduce the number of requests your application is going to make.

You can learn more on this link: Google Geometry Library

0

Thanks Tamas for Your answer. Indeed I went already in this direction, but a set the distance to 10 m because new data is only fetched when at a zoom-level >18. It works and I'll see what distance fits best to our needs.

Here the code I actually use:

var oldcenter = new google.maps.LatLng(0,0);
function getTreesAroundMe(){ 
    google.maps.event.addListener(map, 'center_changed', function() { 
        var newcenter = map.getCenter();
        if (map.getZoom()>18 && google.maps.geometry.spherical.computeDistanceBetween(newcenter, oldcenter)>10) {
            var bounds =  map.getBounds();
            var ne = bounds.getNorthEast();
            var sw = bounds.getSouthWest();
            dlurl = "./mapdata/xml_trees.php?minx=" + sw.lng() + "&miny="+sw.lat()+"&maxx=" + ne.lng()+"&maxy="+ne.lat();
            getTrees(dlurl,true);         
            oldcenter = map.getCenter();     
        }
    });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.