Search code examples
google-maps-api-3google-maps-markersgoogle-fusion-tables

Count and output number of markers bound by circle radius


I have a map thats populated with markers of places from a fusion table. I'm taking the users location and displaying a circle of radius 10 miles from their location. Here is my code - http://connormccarra.com/sandbox/map/. How can I use the api to count the number of markers bound by the circle and output that number in the footer?

Cheers!

Relevant code:

var map;
function Initialize() {
    var MapOptions = {
        zoom: 7,
        center: new google.maps.LatLng(53.4125694, -8.245014),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        sensor: true
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), MapOptions);

      var layer = new google.maps.FusionTablesLayer({
    query: {
      select: 'Address',
      from: '1OPU6utSjRYwJSFK-EXdaGmt2KgLTq2loVIjS3AA'
    }
  });
    layer.setMap(map);

  // Try HTML5 geolocation
  if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = new google.maps.LatLng(position.coords.latitude,
                                       position.coords.longitude);

      var marker = new google.maps.Marker({
        map: map,
        position: pos,
        content: 'You are here!'
      });

      // Add circle overlay and bind to marker
var circle = new google.maps.Circle({
  map: map,
  radius: 16093,    // 10 miles in metres
  fillColor: '#AA0000'
});
circle.bindTo('center', marker, 'position');

      map.setCenter(pos);
    }, function() {
      handleNoGeolocation(true);
    });
  } else {
    // Browser doesn't support Geolocation
    handleNoGeolocation(false);
  }
}

function handleNoGeolocation(errorFlag) {
  if (errorFlag) {
    var content = 'Error: The Geolocation service failed.';
  } else {
    var content = 'Error: Your browser doesn\'t support geolocation.';
  }

  var options = {
    map: map,
    position: new google.maps.LatLng(60, 105),
    content: content
  };

var count = mgr.getMarkerCount(circle);
        document.getElementById("Address").innerHTML += count + "<BR>";

  google.maps.event.addDomListener(window, 'load', initialize);

    //Maps API loaded, now load customizations

    var element = document.createElement('script');
    element.src = 'template.js';
    element.type = 'text/javascript';
    var scripts = document.getElementsByTagName('script')[0];
    scripts.parentNode.insertBefore(element, scripts);
}

Solution

  • The markers created by a FusionTableLayer are not real markers, there is no way to get them as a kind of list to filter them(you can't get any details for the markers, except you click them).

    But you may request the FusionTableAPI with a spatial condition(via AJAX, jsonp is supported).

    The syntax for the query:

    SELECT COUNT() from tableId 
      WHERE ST_INTERSECTS('Address',CIRCLE(LATLNG(lat,lng),10000))
    

    How to send a query : https://developers.google.com/fusiontables/docs/v1/sql-reference

    Demo(using data of another FusionTable because your table is protected):
    http://jsfiddle.net/doktormolle/bAtgf/