官术网_书友最值得收藏!

Plotting messages on the map

We get all the messages based on the extent.

ol.source.Vector

This is used to provide a feature source for the layers. In simple terms, it is used to add a marker on the map.
It has the following options:

  • loader: This is used to load features like from a remote source. In our case, we are fetching from a remote source using AJAX.
  • strategy: This is used to set the strategy to load the features. Default uses ol.loadingstrategy.all, which loads all the features at once.

ol.Feature

A vector object representing the geographic features with a geometry and other attribute properties. It usually has a single geometry property, which in our case is used to add a marker to the specified coordinates using ol.geom.Point, which holds the co-ordinate:

    /* Show messages on the Map */

var vectorSource = new ol.source.Vector({
loader: function (extent, resolution, projection) {
var url = '/message/bbox/' + extent[0] + "," + extent[1] + ","
+ extent[2] + "," + extent[3];
// Get all the messages from the server based upon the extent
$.ajax({
url: url,
dataType: 'json',
success: function (response) {
if (response.error) {
alert(response.error.message);
} else {
// Plot message icon on the map
$.each(response, function (index, value) {
var feature = new ol.Feature({
geometry: new
ol.geom.Point(value.location.coordinates),
content: value.content
});
vectorSource.addFeature(feature);
});
}
}
});
},
strategy: ol.loadingstrategy.tile(ol.tilegrid.createXYZ({
tileSize: 512
}))
});

We plot on the map by adding a layer of vector and setting the source of this vector data to our previous vectorSource where we actually fetch the messages from the backend:


// Styling the vector to message icon
var vector = new ol.layer.Vector({
source: vectorSource,
style: new ol.style.Style({image: new ol.style.Icon({src:
"message-box.png", scale: 0.5})})
});
map.addLayer(vector);
主站蜘蛛池模板: 即墨市| 阿克陶县| 浦东新区| 绥芬河市| 德钦县| 上饶市| 卓尼县| 湖口县| 双柏县| 高雄县| 姚安县| 抚顺市| 五原县| 盐城市| 三穗县| 酒泉市| 襄垣县| 弋阳县| 平和县| 枣强县| 井研县| 龙川县| 兴文县| 贺州市| 永州市| 茌平县| 禹州市| 临洮县| 石棉县| 马龙县| 花垣县| 蛟河市| 乌鲁木齐县| 邮箱| 阜康市| 康乐县| 河南省| 伊金霍洛旗| 尚志市| 察隅县| 兴宁市|