0

Hey guys I am trying to create a KML layer for my google map on my own PHP based website for a project.

The URL for the API I want to use doesn't seem to work for me. The API can be found here. http://openchargemap.org/site/develop/api. Its for electric vehicle charging stations to be presented on the map.

When using other APIs and googles examples, they work fine with my code, using the one I need will not work.

var kmlLayer = new google.maps.KmlLayer({
    url: 'http://api.openchargemap.io/v2/poi/?output=kml&countrycode=GB&maxresults=500',
    suppressInfoWindows: true,
    map: map
});

The above code is inside my initialise map function.

I have tried different variations of the API for the url which none of them work. The example above is for UK results with a maximum of 500 with the output set to KML.

What I have noticed which could be useful is that when this API is entered into the browser with the output as KML, it downloads locally as an unrecognised file where as the google samples on the developers page and other APIS download directly as KML's.

The unrecognised file when opened with Notepad++ is the KML functionality that I want and can be saved as a KML which could be uploaded and used that way. Which makes me think there could be something wrong with the Opencharge map API. But I am all new to this so any help is appreciated!

Thanks guys!

KML layers Google Devlopers page

1 Answer 1

0

The KmlLayer has getStatus() method, which in your case returns FETCH_ERROR. This means that google api has trouble downloading the KML for you. They use their own servers to connect to the Opencharge api and download the KML so it's hard to determine what's the cause. But even when you try to download the KML file yourself, upload it and load from your server, you would get INVALID_DOCUMENT error, meaning the file is not valid KML, or the google maps API cannot parse it. You can check the list of possible statuses in the google maps js api docs.

So yes, there is definitely a problem with the API, not your code. What you can do is create a script on your server which would load the KML, parse it, and create a valid KML which would you then feed to the API. So, you need:

  1. Load the url from your server and parse it as XML (KML is in XML notation). This is an example how to do this in php.
  2. From parsed XML create your own valid KML file containing just the location coordinations and any necessary data.
  3. Make your script return this newly created KML.
  4. Load your script's url instead of the original API, e.g:

    var kmlLayer = new google.maps.KmlLayer({ url: 'http://yourserver/yourscript&optionalparameters', suppressInfoWindows: true, map: map });

Sign up to request clarification or add additional context in comments.

2 Comments

Yes this makes sense, I will give this a shot. I did exactly what you did and visted the URL im trying to get the KML from and its downloading as an unrecognised file for me too.. Thanks for the reply!
@aa29 it doesn't really matter if the file appears as unrecognised as long as the data in it are valid. But in this case the data are not valid KML file, at least not the kind that google maps api can parse. No problem with the reply, If you like the answer you can upvote it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.