...

View Full Version : function returns undefined



arpitbandi
07-27-2011, 01:35 PM
hi here is a code i use to calculate distance b//w 2 places using google api...
it works perfectly and shows the results in the html but when i add a return statement at the end of the function showlocation() it returns undefined.. why it is so.. how to resolve it???


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<meta name="robots" content="noindex,follow" />
<title>Calculate driving distance with Google Maps API</title>
<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAMK3PClIOG6IUkYprx4EfNxSY_HQRLXr6AGORx7Qh39w3-je8JxRROt5eJTcDPJ9nGnVn9xXKTQ2l8Q" type="text/javascript"></script>
<!-- According to the Google Maps API Terms of Service you are required display a Google map when using the Google Maps API. see: http://code.google.com/apis/maps/terms.html -->
<script type="text/javascript">

var geocoder, location1,addr1,addr2, location2, result1,gDir;
function coolAl(add1,add2) {
addr1=add1;
addr2=add2;
var result= return initialize();
showLocation();
alert(result);
}
function initialize() {
geocoder = new GClientGeocoder();
gDir = new GDirections();
GEvent.addListener(gDir, "load", function() {
var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
result1=location1.address + ' (' + location1.lat + ':' + location1.lon + ')/' + location2.address + ' (' + location2.lat + ':' + location2.lon + ')/' + drivingDistanceKilometers + ' kilometers';
document.body.innerHTML=result1;
return drivingDistanceKilometers;
});
}

function showLocation() {
geocoder.getLocations(addr1, function (response) {
if (!response || response.Status.code != 200)
{
alert("Sorry, we were unable to geocode the first address");
}
else
{
location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
geocoder.getLocations(addr2, function (response) {
if (!response || response.Status.code != 200)
{
alert("Sorry, we were unable to geocode the second address");
}
else
{
location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
gDir.load('from: ' + location1.address + ' to: ' + location2.address);
}
});
}
});
}

</script>
</head>

<body onload="coolAl('pune','mumbai')">
</html>

devnull69
07-27-2011, 02:14 PM
This is because the .getLocations() call most probably is asynchronous. Everything you want to do after the .getLocations() finished has to be done (or started from) inside the callback function! A "return" after the .getLocations() will be executed before the method has finished executing!

arpitbandi
07-27-2011, 02:18 PM
can you help me on how to edit it now...?? it'll be gr8 help..

devnull69
07-27-2011, 02:21 PM
geocoder.getLocations(addr1, function (response) {
if (!response || response.Status.code != 200)
{
alert("Sorry, we were unable to geocode the first address");
}
else
{
location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
geocoder.getLocations(addr2, function (response) {
if (!response || response.Status.code != 200)
{
alert("Sorry, we were unable to geocode the second address");
}
else
{
location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
gDir.load('from: ' + location1.address + ' to: ' + location2.address);
// Everything you want to do with the request results has to be done here!
}
});
}
});

arpitbandi
07-27-2011, 02:27 PM
i addedreturn location1.address; to the place you suggested and changed the coolal function to
function coolAl(add1,add2) {
addr1=add1;
addr2=add2;
initialize();
var result=showLocation();
alert(result);
}
but still it says undefined...
sorry for being dumb but im a newbie

devnull69
07-27-2011, 02:46 PM
no no no you can not return anything from there. You have to process the result on that exact place OR call a function/method where you give the request result as a parameter. A return will just do nothing here (there is no longer a function to return from actually).

arpitbandi
07-27-2011, 02:48 PM
k.. then can you please tell the code to take these values to the coolal function?? or may be any tutorial on how to do it.. i read a little but wasnt able to get at all..

devnull69
07-27-2011, 02:51 PM
You started the requests from the coolAl() function already so there's no need to go back to that function. Instead you should write another function that takes the result and does whatever you want with this result. Then you call the new function from the place I suggested earlier.

DaveyErwin
07-27-2011, 06:13 PM
can you help me on how to edit it now...?? it'll be gr8 help..

This might be a solution ?


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<meta name="robots" content="noindex,follow" />
<title>Calculate driving distance with Google Maps API</title>
<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAMK3PClIOG6IUkYprx4EfNxSY_HQRLXr6AGORx7Qh39w3-je8JxRROt5eJTcDPJ9nGnVn9xXKTQ2l8Q" type="text/javascript"></script>
<!-- According to the Google Maps API Terms of Service you are required display a Google map when using the Google Maps API. see: http://code.google.com/apis/maps/terms.html -->
<script type="text/javascript">

var geocoder, location1,addr1,addr2, location2, result1,gDir;
function coolAl(arg) {

alert(arg);
}
function initialize() {
geocoder = new GClientGeocoder();
gDir = new GDirections();
GEvent.addListener(gDir, "load", function() {
var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
result1=location1.address + ' (' + location1.lat + ':' + location1.lon + ')/' + location2.address + ' (' + location2.lat + ':' + location2.lon + ')/' + drivingDistanceKilometers + ' kilometers';
document.body.innerHTML=result1;
coolAl(result1)
return drivingDistanceKilometers;
});
}

function showLocation(addr1,addr2) {
geocoder.getLocations(addr1, function (response) {
if (!response || response.Status.code != 200)
{
alert("Sorry, we were unable to geocode the first address");
}
else
{
location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
geocoder.getLocations(addr2, function (response) {
if (!response || response.Status.code != 200)
{
alert("Sorry, we were unable to geocode the second address");
}
else
{
location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
gDir.load('from: ' + location1.address + ' to: ' + location2.address);
}
});
}
});
}

</script>
</head>

<body onload="initialize();showLocation('pune','mumbai')">
</html>

mathewfarrell35
07-27-2011, 07:20 PM
try to use the finite variables and then define the default value of the variables as zero so that they wont return infinite value.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum