/**
* [ONEMAP.M.toolSquareGrid]
* @return {[object]}
*/
define(function (tplLayout) {
/**
* 模块数据 用于数据存储和外部调用
* @type {Object}
* 数据存放
*/
var modValue = {
LG: null,
}
//var LG = null; // to draw
var LH = null;
var delta = 100; // in meters
var bea = 0; // in degrees
var invxy = 0, // 1 if inverse x & y
revy = 0;
var xlabels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'aa', 'ab', 'ac', 'ad', 'ae', 'af', 'ag', 'ah', 'ai', 'aj', 'ak', 'al', 'am', 'an'];
var ylabels = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40'];
var Nx0 = xlabels.length,
Ny0 = ylabels.length;
var egli = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"];
var delta_list = [1000] // larger than 5 meters
/**
* 模块界面样式 例如:宽,高
* @type {Object}
*/
var styles = {}
/**
* 模块状态,用于存储模块的状态 例如:收起,关闭
* @type {Object}
*/
var status = {
initialized: false //是否初始化
};
/**
* 初始化并订阅事件
* @return {[type]} [description]
*/
function init() {
if (!status.initialized) {
modValue.daihao = map23DControl.curZoingNum || 3;
buildSquareGrid();
//提示
addtip();
subscribe();
status.initialized = true;
} else {
//clearSquareGrid();
//status.initialized = false;
}
}
function addtip() {
$('body').append('
方里网在13-16级上显示当前级别为 ' + map2DViewer.map.getZoom() + '
')
}
function removetip() {
if ($('.toolSquareGridTip').length > 0) {
$('.toolSquareGridTip').remove();
}
}
function buildSquareGrid() {
SGrid()
map2DViewer.map.on('zoomend move', SGrid)
var handler = new Cesium.ScreenSpaceEventHandler(map3DViewer.map.scene.canvas);
//鼠标移动
handler.setInputAction(function (e) {
SGrid()
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
}
function SGrid() {
var zoom = map2DViewer.map.getZoom();
//刷新显示级别
$('.toolSquareGridTip span').html(zoom + ' ');
if (zoom > 12 && zoom <= 16) {
var bounds = map2DViewer.map.getBounds();
var latlng = [];
var IntMeter = getIntMeter(bounds._northEast.lat, bounds._northEast.lng);
bounds._northEast.lat = IntMeter.lat;
bounds._northEast.lng = IntMeter.lon;
window.gauss = GaussUtil.fromLatLon(bounds._northEast.lat, bounds._northEast.lng, modValue.daihao);
gauss.x = Math.ceil(gauss.x / 1000) * 1000;
gauss.y = parseInt(gauss.y / 1000) * 1000;
gauss.y = parseInt(JSON.stringify(gauss.zoneNum) + JSON.stringify(gauss.y))
var IntMeter = getIntMeter(bounds._southWest.lat, bounds._southWest.lng);
bounds._southWest.lat = IntMeter.lat;
bounds._southWest.lng = IntMeter.lon;
latlng.push({
lat: bounds._northEast.lat,
lng: bounds._northEast.lng
});
latlng.push({
lat: bounds._southWest.lat,
lng: bounds._northEast.lng
});
latlng.push({
lat: bounds._southWest.lat,
lng: bounds._southWest.lng
});
latlng.push({
lat: bounds._northEast.lat,
lng: bounds._southWest.lng
});
latlng.push({
lat: bounds._northEast.lat,
lng: bounds._northEast.lng
});
set_new_editable_layer(latlng)
} else {
if (modValue.LG != null) {
map2DViewer.map.removeLayer(modValue.LG)
}
}
}
function getIntMeter(lat, lng) {
var gauss = GaussUtil.fromLatLon(lat, lng, modValue.daihao);
gauss.x = Math.ceil(gauss.x / 1000) * 1000;
gauss.y = Math.ceil(gauss.y / 1000) * 1000;
gauss.y = parseInt(JSON.stringify(gauss.zoneNum) + JSON.stringify(gauss.y))
var latlng = GaussUtil.toLatLon(gauss.x, gauss.y, modValue.daihao);
return latlng
}
var mycrs;
var pre_my_delta;
function set_new_editable_layer(layer) {
var LL = layer
var LL_P_1 = 0,
LL_P_3 = 0,
j = -1,
k = 1
var b = 0;
var s = 1
if (((b > 90) & (b < 270)) | (b < -90) & (b > -270)) s = -1
while (!((s * LL_P_1.x > 0) & (s * LL_P_3.y < 0))) {
if (j == 4) {
j = 0;
k = -1
}
++j
mycrs = new L.Proj.CRS("EPSG:999999", "+proj=tmerc +lat_0=" + LL[mod(j, 4)].lat + " +lon_0=" + LL[mod(j, 4)].lng + " +k=1 +x_0=0 +y_0=0 +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs");
LL_P_1 = mycrs.projection.project(LL[mod(j + k * 1, 4)])
LL_P_3 = mycrs.projection.project(LL[mod(j + k * 3, 4)])
}
var dx = Math.ceil(Math.sqrt(LL_P_1.x * LL_P_1.x + LL_P_1.y * LL_P_1.y))
var dy = Math.ceil(Math.sqrt(LL_P_3.x * LL_P_3.x + LL_P_3.y * LL_P_3.y))
if (map2DViewer.map.getZoom() <= 14) {
my_delta = [];
for (var i = 0; i < delta_list.length; ++i) {
if (
(delta_list[i] >= Math.max(dx / xlabels.length, dy / ylabels.length)) & (delta_list[i] < Math.min(dx / 4, dy / 4))
) {
my_delta.push({
delta: delta_list[i],
Nx: Math.ceil(dx / delta_list[i]),
Ny: Math.ceil(dy / delta_list[i])
})
}
}
pre_my_delta = my_delta;
} else {
my_delta = pre_my_delta;
}
var delta = null,
Nx, Ny
var ret = egli[my_delta.length - 1];
if (ret == null) {
return false
}
ret = ret.toUpperCase()
var J = ret.charCodeAt() - 65
if ($.isNumeric(ret)) {
delta = ret
Nx = Math.ceil(dx / ret)
Ny = Math.ceil(dy / ret)
} else if ((J >= 0) & (J < my_delta.length)) {
delta = my_delta[J].delta
Nx = my_delta[J].Nx
Ny = my_delta[J].Ny
}
if (delta != null) {
bea = 0;
show_grid({
x: 0,
y: 0
}, xlabels, ylabels, delta, Nx, Ny, bea, invxy, revy)
}
}
function mod(n, m) {
return ((n % m) + m) % m;
}
function getLatlng(xy_center) {
//console.log(xy_center)
var latlng = GaussUtil.toLatLon(gauss.x + xy_center.y, gauss.y - xy_center.x, modValue.daihao);
return new L.latLng([latlng.lat, latlng.lon])
}
function show_grid(latlng_31370, xlabels, ylabels, delta, Nx, Ny, b, invxy, revy) {
var G = [],
H = [],
cb = Math.cos(b * Math.PI / 180),
sb = Math.sin(b * Math.PI / 180),
x0 = parseFloat(latlng_31370.x), // ! parseFloat ! otherwise string concat !!
y0 = parseFloat(latlng_31370.y)
for (var i = 0; i < Nx; ++i) {
for (var j = 0; j < Ny; ++j) {
//var xy_center = {
// x: x0 + cb * (i + 0) * delta + sb * (j + 0) * delta,
// y: y0 + sb * (i + 0) * delta - cb * (j + 0) * delta
//}
//var LL = mycrs.projection.unproject(xy_center) // center of the square
//console.log(LL)
if (map2DViewer.map.getZoom() == 13) {
var LL = mycrs.projection.unproject({
x: x0 + cb * (i + 0) * delta + sb * (j + 0) * delta,
y: y0 + sb * (i + 0) * delta - cb * (j + 0) * delta
})
var LL_TL = mycrs.projection.unproject({
x: x0 + cb * (i + 0) * delta + sb * (j + 0) * delta,
y: y0 + sb * (i + 0) * delta - cb * (j + 0) * delta
})
var LL_TR = mycrs.projection.unproject({
x: x0 + cb * (i + 1) * delta + sb * (j + 0) * delta,
y: y0 + sb * (i + 1) * delta - cb * (j + 0) * delta
})
var LL_BL = mycrs.projection.unproject({
x: x0 + cb * (i + 0) * delta + sb * (j + 1) * delta,
y: y0 + sb * (i + 0) * delta - cb * (j + 1) * delta
})
var LL_BR = mycrs.projection.unproject({
x: x0 + cb * (i + 1) * delta + sb * (j + 1) * delta,
y: y0 + sb * (i + 1) * delta - cb * (j + 1) * delta
})
} else {
var LL = getLatlng({
x: x0 + cb * (i + 0) * delta + sb * (j + 0) * delta,
y: y0 + sb * (i + 0) * delta - cb * (j + 0) * delta
})
var LL_TL = getLatlng({
x: x0 + cb * (i + 0) * delta + sb * (j + 0) * delta,
y: y0 + sb * (i + 0) * delta - cb * (j + 0) * delta
})
var LL_TR = getLatlng({
x: x0 + cb * (i + 1) * delta + sb * (j + 0) * delta,
y: y0 + sb * (i + 1) * delta - cb * (j + 0) * delta
})
var LL_BL = getLatlng({
x: x0 + cb * (i + 0) * delta + sb * (j + 1) * delta,
y: y0 + sb * (i + 0) * delta - cb * (j + 1) * delta
})
var LL_BR = getLatlng({
x: x0 + cb * (i + 1) * delta + sb * (j + 1) * delta,
y: y0 + sb * (i + 1) * delta - cb * (j + 1) * delta
})
}
var lat_lngs = [LL_TL, LL_TR, LL_BR, LL_BL, LL_TL]
var myname = GaussUtil.fromLatLon(LL.lat, LL.lng, modValue.daihao);
//myname = parseInt(myname.y/1000)+'000,'+parseInt(myname.x/1000)+'000'
mynamex = myname.zoneNum + "," + myname.y.toFixed(0);
mynamey = myname.x.toFixed(0);
/*var newj = j
if (revy == 1) newj = Ny - 1 - j
if (invxy == 1) {
myname = xlabels[newj].toUpperCase() + ylabels[i]
} else {
myname = xlabels[i].toUpperCase() + ylabels[newj];
}*/
/*if (((i % 1 == 0) & (j % 1 == 0))) {
if(map2DViewer.map.getZoom() > 13){
var myIcon = L.divIcon({
className: 'emptyicon',
html: myname
});
var marker = L.marker(LL, {
icon: myIcon
})
marker.properties = {};
G.push(marker)
H.push(L.marker(LL, {
icon: myIcon,
name: myname
}));
}
}*/
if (map2DViewer.map.getZoom() > 13) {
if ((i == 1) && (j != 1)) {
var myIcon = L.divIcon({
className: 'emptyiconx',
html: mynamey
});
var marker = L.marker(LL, {
icon: myIcon
})
marker.properties = {};
G.push(marker)
H.push(L.marker(LL, {
icon: myIcon,
name: mynamey
}));
}
if (j == 1) {
var myIcon = L.divIcon({
className: 'emptyicon',
html: mynamex
});
var marker = L.marker(LL, {
icon: myIcon
})
marker.properties = {};
G.push(marker)
H.push(L.marker(LL, {
icon: myIcon,
name: mynamex
}));
}
}
G.push(new L.Polyline(lat_lngs, {
color: '#00ffcc',
fillOpacity: 0,
weight: 1
}));
H.push(new L.Polyline(lat_lngs, {
color: '#00ffcc',
fillOpacity: 0,
weight: 1
}));
}
}
var LL_TL = mycrs.projection.unproject({
x: x0,
y: y0
})
var LL_TR = mycrs.projection.unproject({
x: x0 + cb * Nx * delta,
y: y0 + sb * Nx * delta
})
var LL_BL = mycrs.projection.unproject({
x: x0 + sb * Ny * delta,
y: y0 - cb * Ny * delta
})
var LL_BR = mycrs.projection.unproject({
x: x0 + cb * Nx * delta + sb * Ny * delta,
y: y0 + sb * Nx * delta - cb * Ny * delta
})
var lat_lngs = [LL_TL, LL_TR, LL_BR, LL_BL, LL_TL]
if (modValue.LG != null) {
map2DViewer.map.removeLayer(modValue.LG)
}
modValue.LG = L.featureGroup(G).addTo(map2DViewer.map)
LH = L.featureGroup(H)
}
function clearSquareGrid() {
if (modValue.LG != null) {
map2DViewer.map.removeLayer(modValue.LG)
}
map2DViewer.map.off('zoomend move', SGrid)
removetip();
status.initialized = false;
// if ($("#mapBaseLayerControl .choosemap .tufu-control input").is(":checked")) {
// $("#mapBaseLayerControl .choosemap .tufu-control input").click();
// }
}
/**
* 设置界面
*/
function setLayout() {
}
/**
* 界面事件绑定
* @return {[type]} [description]
*/
function bindEvent() {
}
/**
* 界面重置
* @return {[type]} [description]
*/
function layoutResize() {
}
function updaihao(options) {
modValue.daihao = options;
SGrid();
}
/**
* 注册订阅
* @type {Function}
* 推送:ONEMAP.C.publisher.publish(options,'moduleName::type');
* 订阅:ONEMAP.C.publisher.subscribe(layoutResize,'sideBarLayoutChange');
*/
function subscribe() {
ONEMAP.C.publisher.subscribe(clearSquareGrid, 'cleanMap');
ONEMAP.C.publisher.subscribe(updaihao, 'daihao');
}
/**
* 取消订阅
* @type {Function}
* 取消订阅:ONEMAP.C.publisher.unSubscribe(layoutResize,'sideBarLayoutChange');
*/
function unSubscribe() {
ONEMAP.C.publisher.unSubscribe(updaihao, 'daihao');
}
/**
* 模块移除
* @return {[type]} [description]
*/
function remove() {
//取消订阅
unSubscribe();
}
return ONEMAP.M.toolSquareGrid = {
init: init,
clearSquareGrid: clearSquareGrid
}
});