jquery ajax获取geojson格式文件内容,通过openlayers叠加显示在地图。
jquery ajax获取geojson格式文件内容,通过openlayers叠加显示在地图
代码如下:
//—————————————————————————根据行政区编码进行定位———————————————————。
var Mmylayer;
function OverLayTargetLocalLyr(dir)
{
var featureSelectStyle = new ol.style.Style({
stroke: new ol.style.Stroke({
color: [255, 215, 0, 1],
width: 3
}),
fill: new ol.style.Fill({ color: [255, 215, 0, 0.2] })
});
$.getJSON(“js/AnHui_XZQ_Geojson/”+dir+”000000.json”,function(data){
var myfeatures = (new ol.format.GeoJSON({featureProjection: ‘EPSG:3857’})).readFeatures(data);
var myvectorSource = new ol.source.Vector({
features: myfeatures
});
myfeatures[0].setStyle(featureSelectStyle);
if(Mmylayer !=”” && Mmylayer !=”undefine”)
{
obj.map.removeLayer(Mmylayer);
}
Mmylayer = new ol.layer.Vector({
id: “GH_001”,
zIndex: 888888,
source: myvectorSource
});
obj.map.addLayer(Mmylayer);
});
}
//—————————————————————根据行政区编码查询定位函数————————————————————————–
obj.LocalByAddrCode=function(addrcode){
if(addrcode!=null && addrcode!=””)
{
OverLayTargetLocalLyr(addrcode);
}
}
注意:geojson要和地图投影一致!
转载自:https://blog.csdn.net/pdw521/article/details/88288662