1.MAP:
var map =new ol.Map({
layers: [
vecLayer,
sdmapLayer,
sdxzjLayer,
sdjiaotongLayer,
drawLayerVector,
advertMarkerLayer
],
controls : ol.control.defaults({
attributionOptions : /** @type {olx.control.AttributionOptions} */
({
collapsible : false
})
}),
view : new ol.View({
center : [ 117, 36.60 ],
zoom : 7,
maxZoom : 18,
projection : ol.proj.get('EPSG:4326'),
minZoom : 2
}),
target: 'Map'
});
2.地图打点:
图标数组
var iconStyle1 = new Array(10);
for (var i = 0; i < 10; i++) {
iconStyle1[i] = new ol.style.Style({
image : new ol.style.Icon(/** @type {olx.style.IconOptions} */
({
anchor : [ 0.5, 30 ],
anchorXUnits : 'fraction',
anchorYUnits : 'pixels',
opacity : 1,
scale : 1,
src : "images/marker/icon6" + i + ".png"
})),
zIndex : 12
});
}
点位图层
/**
* 实现popup的html元素
*/
var popupElement = document.getElementById('popup');
/**
* 在地图容器中创建一个Overlay
*/
var popup = new ol.Overlay({
offset : [ 0, -22 ],
element : popupElement,
positioning : 'top-center',
stopEvent : false
});
map.addOverlay(popup);
添加点位
var datas = data.body.data;
for(var i=0;ivar adPos = datas[i];
var item = map.getLayers().item(0);
var coordinate;
if(!adPos.latitude||!adPos.longitude){
continue;
}
coordinate = {'lat':adPos.latitude,'lon':adPos.longitude};
if(ol.proj.equivalent(map.getView().getProjection(),ol.proj.get('EPSG:3857'))){
var bd09 = GPS.bd_encrypt(Number(adPos.latitude),Number(adPos.longitude));
var bd09_3857 = ol.proj.transform([bd09.lon, bd09.lat], 'EPSG:4326', 'EPSG:3857');
coordinate = {'lat' : bd09_3857[1], 'lon' : bd09_3857[0]};
}
var adPosGis = new ol.Feature({
geometry : new ol.geom.Point([coordinate.lon,coordinate.lat ]),
name : 'adPosGis',
hiddIndex : i
});
if(parseInt(adPos.adType)>9){
adPos.adType="9";
}
adPosGis.setStyle(iconStyle1[parseInt(adPos.adType)]);
vectorSource.addFeature(adPosGis);
}
点击事件
map.on('click', function(evt) {
var feature = map.forEachFeatureAtPixel(evt.pixel, function(feature,ayer) {
return feature;
})
if (feature) {
var geometry = feature.getGeometry();
var coord = geometry.getCoordinates();
if ("adPosGis" == feature.get("name")) {
popup.setPosition(coord);
showAdvertInfo(feature);
setSettings();
$(popupElement).webuiPopover('destroy').webuiPopover(settings);
$(popupElement).webuiPopover('show');
}else {
var pan = ol.animation.pan({
duration : 500,
source : (map.getView().getCenter())
});
}
}else{
}
});
var addHTML = document.getElementById("xinxi").innerHTML;
function showAdvertInfo(feature) {
var numx = feature.get('hiddIndex');
var rsjson = datas[numx];
console.log(rsjson);
document.getElementById("xinxi").innerHTML = addHTML;
$('#xinxi').render(rsjson);
}
function setSettings() {
settings = {
trigger : 'click',
title : '',
content : document.getElementById("xinxi").innerHTML,
width : 350,
multi : true,
closeable : false,
cache : false,
style : '',
padding : true
};
}
3.地图拉框
var draw;
var drawType ;
function addInteraction(type){
drawType = type;
drawLayerVector.setVisible(true);
map.removeInteraction(draw);
if (drawSourceVector == null) {
drawSourceVector = new ol.source.Vector({ });
drawLayerVector.setSource(drawSourceVector);
}
var value = type;
var geometryFunction, maxPoints;
if (type === 'Box'){
value = 'LineString';
maxPoints = 2;
geometryFunction = function (coordinates, geometry) {
if (!geometry) {
geometry = new ol.geom.Polygon(null);
}
var start = coordinates[0];
var end = coordinates[1];
geometry.setCoordinates([
[start, [start[0], end[1]], end, [end[0], start[1]], start]
]);
return geometry;
}
}
draw = new ol.interaction.Draw({
source: drawSourceVector,
type: (value),
geometryFunction: geometryFunction,
maxPoints: maxPoints
});
map.addInteraction(draw);
draw.on('drawend', drawEndCallBack,this);
}
function drawEndCallBack(evt) {
vectorSource.clear();
drawSourceVector.clear();
currentDrawFeature = evt.feature;
var geo = currentDrawFeature.getGeometry();
var coordinates;
if(drawType=='Box'){
coordinates = geo.getCoordinates();
if(ol.proj.equivalent(map.getView().getProjection(),ol.proj.get('EPSG:3857'))){
coordinates[0][0] = ol.proj.transform(coordinates[0][0], 'EPSG:3857', 'EPSG:4326');
coordinates[0][2] = ol.proj.transform(coordinates[0][2], 'EPSG:3857', 'EPSG:4326')
}
searchAdvert(coordinates[0][0],coordinates[0][2]);
console.log("Box");
console.log(coordinates[0][0]);
console.log(coordinates[0][2]);
}else if(drawType=='Circle'){
var center = geo.getCenter();
var radius = geo.getRadius();
if(ol.proj.equivalent(map.getView().getProjection(),ol.proj.get('EPSG:3857'))){
center = ol.proj.transform(center, 'EPSG:3857', 'EPSG:4326');
radius = radius/111194.87428468118
}
console.log("Circle");
console.log(ol.proj.METERS_PER_UNIT);
console.log(map.getView().getProjection().getUnits());
console.log(map.getView().getProjection().getMetersPerUnit())
console.log("center:="+center);
console.log("radius:="+radius);
searchAdvert('','',center,radius);
}else{
searchAdvert();
}
map.removeInteraction(draw);
}
function removeInteraction(){
drawSourceVector = null;
drawLayerVector.setSource(drawSourceVector);
map.removeInteraction(draw);
drawLayerVector.setVisible(false);
vectorSource = new ol.source.Vector({
});
advertMarkerLayer.setSource(vectorSource);
$('#statisticsAdvertGis').hide();
}
function searchAdvert(start,end,center,radius){
vectorSource.clear();
var topleft,lowerright;
var params = "1=1";
if(start&&end){
if(start[0]>end[0]){
topleft = end;
lowerright = start;
}else{
topleft = start;
lowerright = end;
}
params += "&minx="+topleft[0];
params += "&maxx="+lowerright[0];
params += "&miny="+lowerright[1];
params += "&maxy="+topleft[1];
}else if(center&&radius){
params += "¢erx="+center[0];
params += "¢ery="+center[1];
params += "&radius="+radius;
}
console.log("执行");
console.log(params);
}