473,287 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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 1813
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

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

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
38
by: Lasse Vågsæther Karlsen | last post by:
After working through a fair number of the challenges at www.mathschallenge.net, I noticed that some long-running functions can be helped *a lot* by caching their function results and retrieving...
14
by: Java and Swing | last post by:
static PyObject *wrap_doStuff(PyObject *self, PyObject *args) { // this will store the result in a Python object PyObject *finalResult; // get arguments from Python char *result = 0; char *in=...
4
by: peteh | last post by:
Hello All; The environment is DB2 AIX 8.1.5 (parallel edition) being accessed by a Windows 8.1.2 admin client via Quest. I'm trying to use the snapshot_lockwait table function and getting...
33
by: Pushkar Pradhan | last post by:
I'm using clock() to time parts of my code e.g. clk1 = clock(); /* code */ clk2 = clock(); /* calculate time in secs */ ...... clk1 = clock(); /* code */ clk2 = clock();
8
by: Ravindranath Gummadidala | last post by:
Hi All: I am trying to understand the C function call mechanism. Please bear with me as I state what I know: "every invocation of a function causes a frame for that function to be pushed on...
14
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ?????...
11
by: randomtalk | last post by:
hi, i have the following recursive function (simplified to demonstrate the problem): >>> def reTest(bool): .... result = .... if not bool: .... reTest(True) .... else: .... print...
4
by: default | last post by:
Hello All. I was looking around for a function like strtok() which would tokenize on the complete list of delimiters, rather than tokenize on *any* of the delimiters in the group. I ended up...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.