v3 set html variabele in downloadUrl

74 views
Skip to first unread message

David van der Tuijn

unread,
Aug 20, 2010, 3:26:45 PM8/20/10
to Google Maps JavaScript API v3
Hey all,

I am using this tutorial : http://code.google.com/intl/nl-NL/apis/maps/articles/phpsqlajax_v3.html

I would like to send the html response from downloadUrl to
bindInfoWindow :

[code]
var html = "<b>" + id + "</b>";

downloadUrl("google-maps/user/" + id, function(data) {
var html = data.responseText; // contains a HTML page, tested it with
alert().
});

alert(html);

bindInfoWindow(marker, map, infoWindow, html);
[/code]

However, the alert result is '<b>1</b>', how come the html variabele
is not set to data.responseText ?

i tryed with and without var, so it can't be the scope?!?

Help appreciated!

geoco...@gmail.com

unread,
Aug 20, 2010, 6:46:01 PM8/20/10
to Google Maps JavaScript API v3
On Aug 20, 12:26 pm, David van der Tuijn <da...@vandertuyn.demon.nl>
wrote:
> Hey all,
>
> I am using this tutorial :http://code.google.com/intl/nl-NL/apis/maps/articles/phpsqlajax_v3.html
>
> I would like to send the html response from downloadUrl to
> bindInfoWindow :
>
> [code]
> var html = "<b>" + id + "</b>";
>
> downloadUrl("google-maps/user/" + id, function(data) {
>         var html = data.responseText; // contains a HTML page, tested it with
> alert().
>
> });
>
> alert(html);
>
> bindInfoWindow(marker, map, infoWindow, html);
> [/code]
>
> However, the alert result is '<b>1</b>', how come the html variabele
> is not set to data.responseText ?

downloadUrl is asynchronous. You have to use the value returned when
it arrives from the server (in the call back function).

See this page from Mike Williams' v2 tutorial:
Javascript Concepts - Part 2 Asynchronous I/O
http://econym.org.uk/gmap/async.htm

If still can't get it working, please follow the posting guidelines
and post a link to your page that exhibits the problem.

-- Larry

David van der Tuijn

unread,
Aug 22, 2010, 4:15:44 PM8/22/10
to Google Maps JavaScript API v3
I created a Callback function (MyCallback), but i recieve the
following error in the firefox console : data2 is not defined.

http://www.tfalcon.nl/google/test.html

geoco...@gmail.com

unread,
Aug 22, 2010, 5:31:11 PM8/22/10
to Google Maps JavaScript API v3
On Aug 22, 1:15 pm, David van der Tuijn <davidvandertu...@gmail.com>
wrote:
> I created a Callback function (MyCallback), but i recieve the
> following error in the firefox console : data2 is not defined.

Without looking, I suspect that is because data2 is not defined. Why
do you think it is defined where you use it?

>
> http://www.tfalcon.nl/google/test.html

After looking at your page, I see the problem. The error is being
reported on this line (where data2 is not defined):
downloadUrl("user.php?id=" + id, MyCallback(data2));

the second argument to downloadUrl is supposed to be a function
pointer, which can't take arguments:
downloadUrl("user.php?id=" + id, MyCallback);

When you use the original syntax, it executes the MyCallback function
and uses the returned value (null), as the function.


-- Larry

David van der Tuijn

unread,
Aug 23, 2010, 11:29:21 AM8/23/10
to Google Maps JavaScript API v3
Ok, i've made two solutions both have the same result.

MyCallback :
http://tfalcon.nl/google/test2.html

function(data) :
http://tfalcon.nl/google/test3.html

I thought i was done, but only 1 out of 3 markers display an
infoWindow when clicked on the marker, what happend with the other 2
listeners in bindInfoWindow ?

Rossko

unread,
Aug 23, 2010, 11:39:26 AM8/23/10
to Google Maps JavaScript API v3
> MyCallback :http://tfalcon.nl/google/test2.html

function MyCallback(request, status) {
var html = request.responseText;
... }

'html' will be available after the data has been returned via AJAX
(which may be some time after you requested it) but will become
unavailable again after MyCallback() completes, because it was created
in local scope.
If you want to use data from an asynchronous request, you have to USE
it in the callback.

For some reason you are attempting to download all the infowindow
content at the same time as the marker locations, when first
populating the map.
If you are going to do that, you might as well put the infowindow
content in the same data package as the marker locations.

If, instead, you want to load the infowindow content only when it is
needed, you could put the downloadUrl() request into a marker click
listener, and have the callback for that open the infowindow with the
content.

David van der Tuijn

unread,
Aug 28, 2010, 5:46:22 PM8/28/10
to Google Maps JavaScript API v3
On 23 aug, 17:39, Rossko <ros...@culzean.clara.co.uk> wrote:
> If, instead, you want to load the infowindow content only when it is
> needed, you could put the downloadUrl() request into a marker click
> listener, and have the callback for that open the infowindow with the
> content.

Thanks, for this answer!

Working example :
http://tfalcon.nl/google/test4.html
Reply all
Reply to author
Forward
0 new messages