473,796 Members | 2,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function results not being returned

pezholio
22 New Member
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 1848
r035198x
13,262 MVP
Well from your code the function usePointFromPos tcode does not seem to be having any returns in it.
Aug 6 '08 #2
pezholio
22 New Member
The usePointFromPos tcode 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 Recognized Expert Moderator Expert
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
4965
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 webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
2
7681
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 like mine to run immediately after it. So in the code below, what JS would i need to add to my "myfile.inc" page so that I could guarantee this behavior? <!-- main page --> <html> <head> <script type="text/javascript">
38
2788
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 from cache instead of calculating again. This means that often I can use a natural recursive implementation instead of unwinding the recursive calls to avoid big exponential running-times. Ok, so I thought, how about creating a decorator that...
14
2742
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= 0; char *aString = 0; char *bString = 0; MY_NUM *a;
4
2345
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 unexpected results: * I run a query to "select * from tableA for update" from a Quest session with autocommit turned off. Results are returned as expected * From another Quest session, "selct * from tableA". As expected, no results are returned and...
33
47635
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
14010
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 stack. this contains the arguments this function was called with, address to return to after return from this function (the location in the previous stack frame), location of previous frame on stack (base or start of this frame) and local variables...
14
1848
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 ????? OK, this function may return a boolean, and if this is true, then no message back is really required, but if it fails then some supporting message needs to be returned to the calling code. As I see it there are a few options.
11
2774
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 "YAHHH" .... result =
4
1749
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 just rolling a function. Thought I would post it here for discussion. Thanks.
0
9684
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10236
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10017
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7552
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6793
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.