﻿/*
 * wms-gs-1_0_1.js V1.2
 * 为GMap API v2实现调用WMS的方法
 */

var MAGIC_NUMBER = 6356752.3142;
var DEG2RAD = 0.0174532922519943;
var PI = 3.14159267;

var FORMAT_DEFAULT = "image/png";

var MERC_ZOOM_DEFAULT = 5;
function dd2MercMetersLng(p_lng) { 
	return MAGIC_NUMBER*(p_lng*DEG2RAD); 
}

function dd2MercMetersLat(p_lat) {
	if (p_lat >= 85) p_lat=85;
	if (p_lat <= -85) p_lat=-85;
	return MAGIC_NUMBER*Math.log(Math.tan(((p_lat*DEG2RAD)+(PI/2)) /2));
}

CustomGetTileUrlByXyl=function(a,b) {
	var lURL=this.myBaseURL;
	lURL+="x="+a.x;
	lURL+="&y="+a.y;
	lURL+="&z="+b;
	return lURL;
}

CustomGetTileUrl = function(a, b, c) {
    if (this.myMercZoomLevel == undefined) {
        this.myMercZoomLevel = MERC_ZOOM_DEFAULT;
    }

    if (this.myLayers == undefined) {	//WMS图层
        this.myLayers = "";
    }

    if (this.myStyles == undefined) {	//图层样式参数，此参数服务端没有使用
        this.myStyles = "";
    }

    if (this.myFormat == undefined) {	//输出图像格式，只接受“image/gif”和“image/png”两种值
        this.myFormat = FORMAT_DEFAULT;
    }

    if (this.myBgColor == undefined) {	//图像背景色
        this.myBgColor = "0xFFFFFF";
    }

    if (this.myTransparent == undefined) {	//图像是否透明
        this.myTransparent = "TRUE";
    }

    //旧代码中成员变量myMapname命名不规范，新代码已重命名为myMapName
    //以下三行是为了兼容旧代码
    if (this.myMapname != undefined) {
        this.myMapName = this.myMapname;
    }
    if (this.myMapName == undefined) {	//地图客户端实例名称
        this.myMapName = "map";
    }

    var lULP = new GPoint(a.x * 256, (a.y + 1) * 256);
    var lLRP = new GPoint((a.x + 1) * 256, a.y * 256);
    var lUL = G_NORMAL_MAP.getProjection().fromPixelToLatLng(lULP, b, c);
    var lLR = G_NORMAL_MAP.getProjection().fromPixelToLatLng(lLRP, b, c);

    eval("var lwz = " + this.myMapName + ".getZoom()");
    if (this.myMercZoomLevel != 0 && lwz < this.myMercZoomLevel) {
        var lBbox = dd2MercMetersLng(lUL.x) + "," + dd2MercMetersLat(lUL.y) + "," + dd2MercMetersLng(lLR.x) + "," + dd2MercMetersLat(lLR.y);
        var lSRS = "EPSG:41001";
    } else {
        var lBbox = lUL.x + "," + lUL.y + "," + lLR.x + "," + lLR.y;
        var lSRS = "EPSG:4326";
    }
    var lURL = this.myBaseURL;
    lURL += "&REQUEST=GetMap";
    lURL += "&SERVICE=WMS";
    lURL += "&VERSION=1.1.1";
    lURL += "&LAYERS=" + this.myLayers;
    lURL += "&STYLES=" + this.myStyles;
    lURL += "&FORMAT=" + this.myFormat;
    lURL += "&BGCOLOR=" + this.myBgColor;
    lURL += "&TRANSPARENT=" + this.myTransparent;
    lURL += "&SRS=" + lSRS;
    lURL += "&BBOX=" + lBbox;
    lURL += "&WIDTH=256";
    lURL += "&HEIGHT=256";
    lURL += "&REASPECT=FALSE";
    lURL += "&LEVEL=" + lwz;
	lURL += "&AA=TRUE";
    return lURL;
}

function customOpacity() {
	return this.myOpacity;
}

