I need to add a marker on my map when I call the method addMarker(long, lat).
The only example I have found is this one in the OpenLayers example list, but it adds only one marker in a specific place.
I need to be able to call my method anytime I need and pass the coordinates (the general idea is to have a simple menu where the user can type the long and lat and click a submit button and the marker is drawn on the map).
In the example, if I got it right, they created a feature and its style, then created a layer to draw the icon and initialized the map using that layer.
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([-72.0704, 46.678], 'EPSG:4326', 'EPSG:3857')),
name: 'Null Island',
population: 4000,
rainfall: 500
});
var iconStyle = new ol.style.Style({
image: new ol.style.Icon(/** #type {olx.style.IconOptions} */ ({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
opacity: 0.75,
src: 'data/icon.png'
}))
});
iconFeature.setStyle(iconStyle);
var vectorSource = new ol.source.Vector({
features: [iconFeature]
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
var map = new ol.Map({
layers: [new ol.layer.Tile({ source: new ol.source.OSM() }), vectorLayer],
target: document.getElementById('map'),
view: new ol.View2D({
center: [0, 0],
zoom: 3
})
});
I believe I can create an array of features, something like that(an example I saw here):
var iconFeatures=[];
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([-72.0704, 46.678], 'EPSG:4326',
'EPSG:3857')),
name: 'Null Island',
population: 4000,
rainfall: 500
});
var iconFeature1 = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([-73.1234, 45.678], 'EPSG:4326',
'EPSG:3857')),
name: 'Null Island Two',
population: 4001,
rainfall: 501
});
iconFeatures.push(iconFeature);
iconFeatures.push(iconFeature1);
Here is my method so far:
class map{
private markerFeatures = [];
//receive an array of coordinates
public addMarkers(markers: Array<Marker>): void {
for (var i in markers) {
var markFeature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([markers[i].long, markers[i].lat], 'EPSG:4326',
'EPSG:3857')),
name: 'Null Island',
population: 4000,
rainfall: 500
});
this.markerFeatures.push(markFeature);
}
}
}
But nothing happened when I did that.
Obs: I created the map, the layer and called the method.
I'm using OL v3.7
Assuming you have a layer containing a vector source, you just need to add one more step to your addMarkers function:
myVectorSource.addFeatures(markerFeatures);
Related
How can I change the standard blue color and size of points in openlayers 3?
I do like this:
while (lats.length > 0) {
var layer = new ol.layer.Vector({
source: new ol.source.Vector({
features: [
new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([lons.shift(), lats.shift()], 'EPSG:4326', 'EPSG:3857'))
})
]
})
});
map.addLayer(layer);
}
If I add styles then either nothing changes, or everything does not work. How to add styles here?
this is answer:
var vectorSource = new ol.source.Vector({
//create empty vector
});
var styleGreen = new ol.style.Style({
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({
color: 'green'
})
})
});
while (lats.length > 0) {
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([lons.shift(), lats.shift()], 'EPSG:4326', 'EPSG:3857'))
});
vectorSource.addFeature(iconFeature);
}
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: styleGreen
});
map.addLayer(vectorLayer);
I searched without success how to add some "arrow direction" on a GPX trace.
I used this code to display a trace :
var center = ol.proj.transform([4.90756, 45.5172], 'EPSG:4326', 'EPSG:3857');
var map = new ol.Map({
layers: [new ol.layer.Tile({source: new ol.source.OSM()})],
target: 'map',
controls: ol.control.defaults({attributionOptions: ({ collapsible: false})}),
view: new ol.View({center: center,zoom: 12})
});
var vectorGpx = new ol.layer.Vector({
source: new ol.source.Vector({
url: 'http://www.vlsm.fr/test/test1.gpx',
format: new ol.format.GPX()
}),
style: new ol.style.Style({stroke: new ol.style.Stroke({color: '#1aa79b',width: 3})})
});
map.addLayer(vectorGpx);
http://jsfiddle.net/bzyev869/
Does something exist to do that?
openlayers 3 doesnt have inbuilt functionality to add arrow-style.However,you can calculate direction between lat long and add arrow icon.Here is the fiddle http://jsfiddle.net/bzyev869/2/
for(var i=0;i<vectorGpx.getSource().getFeatures()[0].getGeometry().getCoordinates()[0].length;i++){
var s= vectorGpx.getSource().getFeatures()[0].getGeometry().getCoordinates()[0][i];
if(i+1 <vectorGpx.getSource().getFeatures()[0].getGeometry().getCoordinates()[0].length){
var f= vectorGpx.getSource().getFeatures()[0].getGeometry().getCoordinates()[0][i+1];
}
var dx = s[0] - f[0];
var dy = s[1] - f[1];
var rotation = Math.atan2(dy, dx);
styles.push(new ol.style.Style({
geometry: new ol.geom.Point([f[0],f[1]]),
image: new ol.style.Icon({
src: 'http://openlayers.org/en/v3.14.2/examples/data/arrow.png',
anchor: [0.75, 0.5],
rotateWithView: false,
rotation: -rotation
})
}));
}
I am trying to load my layer from Geoserver as WFS layer in my OL3 application.
Here is my code to create WFS layer:
var vectorSource = new ol.source.Vector({
format: new ol.format.GeoJSON(),
url: function(extent, resolution, projection) {
return 'http://localhost:8080/geoserver/wfs?service=WFS&' +
'version=1.0.0&request=GetFeature&typename=main:Building_WGS&' +
'outputFormat=application/json&srsname=EPSG:4326&';
},
strategy: ol.loadingstrategy.tile(ol.tilegrid.createXYZ({
}))
});
var vector1 = new ol.layer.Vector({
source: vectorSource,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'rgba(0, 0, 255, 1.0)',
width: 2
})
})
});
To make my code complete here is the code where I have created map object:
var centerpos = [84.2, 28.2];
var newpos = ol.proj.transform(centerpos,'EPSG:4326','EPSG:900913');
var maxExtent = [80.05844110726194,26.34796712822462,88.2015218371264,30.44742963310623];
var map = new ol.Map({
layers: [baseLayerOSM, vector1],
target: 'map',
//controls: [new CustomControl()],
view: new ol.View({
extent:ol.proj.transformExtent(maxExtent, 'EPSG:4326', 'EPSG:900913'),
projection : 'EPSG:900913', // OSM projection
center : newpos,
minZoom:7,
zoom: 7
})
});
I can see the OSM layer in my map. But I can't see the layer Building_WGS. I have a workspace main in geoserver.
When I copy the URL
'http://localhost:8080/geoserver/wfs?service=WFS&' +
'version=1.0.0&request=GetFeature&typename=main:Building_WGS&' +
'outputFormat=application/json&srsname=EPSG:4326&';
I can see the JSON contents.
Can anyone suggest what am I doing wrong in my code?
I even tried to add crossOrigin:"Anonymous" but that doesn't help either.
HELP please
AJ
I've been trying the new version (3) of Open Layers. I've modified the icon feature example slightly, so it would show a polygon. I've been searching, reading and trying for a couple of hours, but can't figure out what I'm doing wrong.
I don't want to use geoJSON, because I want to dynamically add and remove features.
This is the code I have so far:
<script type="text/javascript">
var point1 = ol.proj.transform([37.41, 8.82], 'EPSG:4326', 'EPSG:3857');
var point2 = ol.proj.transform([35.41, 9.82], 'EPSG:4326', 'EPSG:3857');
var point3 = ol.proj.transform([33.41, 11.82], 'EPSG:4326', 'EPSG:3857');
var polyFeat = new ol.Feature({
geometry: new ol.geom.Polygon([point1, point2, point3])
});
var polyStyle = new ol.style.Style({
fill: new ol.style.Fill({
color: 'blue'
}),
stroke: new ol.style.Stroke({
color: 'red',
width: 2
})
});
polyFeat.setStyle(polyStyle);
var vectorSource = new ol.source.Vector({
features: [polyFeat]
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
var rasterLayer = new ol.layer.Tile({
source: new ol.source.TileJSON({
url: 'http://api.tiles.mapbox.com/v3/mapbox.geography-class.jsonp'
})
});
var map = new ol.Map({
layers: [rasterLayer, vectorLayer],
target: document.getElementById('map'),
view: new ol.View({
center: ol.proj.transform([37.41, 8.82], 'EPSG:4326', 'EPSG:3857'),
zoom: 3
})
});
</script>
Why is the polygon not showing?
Two small things to solve your issue:
First, it's recommended to close a polygon, so declare a fourth point with same coordinates as the first.
var point4 = ol.proj.transform([37.41, 8.82], 'EPSG:4326', 'EPSG:3857');
Then, your geometry new ol.geom.Polygon([point1, point2, point3]) should be new ol.geom.Polygon([[point1, point2, point3, point4]])
The important fact here is not the point4 addition but to transform your array point to an array of array of points. See the API that says OpenLayers 3 ol.geom.Polygon constructor expects an Array.<Array.<ol.Coordinate>> expected.
I have created a map with OpenLayers 3 using static image. It uses fake projection so the map can use to properly display the layer as it's measured in pixels. This is the code:
var pixelProjection = new ol.proj.Projection({
code: 'pixel',
units: 'pixels',
extent: [0, 0, 1389, 1070]
});
var map = new ol.Map({
layers: [
new ol.layer.Image({
source: new ol.source.ImageStatic({
url: 'http://s25.postimg.org/4o15oqbmn/jdgf.jpg',
imageSize: [1389, 1070],
projection: pixelProjection,
imageExtent: pixelProjection.getExtent()
})
})
],
target: 'map',
view: new ol.View2D({
projection: pixelProjection,
center: ol.extent.getCenter(pixelProjection.getExtent()),
zoom: 2
})
});
I was trying to add marker overlays to add some more interactions however I'm struggling to specify position and the markers is outside the map rather then inside where I want to place it.
var marker = new ol.Overlay({
position: [200, 200],
element: document.getElementById('marker'),
stopEvent: false
});
I'm very novice to this so if anyone have an idea how to set position correctly I will be thankful.
Using this OpenLayers 3 map as an example, I was able to add a marker to the center of your static image with the following:
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point([700, 700])
});
var iconStyle = new ol.style.Style({
image: new ol.style.Icon({
src: 'http://ol3js.org/en/master/examples/data/icon.png'
})
});
iconFeature.setStyle(iconStyle);
var vectorSource = new ol.source.Vector({
features: [iconFeature]
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});