Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

im getting the KML value from my postgis database.. st_askml()

And the output is like this:

"<Polygon><outerBoundaryIs><LinearRing><coordinates>-99.999999990686774,48.499999959021807 -99.999999990686774,47.999999988824129 -99.500000020489097,47.999999988824129 -99.500000020489097,48.499999959021807 -99.999999990686774,48.499999959021807</coordinates></LinearRing></outerBoundaryIs></Polygon>"

Now i need to display them on a Openlayers Map i got.

Layer: var KML_Layer = new OpenLayers.Layer.Vector('KML');

Formatting code:

function GetFeaturesFromKMLString (strKML) {
var format = new OpenLayers.Format.KML({
        'internalProjection': options.projections.source, //EPSG:3785/900913
        'externalProjection': options.projections.display //EPSG:4326
    });

return format.read(strKML);
};

Adding the feature to the layer:

KML_Layer.addFeatures(value.st_askml);

Debuggin info:

value.st_askml = The output format i pasted earlier in topic.

After running it trough the GetFeaturesFromKMLString() its set to [] (No value here)

How can i get this to display correctly on the map?

share|improve this question
    
Can you try manually adding the <kml><placemark>...</placemark></kml> bits around the strKML? –  BradHards Jan 9 '13 at 9:45
    
Done and failed =/ I tried adding it like this: <kml><placemark>'+value.st_askml+'</placemark></kml> - Before it was sent to the KML reader. And also tried inserting the string directly to the vector. no result still .. Console.log() still gives me [] –  Christer Jan 9 '13 at 9:49
    
Please show the exact string you are putting into addFeatures()? Did you pass an xmlns for whatever version of KML you are trying to use? Does it work if you use the examples from projects.opengeo.org/mobile/browser/demos/OL-investigation/… ? –  BradHards Jan 9 '13 at 10:09

1 Answer 1

up vote 1 down vote accepted

ST_AsKML() function doesnt return you valid kml file for openlayers or google earth. the result you have seen being formed from raw coordinates for kml file. you can achive this with following code which @BradHards has mentioned.

put your output between <Placemark>your output</Placemark>. you can try it whether works or not with google earth. i have tested it and has worked.

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>
  <Style id="0">    
  </Style> 
    <Placemark>           
     <Polygon>
      <outerBoundaryIs>
       <LinearRing>
        <coordinates>
        -99.999999990686774,48.499999959021807 
        -99.999999990686774,47.999999988824129 
        -99.500000020489097,47.999999988824129 
        -99.500000020489097,48.499999959021807 
        -99.999999990686774,48.499999959021807
        </coordinates>
       </LinearRing>
      </outerBoundaryIs>
     </Polygon>
    </Placemark>
</Document>
</kml>

i hope it helps you...

share|improve this answer
    
So the st_askml wont work with a JSON request. i have to make the request get an xml/kml document? –  Christer Jan 9 '13 at 10:38
    
Pulled it off by pasting the result as an XML/KML instead of Json :) Thanks! –  Christer Jan 9 '13 at 10:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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