Any way to know if kmllayer fails to load?

202 views
Skip to first unread message

Stephen

unread,
Jan 27, 2011, 4:51:55 PM1/27/11
to Google Maps JavaScript API v3
I have a map that allows users to load arbitrary kml layers. Often a
layer will fail to load, either because the url is incorrect or not
publicly accessible. There is unfortunately no error thrown when this
occurs and as far as I can tell there does not seem to be any way to
detect that the kml has not actually been loaded.

Does anybody have an idea for how I can detect that this has occurred
(and notify the user - it took me at least 15 minutes to realize that
the kml had to be publicly accessible or it would silently fail). As I
understand, using geoxml3 wont help since url may not be served from
same domain.

Thanks,
Stephen

geoco...@gmail.com

unread,
Jan 27, 2011, 5:24:59 PM1/27/11
to Google Maps JavaScript API v3
I don't think there is any way documented.

I believe there are events that fire if the KmlLayer does load.
Cludgy but you could research the maximum time google's servers will
wait for the data and then take to render it, add a safety factor and
if you don't get a positive indication that it loaded, display an
error.

Or you could add an enhancement request to the issues list to add
events for the error case.

-- Larry



>
> Thanks,
> Stephen

Stephen

unread,
Jan 27, 2011, 5:50:30 PM1/27/11
to Google Maps JavaScript API v3
Thank you. I think I will. (http://code.google.com/p/gmaps-api-issues/
issues/detail?id=3015)

Stephen

On Jan 27, 5:24 pm, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:

Joseph Elfelt

unread,
Jan 27, 2011, 5:47:12 PM1/27/11
to Google Maps JavaScript API v3
Have you considered using XMLHttpRequest to try and read the headers
of the kml file.
If you are unable to read those headers (due to the reasons you
describe) then your app could display an error message.

Also, you might experiment with this listen once:

google.maps.event.addListenerOnce(mykml, "defaultviewport_changed",
function() {
// do stuff....
});

Perhaps your app could detect if this listener has *not* fired within
a certain time.

geoco...@gmail.com

unread,
Jan 27, 2011, 6:35:02 PM1/27/11
to Google Maps JavaScript API v3
On Jan 27, 2:47 pm, Joseph Elfelt <josephelf...@gmail.com> wrote:
> Have you considered using XMLHttpRequest to try and read the headers
> of the kml file.

Wouldn't you need to do that through a proxy for it even to attempt to
work? The OP said it was cross-domain (I almost suggested the same
thing...)

-- Larry

Joseph Elfelt

unread,
Jan 27, 2011, 7:07:46 PM1/27/11
to Google Maps JavaScript API v3
Larry has an excellent point.

Firefox 3.5+ is supposed to support cross domain XMLHttpRequests
See: https://developer.mozilla.org/En/HTTP_Access_Control
But it is likely not realistic to expect users to always be using FF
3.5+ or a different browser with the same feature.

Here's a modified suggestion.

If you have access to server-side scripting then you could
1. Send an XMLHttpRequest to your own server
2. That request would cause a script to run on your server
3. That script would simply try to read the kml headers and report
back to the javascript on the client machine

A server-side script can read across domains with no problem.
My app does exactly that.

In fact, you are welcome to use my app to try and read any of your
good/bad/ugly kml files. Bad kml files/urls should produce an error
message.
The simplest syntax is:
http://www.mappingsupport.com/p/gmap4.php?q=http:url_to_your_file.kml




Jeremy Smith

unread,
May 26, 2011, 1:57:38 PM5/26/11
to google-map...@googlegroups.com
I am having a similar issue with KML layers failing to load.  Joseph's solution is interesting, but it will only catch some failures.  KML layers will also fail to load if they contain too many points or are over a certain size.  I really wish there were an event to which I could add a listener on a failure.
Reply all
Reply to author
Forward
0 new messages