简体   繁体   中英

OpenStreetMap point not showing on map with open layers

var map;
var vectors;
var point;
var drag;

Any long and Lat can be used

function mapCreate(lon,lat){
            map = new OpenLayers.Map("map1");
            var osm = new OpenLayers.Layer.OSM();
            //create a vector
            vectors = new OpenLayers.Layer.Vector("Vector Layer");
            map.addLayer(osm);
            var center = new OpenLayers.LonLat(lon,lat).transform(
                new OpenLayers.Projection("EPSG:4326"),
                map.getProjectionObject()
            );

Assign a lat long to the point

point = new OpenLayers.Geometry.Point(lat,lon);

Add point to vectors

        vectors.addFeatures([new OpenLayers.Feature.Vector(point)]);

        map.setCenter(center, 15);
        //add vectors to map
        map.addLayer(vectors);

    }

Am I missing something?

Are you looking at the full map? There's a high chance that you're setting the point's location as lat/lon. The OpenLayers LonLat object is so named only to trick innocent users like you into thinking that it automatically converts latitude longitude, or expects them, or something. Don't trust it, reproject into the projection of your map.

I thought Collection were necessary, but looks like you have lat & lon swapped. A point must have lon, then lat.

feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Collection([new OpenLayers.Geometry.Point(0, 0)]), {});
vectors.addFeatures([feature]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM