Question

I was wondering if I can limit my map search to only return locations that are located within the bounds of my layer?

I understand you can create a bounds object and then use this to check if what you search is within it, but I don't know how to implement this correctly and stop the map from travelling across the globe to the wrong location.

The problem arises if I search a postcode sector (that is in my fusion table / layer) sometimes it will take me to a highway in China with a name the same as the postcode sector (for example 'G1'). I could understand if Google was setting a precedence to look for locations with that name first instead of my fusion table but if I search 'G84', it will display the correct area from my table/layer so it makes me think that it is looking at the table first but assigning priority to the wrong place?

Here is a JSFiddle of my code, I have added in everything from my code but I can't get the map to display here: http://jsfiddle.net/3U4Vd/10/

function initialize() {
var tableId = '1S0FCMn-wGr2zy6SrAnre6HNF_KghfZmObb4sNLc';
var geocoder = new google.maps.Geocoder();
map = new google.maps.Map(document.getElementById('map-canvas'), {
    center: new google.maps.LatLng(55.864237, -4.2518059999999880),
    zoom: 13,
    mapTypeId: google.maps.MapTypeId.ROADMAP
});

layer = new google.maps.FusionTablesLayer({
    query: {
        select: "col40>>0",
        from: '1S0FCMn-wGr2zy6SrAnre6HNF_KghfZmObb4sNLc'
    },
    map: map,
    styleId: 2,
    templateId: 2
});
}

That's my initial loading of the map and layer.

Was it helpful?

Solution

You may additionally add a bounds-property(the bounds of all the features in the table) to the request, then the geocoder will prefer results within these bounds.

sample-code:

 geocoder.geocode({
        address: address,
        bounds: new google.maps.LatLngBounds(
                     new google.maps.LatLng(55.661031, -4.893133), 
                     new google.maps.LatLng(56.365728, -3.865911))
    },
    function(){
      //some code
    }
  );

demo: http://jsfiddle.net/doktormolle/JLC4F/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top