469,354 Members | 2,406 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,354 developers. It's quick & easy.

Function results not being returned

pezholio
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. function createMarker(point,html) {
  3.         // FF 1.5 fix
  4.         html = '<div style="white-space:nowrap;">' + html + '</div>';
  5.         var marker = new GMarker(point);
  6.         GEvent.addListener(marker, "click", function() {
  7.           marker.openInfoWindowHtml(html);
  8.         });
  9.         return marker;
  10.       }
  11.  
  12. function usePointFromPostcode(postcode, infoText, callbackFunction) {
  13.  
  14. var localSearch = new GlocalSearch();
  15.  
  16.     localSearch.setSearchCompleteCallback(null, 
  17.         function() {
  18.  
  19.             if (localSearch.results[0])
  20.             {        
  21.                 var resultLat = localSearch.results[0].lat;
  22.                 var resultLng = localSearch.results[0].lng;
  23.                 var point = new GLatLng(resultLat,resultLng);
  24.                 callbackFunction(point,infoText);
  25.             }else{
  26.                 alert("Postcode not found!");
  27.             }
  28.         });    
  29.  
  30.     localSearch.execute(postcode + ", UK");
  31. }
  32.  
  33. function placeMarkerAtPoint(point, infoText)
  34. {
  35.       var marker = createMarker(point,infoText)
  36.       map.addOverlay(marker);
  37. }
  38.  
  39. function setCenterToPoint(point)
  40. {
  41.     map.setCenter(point, 15);
  42. }
  43.  
  44.     Rm = 3961; // mean radius of the earth (miles) at 39 degrees from the equator
  45.     Rk = 6373; // mean radius of the earth (km) at 39 degrees from the equator
  46.  
  47.     /* main function */
  48. function findDistance(homePoint, planPoint)
  49.     {
  50.  
  51.     homePoint = homePoint.split(",");
  52.     planPoint = planPoint.split(",");
  53.  
  54.         // get values for lat1, lon1, lat2, and lon2
  55.         t1 = homePoint[0];
  56.         n1 = homePoint[1];
  57.         t2 = planPoint[0];
  58.         n2 = planPoint[1];
  59.  
  60.         // convert coordinates to radians
  61.         lat1 = deg2rad(t1);
  62.         lon1 = deg2rad(n1);
  63.         lat2 = deg2rad(t2);
  64.         lon2 = deg2rad(n2);
  65.  
  66.         // find the differences between the coordinates
  67.         dlat = lat2 - lat1;
  68.         dlon = lon2 - lon1;
  69.  
  70.         // here's the heavy lifting
  71.         a  = Math.pow(Math.sin(dlat/2),2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon/2),2);
  72. //        c  = 2 * Math.atan(Math.sqrt(a),Math.sqrt(1-a)); // great circle distance in radians
  73.         c  = 2 * Math.atan2(Math.sqrt(a),Math.sqrt(1-a)); // great circle distance in radians
  74.         dm = c * Rm; // great circle distance in miles
  75.         dk = c * Rk; // great circle distance in km
  76.  
  77.         // round the results down to the nearest 1/1000
  78.         mi = round(dm);
  79.         km = round(dk);
  80.  
  81.  
  82.         return mi;
  83.  
  84.     }
  85.  
  86.  
  87.     /* convert degrees to radians */
  88.     function deg2rad(deg)
  89.     {
  90.         rad = deg * Math.PI/180; // radians = degrees * pi/180
  91.         return rad;
  92.     }
  93.  
  94.  
  95.     /* round to the nearest 1/1000 */
  96.     function round(x)
  97.     {
  98.         r = Math.round(x*1000)/1000;
  99.         return r;
  100.     }
  101.  
  102. function returnCoords(point) {
  103.     return point;
  104. }
  105.  
  106.     function mapLoad() {
  107.  
  108.     homePoint = usePointFromPostcode('WS14 9SQ', '', returnCoords);    
  109.  
  110.     if (GBrowserIsCompatible()) {
  111.         map = new GMap2(document.getElementById("map"));
  112.  
  113.  
  114.         map.addControl(new GLargeMapControl());
  115.         map.addControl(new GMapTypeControl());
  116.         map.setCenter(new GLatLng(52.690743,-1.797638), 11, G_NORMAL_MAP); 
  117.  
  118. if (usePointFromPostcode('B78 3DY', homePoint , findDistance) < 5) {
  119. usePointFromPostcode('B78 3DY', 'Text goes here', placeMarkerAtPoint);
  120. }
  121. if (usePointFromPostcode('WS14 9LE', homePoint , findDistance) < 5) {
  122. usePointFromPostcode('WS14 9LE', 'Text goes here', placeMarkerAtPoint);
  123. }
  124. if (usePointFromPostcode('WS13 6SB', homePoint , findDistance) < 5) {
  125. usePointFromPostcode('WS13 6SB', 'Text goes here', placeMarkerAtPoint);
  126. }
  127. if (usePointFromPostcode('WS15 4LF', homePoint , findDistance) < 5) {
  128. usePointFromPostcode('WS15 4LF', 'Text goes here', placeMarkerAtPoint);
  129. }
  130. if (usePointFromPostcode('WS13 6QH', homePoint , findDistance) < 5) {
  131. usePointFromPostcode('WS13 6QH', 'Text goes here', placeMarkerAtPoint);
  132. }
  133. if (usePointFromPostcode('WS7 4SJ', homePoint , findDistance) < 5) {
  134. usePointFromPostcode('WS7 4SJ', 'Text goes here', placeMarkerAtPoint);
  135. }
  136. if (usePointFromPostcode('WS13 6EF', homePoint , findDistance) < 5) {
  137. usePointFromPostcode('WS13 6EF', 'Text goes here', placeMarkerAtPoint);
  138. }
  139. if (usePointFromPostcode('WS9 9EF', homePoint , findDistance) < 5) {
  140. usePointFromPostcode('WS9 9EF', 'Text goes here', placeMarkerAtPoint);
  141. }
  142. if (usePointFromPostcode('B74 3AP', homePoint , findDistance) < 5) {
  143. usePointFromPostcode('B74 3AP', 'Text goes here', placeMarkerAtPoint);
  144. }
  145. if (usePointFromPostcode('B78 2AB', homePoint , findDistance) < 5) {
  146. usePointFromPostcode('B78 2AB', 'Text goes here', placeMarkerAtPoint);
  147. }
  148.  
  149.     }
  150. }
  151.  
  152. function addLoadEvent(func) {
  153.   var oldonload = window.onload;
  154.   if (typeof window.onload != 'function') {
  155.     window.onload = func;
  156.   } else {
  157.     window.onload = function() {
  158.       oldonload();
  159.       func();
  160.     }
  161.   }
  162. }
  163.  
  164. function addUnLoadEvent(func) {
  165.     var oldonunload = window.onunload;
  166.     if (typeof window.onunload != 'function') {
  167.       window.onunload = func;
  168.     } else {
  169.       window.onunload = function() {
  170.         oldonunload();
  171.         func();
  172.       }
  173.     }
  174. }
  175.  
  176. addLoadEvent(mapLoad);
  177. addUnLoadEvent(GUnload);
  178.  
  179.  
Where I seem to be falling down is this bit:

Expand|Select|Wrap|Line Numbers
  1. homePoint = usePointFromPostcode('WS14 9SQ', '', returnCoords);    
  2.  
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
Aug 6 '08 #1
3 1595
r035198x
13,262 8TB
Well from your code the function usePointFromPostcode does not seem to be having any returns in it.
Aug 6 '08 #2
The usePointFromPostcode function should then pass the point to the returnCoords function, which then returns the coordinates (in theory anyway!)
Aug 6 '08 #3
gits
5,390 Expert Mod 4TB
are you sure that the response is ready at this time? the coords are requested with an ajax call and the callback is out of the flow as far as i could see. your program don't wait for the response and just assumes the you would have immediate response after the call in line 107 ... you should ensure that your further processing is triggered by the callback-function ...

kind regards
Aug 6 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Penn Markham | last post: by
2 posts views Thread by laredotornado | last post: by
38 posts views Thread by Lasse Vågsæther Karlsen | last post: by
14 posts views Thread by Java and Swing | last post: by
33 posts views Thread by Pushkar Pradhan | last post: by
8 posts views Thread by Ravindranath Gummadidala | last post: by
11 posts views Thread by randomtalk | last post: by
4 posts views Thread by default | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.