2

I am trying to read an KML file (point feature) and show it on a vector layer on OSM as the base map. My code seems to have a problem that does not show the features on the map. I don't now if its the problem with reading the data from KML file or is it related to visualizing it by Openlayers. Any help would be appreciated... Thanks!

My code:

<script src="http://www.openlayers.org/api/OpenLayers.js"></script>
    <script>
      function init() {
        map = new OpenLayers.Map("map");
        var mapnik         = new OpenLayers.Layer.OSM();
        var fromProjection = new OpenLayers.Projection("EPSG:4326");   // Transform from WGS 1984
        var toProjection   = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
        var position       = new OpenLayers.LonLat(48.69,31.345).transform( fromProjection, toProjection);
        var zoom           = 12; 
        var layer = new OpenLayers.Layer.Vector("KML", {
            strategies: [new OpenLayers.Strategy.Fixed()],
            protocol: new OpenLayers.Protocol.HTTP({
            url: "points.kml",
            format: new OpenLayers.Format.KML()
            })
        });
        map.addLayers([mapnik, layer]);
        map.addControl(new OpenLayers.Control.LayerSwitcher());
        map.setCenter(position, zoom );
      }

and my kml data:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<name>Agencies test</name>
<Folder>
<name>BlahBlah!</name>
<Placemark id="2">
<name>Something here!</name>
<description>test</description>
<LookAt>
<longitude>49.69</longitude>
<latitude>32.345</latitude>
<altitude>0</altitude>
<range>24.63686803544318</range>
<tilt>0</tilt>
<heading>1.387289180270979e-005</heading>
</LookAt>
<Point>
<coordinates>49.69,32.345,0</coordinates>
</Point>
</Placemark>
</Folder>
</Document>
</kml>

4 Answers 4

1

Enable web developer extensions and post a better error report - what shows up in the console - please.

4
  • I cannot see any error reports to share with you, the page loads, the OSM map loads, but there is no feature symbols on the map...
    – Amin M
    Oct 4, 2012 at 18:56
  • Have you enabled developer extensions?
    – tmcw
    Oct 4, 2012 at 20:18
  • yes, but as i mentioned, I don't see any error reports. the page loads fine and shows my vector layer (in layer switcher), but the problem is that it doesn't show my data in KML (point symbols)... any clue?
    – Amin M
    Oct 4, 2012 at 20:23
  • I checked it. It seems that the kml file could not be accessed. it gives an error 404 (not found). but I replaced it with a textfile and it worked fine. Is it something related to server settings issue, should I store KML files in a special path in the server???
    – Amin M
    Oct 5, 2012 at 10:23
1

I kind of have a hunch that if you loose the fromProjection and toProjection definitions for your map it will display fine. Last week I tried to force the projections as well together with a KML file as data source and it only worked (and displayed fine) when I removed any explicit projection attributes. Just hinting, might be worth a try (I use latest openlayers release from git repo).

another thing you could try on the kml layer itself is set the projection as such:

        projection: map.displayProjection

Keep the info coming.

0

Your data needs to be in a Geographic xml-formatted format. e.g. - KML or GeoRSS or WFS. Openalyers.Format.XML is really there as a helper class to read generic XML files, so you can't use it as a Layer Format on the map. And then add the layer to your map with that specific format (again... KML or GeoRSS...)

11
  • thank you for your response. I changed it to KML format. and changed the code as well, but still does not work. any clue?
    – Amin M
    Oct 4, 2012 at 18:27
  • btw, the code is now changed to: var layer = new OpenLayers.Layer.Vector("KML", { strategies: [new OpenLayers.Strategy.BBOX({resFactor: 1.1})], protocol: new OpenLayers.Protocol.HTTP({ url: "points.kml", format: new OpenLayers.Format.KML() }) });
    – Amin M
    Oct 4, 2012 at 18:28
  • You changed the code, but did you change the actual file itself? If so, post new code and file up top. And to debug, add Firebug to Firefox and turn on with f12, or use f12 in Chrome (tools built in) to see any messages.
    – Vadim
    Oct 4, 2012 at 19:27
  • Yes, I have also changed the datasource from XML to KML as well. The codes and KML file are now updated up top. Used f12 in Chrome, but does not show any messages (error reports). The page loads, the OSM map loads, but there is no feature symbols on the map...
    – Amin M
    Oct 4, 2012 at 19:41
  • Not seeing new code. by "up top" I mean here, in your original post.
    – Vadim
    Oct 4, 2012 at 19:43
0

Your code works fine for me. Simply zoom out to view your KML point.

The problem is your point in the KML is at 49.69,32.345 but the position you are zooming in on is at 48.69,31.345.

enter image description here

7
  • How is that possible? I'm using Chrome, what browser are u using? maybe its related to browser???
    – Amin M
    Oct 4, 2012 at 21:17
  • are u using the same code of mine? or making some editings? do you define a symbol to show on map for the circle or is it by Openlayers default settings?
    – Amin M
    Oct 4, 2012 at 21:18
  • Would you mind sharing the code that worked with me?? you can also email it to me: [email protected]
    – Amin M
    Oct 5, 2012 at 10:02
  • did you store the KML file in a special path? For me the problem is that it cannot find and access the KML file on the server. How can I solve this problem?
    – Amin M
    Oct 5, 2012 at 10:25
  • Okay I uploaded my code here. (using the same code as you) Though strangely enough, it DOES NOT WORK on the web server. But it does work locally when I point my firefox browser to file:///D:/temp/test/jsdev/kml_test2.html Very weird indeed.
    – CaptDragon
    Oct 5, 2012 at 20:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.