/**
* @fileoverview 工具 测量地图面积 模块
* @author Song.Huang
* @version 1.0.0
*/
define(['vendorDir/map23dlib/leaflet.mapshot',
'css!vendorDir/map23dlib/leaflet.mapshot'],
function(){
/**
* 状态值
* @type {Boolean}
* @default false
* @private
*/
var status ={
initialized:false//是否初始化
}
/**
* 模块数据 用于数据存储和外部调用
* @type {Object}
* 数据存放
*/
var modValue ={
loopAjaxTimeOut:{},
time_stamp:'',
postData:null,
mapShotControl:null,
BLC: [200000000, 100000000, 50000000, 25000000, 12000000, 6000000, 1600000, 1550000, 800000, 400000, 200000, 100000, 50000, 25000, 12000, 2300, 3000, 1500, 800]
}
var status = {
countingLock:false
}
/**
* 初始化
* 监听事件
* @type {Function}
*/
function init(){
if(!status.initialized){
status.initialized = true;
setLayout();
bindEvent();
subscribe();
modValue.mapShotControl = L.control.mapShot().addTo(map2DViewer.map);
//map2DViewer.map.on('mapShot:postData',modValue.mapShotPostData,this);
}
ONEMAP.C.publisher.publish({
modName:'toolMapShot'
},'tools:active');
}
function setLayout(){
$('body').append($('
'));
$(window).resize(function() {
layoutResize();
});
};
function bindEvent(){
};
function layoutResize(){
};
function mapfishPostData(postData){
var newPostData = encodeURIComponent(changeMapShotData(postData));
// console.log(modValue.mapShotDrawData)
//var appId = 'print_osm_new_york_EPSG_900913';
var appId = 'print_osm_EPSG3857';
var format = postData.ext;
//console.log(changeMapShotData(postData));
var startTime = new Date().getTime();
console.log(newPostData);
ONEMAP.C.publisher.publish({ type: 'success', message: '开始下载' }, 'noteBar::add');
//return false;
$.ajax({
type: 'POST',
url: onemapUrlConfig.mapFishServerUrl+'/print/' + appId + '/report.' + format,
data: newPostData,
success: function(data) {
downloadWhenReady(startTime, data);
},
error: function(data) {
ONEMAP.C.publisher.publish({ type: 'error', message: '下载出错' }, 'noteBar::add');
},
dataType: 'json'
});
};
function downloadWhenReady(startTime, data) {
if ((new Date().getTime() - startTime) > 30000) {
ONEMAP.C.publisher.publish({ type: 'success', message: '准备下载' }, 'noteBar::add');
} else {
updateWaitingMsg(startTime, data);
setTimeout(function() {
$.getJSON(onemapUrlConfig.mapFishServerUrl + data.statusURL, function(statusData) {
if (!statusData.done) {
downloadWhenReady(startTime, data);
} else {
window.location = onemapUrlConfig.mapFishServerUrl + statusData.downloadURL;
//ONEMAP.C.publisher.publish({ type: 'success', message: 'Downloading: ' + data.ref }, 'noteBar::add');
}
}, function error(data) { ONEMAP.C.publisher.publish({ type: 'error', message: '下载出错' }, 'noteBar::add') });
}, 1000);
}
}
function changeMapShotData(postData) {
//计算截图中心点
var scale = modValue.BLC[postData.zoom - 1];
var mapcenter = lonlatToMercator(postData.center);
var geojsonFeatures = [];
var geojsonStyles = {};
// modValue.mapShotDrawData = {}
// modValue.mapShotDrawData = map2DViewer.map._layers;
//获取截图元素数据
// $.each(modValue.mapShotDrawData,function(i,t){
$.each(map2DViewer.map._layers,function(i,t){
var guid = map23DControl.buildGuid('mapfish_style');
// if(t.drawType == 'marker'){
if(t.shape == 'Marker'){
// var curmarkerCoor = stringToParse([t.drawInfo.center[1],t.drawInfo.center[0]]);
var curmarkerCoor = stringToParse([t._latlng.lng,t._latlng.lat]);
curmarkerCoor = lonlatToMercator(curmarkerCoor)
var fItem = {
"geometry": {
"coordinates": curmarkerCoor,
"type": "Point"
},
"properties": {
"_gx_style": guid
},
"type": "Feature"
};
var iconUrl = onemapUrlConfig.siteUrl+'/scripts/vendor/leaflet/draw_new/images/marker-icon.png';
// if(t.options.icon){
// iconUrl = t.options.icon.options.iconUrl;
// }
geojsonStyles[guid] = {
"fillColor": "#ff0000",
"fillOpacity": 1,
"pointRadius": 5,
"externalGraphic":iconUrl,
"graphicOpacity":1,
"graphicFormat":"image/png"
};
geojsonFeatures.push(fItem);
// }else if(t.drawType == 'polyline'){
}else if(t.shape == 'polyline'){
var polylinescoor = [];
for (var i = 0; i < t.drawInfo.LatLngs.length; i++) {
curpolylinescoor = stringToParse([t.drawInfo.LatLngs[i].lng,t.drawInfo.LatLngs[i].lat]);
curpolylinescoor = lonlatToMercator(curpolylinescoor);
polylinescoor.push(curpolylinescoor);
}
var guid = guid;
var fItem = {
"geometry": {
"coordinates": polylinescoor,
"type": "LineString"
},
"properties": {
"_gx_style": guid
},
"type": "Feature"
};
geojsonStyles[guid] = {
"strokeColor": t.drawInfo.shapeOptions.color,
"strokeDashstyle": "solid",
"strokeLinecap": "round",
"strokeOpacity": t.drawInfo.shapeOptions.opacity,
"strokeWidth": t.drawInfo.shapeOptions.weight
};
geojsonFeatures.push(fItem);
// }else if(t.drawType == 'polygon'){
}else if(t.shape == 'polygon'){
var polygonscoor = [];
polygonscoor[0] = [];
for (var i = 0; i < t.drawInfo.LatLngs[0].length+1; i++) {
if(i 5) {
time = (elapsed / 10) + " sec";
}
ONEMAP.C.publisher.publish({ type: 'success', message: '等候时间' + time }, 'noteBar::add');
}
//经纬度转魔卡托
function lonlatToMercator(lonlat) {
var mercator = [];
var x = lonlat[0] * 20037508.34 / 180;
var y = Math.log(Math.tan((90 + lonlat[1]) * Math.PI / 360)) / (Math.PI / 180);
y = y * 20037508.34 / 180;
mercator.push(x);
mercator.push(y);
return mercator;
}
function stringToParse(lnglat) {
var x = JSON.parse(lnglat[0]);
var y = JSON.parse(lnglat[1]);
var parsecoor = [x, y];
return parsecoor;
}
function mapShotPostData(postData){
modValue.postData = postData;
var LTpx = map2DViewer.map.project(
new L.LatLng(JSON.parse(postData.bbox)[1],JSON.parse(postData.bbox)[0]),
postData.zoom
);
var RBpx = map2DViewer.map.project(
new L.LatLng(JSON.parse(postData.bbox)[3],JSON.parse(postData.bbox)[2]),
postData.zoom
);
var countDownTF = parseInt(((RBpx.x-LTpx.x)*(RBpx.y-LTpx.y))/(5120*5120));
if(countDownTF > onemapUrlConfig.mapShotMaxPicCount){
var xiangsu = parseInt(5120*Math.sqrt(onemapUrlConfig.mapShotMaxPicCount));
/*$('#mapShotDownload p').html('当前范围产生'+countDownTF+'个图幅数,'+
'
已经超过单次下载'+onemapUrlConfig.mapShotMaxPicCount+'个最大图幅数(5120×5120像素/图幅),请分开截图!
'+
'修改区域')*/
$('#mapShotDownload p').html('地图拼接下载像素超过了'+xiangsu+'×'+xiangsu+'像素,请缩小截图下载区域
'+
'修改区域')
$('#mapShotDownload').show();
return false;
}
// if((RBpx.x-LTpx.x)>onemapUrlConfig.mapShotMaxPicWidth){
// alert('截图宽度不能大于 '+onemapUrlConfig.mapShotMaxPicWidth+' 像素!');
// return false;
// }
// if((RBpx.y-LTpx.y)>onemapUrlConfig.maxPicHeight){
// alert('截图高度不能大于 '+onemapUrlConfig.maxPicHeight+' 像素!');
// return false;
// }
$('#mapShotDownload').show();
var nDate = new Date();
modValue.time_stamp = nDate.getTime();
var url = onemapUrlConfig.mapShotDownServerUrl+'/puzzles/?bbox='+postData.bbox+'&zoom='+postData.zoom+'&type='+postData.guid+'&ext='+postData.ext+'&time_stamp='+modValue.time_stamp+'&name='+encodeURI(postData.name);
$.ajax({
type:"get",
url:url,
dataType:'jsonp',
jsonp:'callback',
success:function(data){
//modValue._loopGetResult({ajaxKey:'1'});
}
});
setTimeout(function(){
loopGetResult({ajaxKey:modValue.time_stamp});
}, 1000);
};
function againMapShotPostData(){
mapShotPostData(modValue.postData);
}
function loopGetResult(options){
$.ajax({
url:onemapUrlConfig.mapShotDownServerUrl+'/processing/'+modValue.time_stamp,
type:"get",
dataType:'jsonp',
jsonp:'callback',
success:function(data){
if(data.processing == ''){
$('#mapShotDownload p').html('服务器处理失败,请重试!
再次提交');
}
if(data.processing.length<3){
if(modValue.loopAjaxTimeOut.hasOwnProperty(options.ajaxKey)){
clearTimeout(modValue.loopAjaxTimeOut[options.ajaxKey]);
}
$('#mapShotDownload p').html('服务器正在截图,截图进度 '+data.processing +' %.
取消截图');
modValue.loopAjaxTimeOut[options.ajaxKey] = setTimeout(function(){
loopGetResult(options);
},1000);
}else if(data.processing == '100'){
if(modValue.loopAjaxTimeOut.hasOwnProperty(options.ajaxKey)){
clearTimeout(modValue.loopAjaxTimeOut[options.ajaxKey]);
}
$('#mapShotDownload p').html('服务器已完成截图,正在进行打包.
取消截图');
modValue.loopAjaxTimeOut[options.ajaxKey] = setTimeout(function(){
loopGetResult(options);
},1000);
}else {
delete modValue.loopAjaxTimeOut[options.ajaxKey];
//获取最终结果
$('#mapShotDownload p').html('服务器截图完成,点击下载!
继续截图退出截图')
//alert(data);
}
}
});
};
function continueMapShot(){
if(modValue.loopAjaxTimeOut.hasOwnProperty(modValue.time_stamp)){
clearTimeout(modValue.loopAjaxTimeOut[modValue.time_stamp]);
}
$('#mapShotDownload').hide().html('正在提交截图数据到服务器
');
}
function exitMapShot(){
$('#mapshotScreen').click();
modValue.mapShotControl.remove();
$('#mapShotDownload').hide()
}
/**
* 注册监听
*/
function subscribe(){
ONEMAP.C.publisher.subscribe(remove, 'tools:active');
ONEMAP.C.publisher.subscribe(mapShotPostData,'mapShot:postData');
ONEMAP.C.publisher.subscribe(mapfishPostData,'mapFish:postData');
};
/**
* 功能移除
*/
function remove(options){
if(options.modName != 'toolMapShot'){
$('.tools-mapshot').removeClass('cur')
modValue.mapShotControl.remove();
}else{
if($('.tools-mapshot').hasClass('cur')){
$('.tools-mapshot').removeClass('cur');
modValue.mapShotControl.remove();
}else{
$('.tools-mapshot').addClass('cur');
modValue.mapShotControl.init();
}
}
};
return ONEMAP.M.toolMapShot = {
init:init,
continueMapShot:continueMapShot,
exitMapShot:exitMapShot
}
});