[OpenLayers-Users] kml projection

Saris Hein hein at beta9.be
Fri Oct 10 10:58:57 EDT 2008


Hi,

I am trying to show an inline kml string onto a OSM layer (using the  
proper projection transformations).

Everything seems to work, except the features in the KML string are  
not shown properly.
I use this code:

         function init(){
             var options = {
                 projection: new OpenLayers.Projection("EPSG:900913"),
                 displayProjection: new OpenLayers.Projection("EPSG: 
4326"),
                 units: "km",
                 numZoomLevels: 18,
                 maxResolution: 156543.0339,
                 maxExtent: new OpenLayers.Bounds(-20037508, -20037508,
                                                  20037508, 20037508.34)
             };
             map = new OpenLayers.Map('map', options);

     		// create OSM layer
     		var mapnik = new OpenLayers.Layer.TMS(
         		"OpenStreetMap (Mapnik)",
           		"http://a.tile.openstreetmap.org/",
         		{
             		type: 'png', getURL: osm_getTileURL,
             		displayOutsideMaxExtent: true,
             		attribution: '<a href="http:// 
www.openstreetmap.org/">OpenStreetMap</a>'
         		}
     		);

	    map.addLayer(mapnik);

			// Add a KML as inline String
			var kmlString = '<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.1 
"><Document><Style id="marker"><IconStyle><Icon><href>http://localhost:3000/openlayers/img/marker-green.png 
</href></Icon></IconStyle></Style><Placemark id="13856"><name>place  
name</name><description>NULL::character varying</ 
description><styleUrl>#marker</ 
styleUrl><Point><altitudeMode>relativeToGround</ 
altitudeMode><coordinates>23.29555556,-17.98611111</coordinates></ 
Point></Placemark><Placemark id="13852"><name>place name 2</ 
name><description>NULL::character varying</ 
description><styleUrl>#marker</ 
styleUrl><Point><altitudeMode>relativeToGround</ 
altitudeMode><coordinates>24.56638889,-17.54027778</coordinates></ 
Point></Placemark></Document></kml>';
			
			kmlLayer = new OpenLayers.Layer.Vector("KML", {
					projection: new OpenLayers.Projection("EPSG:4326"),
					format: OpenLayers.Format.KML,
					formatOptions: {
						extractStyles: true,
						extractAttributes: true
						}
					});
					
			var kmlParser = new OpenLayers.Format.KML();
			var kmlFeatures = kmlParser.read(kmlString);

			kmlLayer.addFeatures(kmlFeatures);

			
			map.addLayer(kmlLayer);
			
     		bounds = getFeaturesBounds(kmlLayer.features);
                  
map.zoomToExtent(bounds.transform(map.displayProjection,  
map.getProjectionObject()));
  		}

When I read the same KML from a file and use this code, the data is  
shown correctly:

			kmlLayer = new OpenLayers.Layer.GML("KML", "kml/data.kml",
			      {
			      projection: new OpenLayers.Projection("EPSG:4326"),
			      format: OpenLayers.Format.KML,
			      formatOptions: {
			        extractStyles: true,
			        extractAttributes: true
			        }
			      });
			
My guess is that I should do something else to add the features to the  
kmlLayer. Like a projection maybe?

Thanks for your time.
--
Hein Saris - mailto:hein at beta9.be
Beta Nine - software engineering - http://www.beta9.be
.Mac - heinsaris at mac.com - http://homepage.mac.com/heinsaris
Skype: heinsaris






More information about the Users mailing list