Hi,
I'm having a bit of trouble using Google Maps and the Google Ajax Search API, I'm trying to take a postcode, geocode it and then calculate the distance between the that postcode and a second geocoded postcode. If the point is less than 5 miles away, I then put a point on the map. My code is here:
Where I seem to be falling down is this bit:
It doesn't seem to return a value. Any ideas?
(Please bear in ming, I'm a bit of a noob when it comes to Javascript, I'm more of a PHP man myself, so apologies in advance for the sloppy code!!)
Cheers
I'm having a bit of trouble using Google Maps and the Google Ajax Search API, I'm trying to take a postcode, geocode it and then calculate the distance between the that postcode and a second geocoded postcode. If the point is less than 5 miles away, I then put a point on the map. My code is here:
Code:
function createMarker(point,html) {
// FF 1.5 fix
html = '<div style="white-space:nowrap;">' + html + '</div>';
var marker = new GMarker(point);
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml(html);
});
return marker;
}
function usePointFromPostcode(postcode, infoText, callbackFunction) {
var localSearch = new GlocalSearch();
localSearch.setSearchCompleteCallback(null,
function() {
if (localSearch.results[0])
{
var resultLat = localSearch.results[0].lat;
var resultLng = localSearch.results[0].lng;
var point = new GLatLng(resultLat,resultLng);
callbackFunction(point,infoText);
}else{
alert("Postcode not found!");
}
});
localSearch.execute(postcode + ", UK");
}
function placeMarkerAtPoint(point, infoText)
{
var marker = createMarker(point,infoText)
map.addOverlay(marker);
}
function setCenterToPoint(point)
{
map.setCenter(point, 15);
}
Rm = 3961; // mean radius of the earth (miles) at 39 degrees from the equator
Rk = 6373; // mean radius of the earth (km) at 39 degrees from the equator
/* main function */
function findDistance(homePoint, planPoint)
{
homePoint = homePoint.split(",");
planPoint = planPoint.split(",");
// get values for lat1, lon1, lat2, and lon2
t1 = homePoint[0];
n1 = homePoint[1];
t2 = planPoint[0];
n2 = planPoint[1];
// convert coordinates to radians
lat1 = deg2rad(t1);
lon1 = deg2rad(n1);
lat2 = deg2rad(t2);
lon2 = deg2rad(n2);
// find the differences between the coordinates
dlat = lat2 - lat1;
dlon = lon2 - lon1;
// here's the heavy lifting
a = Math.pow(Math.sin(dlat/2),2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon/2),2);
// c = 2 * Math.atan(Math.sqrt(a),Math.sqrt(1-a)); // great circle distance in radians
c = 2 * Math.atan2(Math.sqrt(a),Math.sqrt(1-a)); // great circle distance in radians
dm = c * Rm; // great circle distance in miles
dk = c * Rk; // great circle distance in km
// round the results down to the nearest 1/1000
mi = round(dm);
km = round(dk);
return mi;
}
/* convert degrees to radians */
function deg2rad(deg)
{
rad = deg * Math.PI/180; // radians = degrees * pi/180
return rad;
}
/* round to the nearest 1/1000 */
function round(x)
{
r = Math.round(x*1000)/1000;
return r;
}
function returnCoords(point) {
return point;
}
function mapLoad() {
homePoint = usePointFromPostcode('WS14 9SQ', '', returnCoords);
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(52.690743,-1.797638), 11, G_NORMAL_MAP);
if (usePointFromPostcode('B78 3DY', homePoint , findDistance) < 5) {
usePointFromPostcode('B78 3DY', 'Text goes here', placeMarkerAtPoint);
}
if (usePointFromPostcode('WS14 9LE', homePoint , findDistance) < 5) {
usePointFromPostcode('WS14 9LE', 'Text goes here', placeMarkerAtPoint);
}
if (usePointFromPostcode('WS13 6SB', homePoint , findDistance) < 5) {
usePointFromPostcode('WS13 6SB', 'Text goes here', placeMarkerAtPoint);
}
if (usePointFromPostcode('WS15 4LF', homePoint , findDistance) < 5) {
usePointFromPostcode('WS15 4LF', 'Text goes here', placeMarkerAtPoint);
}
if (usePointFromPostcode('WS13 6QH', homePoint , findDistance) < 5) {
usePointFromPostcode('WS13 6QH', 'Text goes here', placeMarkerAtPoint);
}
if (usePointFromPostcode('WS7 4SJ', homePoint , findDistance) < 5) {
usePointFromPostcode('WS7 4SJ', 'Text goes here', placeMarkerAtPoint);
}
if (usePointFromPostcode('WS13 6EF', homePoint , findDistance) < 5) {
usePointFromPostcode('WS13 6EF', 'Text goes here', placeMarkerAtPoint);
}
if (usePointFromPostcode('WS9 9EF', homePoint , findDistance) < 5) {
usePointFromPostcode('WS9 9EF', 'Text goes here', placeMarkerAtPoint);
}
if (usePointFromPostcode('B74 3AP', homePoint , findDistance) < 5) {
usePointFromPostcode('B74 3AP', 'Text goes here', placeMarkerAtPoint);
}
if (usePointFromPostcode('B78 2AB', homePoint , findDistance) < 5) {
usePointFromPostcode('B78 2AB', 'Text goes here', placeMarkerAtPoint);
}
}
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
function addUnLoadEvent(func) {
var oldonunload = window.onunload;
if (typeof window.onunload != 'function') {
window.onunload = func;
} else {
window.onunload = function() {
oldonunload();
func();
}
}
}
addLoadEvent(mapLoad);
addUnLoadEvent(GUnload);
Code:
homePoint = usePointFromPostcode('WS14 9SQ', '', returnCoords);
(Please bear in ming, I'm a bit of a noob when it comes to Javascript, I'm more of a PHP man myself, so apologies in advance for the sloppy code!!)
Cheers
Comment