var mapConfig = window[window.EASYMAP_CONFIG['mapConfigName']];
var xzxqLayerGruop ;
function initMap(divId){
// console.log(mapConfig)
if ( mapConfig.option.crs === 'EPSG4326' ){
mapConfig.option.crs = L.CRS.EPSG4326;
}else if (mapConfig.option.crs === 'EPSG3857'){
mapConfig.option.crs = L.CRS.EPSG3857;
}
var map = L.map(divId,mapConfig.option);
// newLayerAddToMap(mapConfig.layers[0],map);
initControl(map);
initMapEvent(map);
// console.log(map.pm)
map.pm.setLang('zh');
window.map = map;
return map;
}
function initControl(map){
setLayerControl(map);
L.control.zoom({position: 'bottomright',zoomInTitle:'放大',zoomOutTitle:'缩小'}).addTo(map);
var topright = L.control.attribution({position: 'topright',prefix:''}).addTo(map);
var rightTopTool1 = '
';
}
if(i == 0 && data[i].level > 3){
htmlList += '
';
}
//加载辖区边界
//drawBoundOnMap(data[i].code);
}
for( var i in data )
{
if( data[i].childrens.length > 0 )
{
htmlList += initCityList(data[i].childrens);
//htmlList += '
'+initCityList(data[i].childrens)+'
';
}
}
return htmlList;
}
function selectCity(thi){
if( thi.className.indexOf('ui3-city-change-click') === -1 )
{
thi.className = thi.className+' ui3-city-change-click';
document.getElementsByClassName('map_popup')[0].style.display = 'block';
}else{
thi.className = thi.className.replace('ui3-city-change-click' , '');
document.getElementsByClassName('map_popup')[0].style.display = 'none';
}
}
function closeMapPoup(){
document.getElementById('ui3_city_change').className = document.getElementById('ui3_city_change').className.replace('ui3-city-change-click' , '');
document.getElementsByClassName('map_popup')[0].style.display = 'none';
}
function traffic(thi,name){
var map = window.map;
if(!map[name]){
for( var i in mapConfig.overlayMaps ){
if( name === mapConfig.overlayMaps[i].name ){
if(!map[name]){
map[name] = {};
}
map[name].bjLayer = newLayerByType(mapConfig.overlayMaps[i]).addTo(map);
map[name].flag = true;
}
}
thi.children[0].className = thi.children[0].className+' active';
thi.children[1].className = 'active';
}else{
if(map[name]&&!map[name].flag)
{
map[name].bjLayer.addTo(map);
map[name].flag = true;
thi.children[0].className = thi.children[0].className+' active';
thi.children[1].className = 'active';
}else{
map[name].flag = false;
if(map.hasLayer(map[name].bjLayer)){
map.removeLayer(map[name].bjLayer);
}
thi.children[0].className = thi.children[0].className.replace('active','');
thi.children[1].className = '';
}
}
}
//度转度°分′秒″
function ToDegrees(val) {
val = val.toString();
if (typeof (val) == "undefined" || val == "") {
return "";
}
var i = val.indexOf('.');
var strDu = i < 0 ? val : val.substring(0, i);//获取度
var strFen = "0";
var strMiao = "0";
var preMiao = "0";
var sufMiao = "0";
if (i > 0) {
var strFen = "0" + val.substring(i);
strFen = strFen * 60 + "";
i = strFen.indexOf('.');
if (i > 0) {
strMiao = "0" + strFen.substring(i);
strFen = strFen.substring(0, i);//获取分
strMiao = strMiao * 60 + "";
i = strMiao.indexOf('.');
strMiao = strMiao.substring(0, i + 4);//取到小数点后面三位
strMiao = parseFloat(strMiao).toFixed(2);//精确小数点后面两位
var j = strMiao.indexOf(".")
if(j>0){
preMiao = strMiao.substring(0,j)
sufMiao = strMiao.substring(j+1)
}
}
}
return strDu + "°" + (strFen.length == 2 ? strFen : "0"+strFen) + "'" + (preMiao.length == 2 ? preMiao : "0"+preMiao)+"."+sufMiao+'"';
}
var jwd = "";
function initMapEvent(map){
map.on('mousemove',
function(e) {
var x = e.latlng.lat;
var y = e.latlng.lng;
jwd = ToDegrees(y) + "E " + ToDegrees(x)+"N";
document.getElementById('jwd').style.removeProperty("display")
document.getElementById('jwd').innerHTML = jwd+" 图级:"+window.map.getZoom();
});
map.on('zoomend',
function(e) {
// console.log(e)
var target = e.target
// var latlng = target._animateToCenter
// var x = latlng.lat;
// var y = latlng.lng;
var zoom = target._zoom
// var jwd = ToDegrees(y) + "E " + ToDegrees(x)+"N";
document.getElementById('jwd').style.removeProperty("display")
document.getElementById('jwd').innerHTML = jwd+" 图级:"+zoom;
});
map.on('click', function(e) {
var event = e.originalEvent;
map.eachLayer(function(layer){
if( layer && layer.type === 'GWVTAnno' )
{
var feature = layer.getFeatureByXY(event.x,event.y);
if(feature){
layer.bindTooltip(feature.label).openTooltip(e.latlng);
}
}
});
});
// map.fire map.initControlLayer
/*map.on('pm:create', e => {
console.log(e.layer);
console.log(e.layer.getBounds());
console.log(e.layer.getLatLngs()[0].toString());
console.log(e.layer.getLatLngs()[0][0].toString());
}); */
}
var layerArr=[]
function clickImg(layer){
for ( var i = 0 ; i < layerArr.length ; i++ )
{
document.getElementById(layerArr[i]).removeAttribute("style");
}
layerArr.push(layer)
document.getElementById(layer).style.borderStyle="solid";
document.getElementById(layer).style.borderColor="yellow";
layerArr = [...new Set(layerArr)];
}
function setLayerControl(map){
var layerHtml = '',currentLayerHtml;
var baseMaps = {},overlayMaps={};
var firstMap = "sl"
for( var i in mapConfig.layers )
{
var layer = mapConfig.layers[i];
baseMaps["
"+layer.name+" "] = newLayerGroup(layer);
if(i === "0"){
firstMap = layer.mapType
}
}
/*for( var i in mapConfig.overlayMaps )
{
var layer = mapConfig.overlayMaps[i];
overlayMaps[layer.name] = newLayerGroup(layer);
}*/
var layerControl = L.control.layers(baseMaps, overlayMaps,{position:'bottomright'}).addTo(map);
layerControlContailner= layerControl.getContainer();
//layerControlContailner.style.height = '36px';
layerControlContailner.style['background-color'] = 'rgba(255,255,255,.8)';
layerControlContailner.style['transition-property'] = 'width,background-color';
layerControlContailner.style['transition-duration'] = '.4s';
layerControlContailner.children[0].width='50px';
layerControlContailner.children[0].height='62px';
layerControlContailner.children[1].children[0].style.display = '-webkit-inline-box';
setControlImg(map)
map.addLayer(layerControl._layers[0].layer);
var inRedioDOM = document.getElementsByName('leaflet-base-layers');
for ( var i = 0 ; i < inRedioDOM.length ; i++ )
{
inRedioDOM[i].style.display = 'none';
}
clickImg(firstMap)
}
function setControlImg(map){
map.on('baselayerchange',function(res){
// console.log(res.layer.setZIndex)
var src = $(res.name).find('img').attr('src')
// console.log($('.leaflet-control-layers-toggle'))
$('.leaflet-control-layers-toggle').css({
'background-image':'url('+src+')'
})
})
$('.leaflet-control-layers-toggle').css({
'background-image':'url('+mapConfig.layers[0].imgUrl+')'
})
}
function newLayerAddToMap(layerConfig,map){
map.addLayer(newLayerGroup(layerConfig));
}
function newLayerGroup(layerConfig){
var layerGroup = L.layerGroup();
// layerGroup.setZIndex(10)
layerGroup.addLayer(newLayerByType(layerConfig));
if(layerConfig.subLayer && layerConfig.subLayer.length > 0 ){
for( var s in layerConfig.subLayer )
{
layerGroup.addLayer(newLayerByType(layerConfig.subLayer[s]));
}
}
return layerGroup;
}
function newLayerByType(layerConfig){
var layer;
if(layerConfig.type === 'wmts')
{
layer = new L.tileLayer.wmts( layerConfig.url, layerConfig.option );
}else if(layerConfig.type === 'wms')
{
layer = new L.tileLayer.wms(layerConfig.url, layerConfig.option);
}else if(layerConfig.type ==='GXYZ'){
layer = new L.GXYZ(layerConfig.url,layerConfig.option);
}else if(layerConfig.type ==='GWVTAnno'){
layer = new L.GWVTAnno(layerConfig.option);
var dataSource = new Custom.URLDataSource();
dataSource.url = layerConfig.url;
layer.addDataSource(dataSource);
}else if(layerConfig.type ==='gd'){
layer = L.tileLayer(
layerConfig.url,
layerConfig.option
)
}
layer.type = layerConfig.type;
return layer ;
}
function doXHR(url,callback){
var httpXml = createXMLHttpRequest();
httpXml.open('get', url, true);
httpXml.send(null);
httpXml.onreadystatechange = function() {
if(httpXml.readyState == 4 && httpXml.status == 200) {
var data = JSON.parse(httpXml.responseText);
if(callback){
callback(data);
}
}
};
}
function createXMLHttpRequest() {
var xmlHttp;
try{
xmlHttp = new XMLHttpRequest();
} catch (e) {
// 适用于IE6
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
// 适用于IE5.5,以及IE更早版本
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){}
}
}
return xmlHttp;
}