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: -
-
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);
-
-
Where I seem to be falling down is this bit: -
homePoint = usePointFromPostcode('WS14 9SQ', '', returnCoords);
-
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
3 1848
Well from your code the function usePointFromPos tcode does not seem to be having any returns in it.
The usePointFromPos tcode function should then pass the point to the returnCoords function, which then returns the coordinates (in theory anyway!)
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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">
|
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...
|
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;
|
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...
| |
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();
|
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...
|
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.
|
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 =
|
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.
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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();...
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |