[OpenLayers-Users] Using Vector Layer with OSM

Christopher Schmidt crschmidt at metacarta.com
Sun Nov 23 17:26:03 EST 2008


On Sun, Nov 23, 2008 at 11:20:56PM +0100, Christian Hackl wrote:
> Hi,
> 
> This is probably a newbie question, but since I cannot figure out the 
> answer even after days of reading docs, searching the archives and 
> studying examples, I thought I'd just ask, so here we go:
> 
> How does one use a Vector layer upon an OSM base layer?
> 
> All the working Vector examples I've seen use base layers other than 
> OSM. Copying the entire example code with just replacing the base layer 
> yields wrong results. No points or lines are visible anymore, or they 
> all appear centered at the equator no matter which values are chosen as 
> coordinates.
> 
> I'm pretty sure that the problem has to do with projections, but as far 
> as that topic is concerned, I'm completely lost, both on theory and on 
> the practical part. I've tried to set the correct projection for OSM, 
> but none of my trial & error attempts worked. Any hints or reading 
> material on projections in OpenLayers would be very appreciated.

http://docs.openlayers.org/spherical_mercator/#working-with-projected-coordinates

> 
> Here's a simplified example of my problem:
> 
> function init()
> {
>    var map = new OpenLayers.Map("map");
>    map.addControl(new OpenLayers.Control.LayerSwitcher);
> 
> // the following uses WMS and *does* work:
> //  map.addLayer(new OpenLayers.Layer.WMS("OpenLayers WMS",
> //  "http://labs.metacarta.com/wms/vmap0", {layers: 'basic'} ));
> 
> // the following uses OSM and does *not* work:
>    map.addLayer(new OpenLayers.Layer.OSM.Osmarender("Osmarender"));
> 
>    var vectorLayer = new OpenLayers.Layer.Vector("Just a point...");
>    var point = new OpenLayers.Geometry.Point(-111.04, 45.68);

     point.transform(new OpenLayers.Projection("EPSG:4326"), new
     OpenLayers.Projection("EPSG:900913");

>    var pointFeature = new OpenLayers.Feature.Vector(point);
> 
>    map.addLayer(vectorLayer);
>    map.setCenter(new OpenLayers.LonLat(point.x, point.y), 5);
>    vectorLayer.addFeatures([pointFeature]);
> }

Regards,
-- 
Christopher Schmidt
MetaCarta



More information about the Users mailing list