Hi All,
I'm trying to display a postal area on Google Maps API within it's boundaries. I want to do this dynamically so that everytime someone changes the text in a text field (entering a postcode or part of), it checks the postcode and calculates the boundaries then displays them.
My current issue is that I cannot get the map to refresh after each keyup.
My code is as follows:
var map;
var postCodePlot;
var postCodeBorderCoords = [];
function initialize() {
//alert("initialize called");
var mapProp = {
center: new google.maps.LatLng(51.55777197508853, -0.13395323779611973),
zoom:14,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
// Construct the polygon.
postCodePlot = new google.maps.Polygon({
paths: postCodeBorderCoords,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35
});
postCodePlot.setMap(map);
//google.maps.event.trigger(map, 'resize');
}
google.maps.event.addDomListener(window, 'load', initialize);
google.maps.event.addDomListener($("#postcodeInput")[0], 'keyup', function() {
postCodeBorderCoords = [];
$.getJSON("locateTerritory.php", {postCodeToCheck:$(this).val()}, function(result) {
$.each(result, function(key, value) {
var gridRef = new OsGridRef(value['easting'], value['northing']);
var latLon = OsGridRef.osGridToLatLong(gridRef);
postCodeBorderCoords.push(new google.maps.LatLng(latLon.lat, latLon.lon));
//alert(latLon.lat + ", " + latLon.lon);
});
postCodeBorderCoords.push(postCodeBorderCoords[0]);
});
initialize();
});
Where $("#postcodeInput") is the text field id.
My JavaScript skills are pretty weak so I'm probably making some fairly rudimentary errors here.
Also, if I leave in the alert in the initialize() function, after I close the alert window the map refreshes with the desired result. How to get it working without this...?
I've also tried google.maps.event.trigger(map, 'resize'); but I'm not sure if I'm using it right or if it is even what I need.
Thanks for any help offered.