天津*应急系统之GIS一张图(arcgis api for flex)讲解(三)显示地图坐标系模块

config.xml文件的配置如下:

	<widget left="3" bottom="3"
config="widgets/Coordinate/CoordinateWidget.xml" url="widgets/Coordinate/CoordinateWidget.swf" />

源代码目录如下:

天津*应急系统之GIS一张图(arcgis api for flex)讲解(三)显示地图坐标系模块

地图坐标系模块的源代码原理解析,详细的代码在下载的开源flexviewer自带的:

(1)CoordinateWidget.xml

 <?xml version="1.0" ?>
<configuration label="Coordinates (default)">
<!-- geo, dms, mercator 主要是坐标输出单位,默认的是经纬度 -->
<outputunit>geo</outputunit>
</configuration>

(2)CoordinateWidget.mxml

 <?xml version="1.0" encoding="utf-8"?>
<viewer:BaseWidget xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:viewer="com.esri.viewer.*"
layout="horizontal"
widgetConfigLoaded="basewidget_widgetConfigLoadedHandler(event)">
<fx:Script>
<![CDATA[
import com.esri.ags.events.MapEvent;
import com.esri.ags.geometry.MapPoint;
import com.esri.ags.utils.WebMercatorUtil; import mx.formatters.NumberBaseRoundType;
import mx.utils.StringUtil; private var m_template:String;
private var m_func:Function = substitute; protected function basewidget_widgetConfigLoadedHandler(event:Event):void
{
if (configXML)
{
//下面是读取CoordinateWidget.xml配置文件的资源,要是配置了的话
const decimalSeparator:String = configXML.numberformatter.@decimalseparator;
numberFormatter.decimalSeparatorTo = decimalSeparator ? decimalSeparator : ".";
const thousandsSeparator:String = configXML.numberformatter.@thousandsseparator;
numberFormatter.thousandsSeparatorTo = thousandsSeparator ? thousandsSeparator : ",";
numberFormatter.useThousandsSeparator = configXML.numberformatter.@usethousandsseparator == "true";
numberFormatter.precision = parseFloat(configXML.numberformatter.@precision || "-1");
const rounding:String = configXML.numberformatter.@rounding;
numberFormatter.rounding = rounding ? rounding : NumberBaseRoundType.NONE;
//获取设置坐标显示的字体和颜色样式等
const color:String = configXML.labelstyle.@color[0] || configXML.label.@color[0];
coords.setStyle("color", toNumber(color ? color : "0x000000"));
const fontFamily:String = configXML.labelstyle.@fontfamily[0] || configXML.label.@fontfamily[0];
coords.setStyle("fontFamily", fontFamily ? fontFamily : "Verdana");
const fontSize:String = configXML.labelstyle.@fontsize[0] || configXML.label.@fontsize[0];
coords.setStyle("fontSize", parseInt(fontSize ? fontSize : "9"));
const fontWeight:String = configXML.labelstyle.@fontweight[0] || configXML.label.@fontweight[0];
coords.setStyle("fontWeight", fontWeight ? fontWeight : "bold"); // If no template specified, show them with a space in between (except for special case below)
m_template = configXML.labels.template[0] || configXML.label.@template[0] || "{0} {1}"; if (map.loaded)
{
map_loadHandler(null);
}
else
{
map.addEventListener(MapEvent.LOAD, map_loadHandler);//加载地图
}
} function map_loadHandler(event:MapEvent):void
{
map.removeEventListener(MapEvent.LOAD, map_loadHandler);
const wkid:int = map.spatialReference.wkid; //获取地图的空间坐标参考系
m_func = substitute;
const outputUnit:String = configXML.outputunit;//获取地图的坐标显示单位,从配置文件获取
if (outputUnit === "mercator")//判断地图的坐标体系,墨卡托情况下执行
{
if (wkid === 4326 || wkid === 4269 || wkid === 4267)
{
m_func = geographicToMercator;//调用地理坐标系转换墨卡托坐标系
}
}
else if (outputUnit === "geo")//地理坐标系情况下执行
{
if (wkid === 102100 || wkid === 102113 || wkid === 3857)
{
m_func = mercatorToGeographic;//调用墨卡托坐标系转换地理坐标系
// special default for geographic outputs
m_template = configXML.labels.template[0] || configXML.label.@template[0] || getDefaultString("latitudeLabel") + ":{1} " + getDefaultString("longitudeLabel") + ":{0}";//设置坐标显示的文字,比如经度,纬度
numberFormatter.precision = parseFloat(configXML.numberformatter.@precision || "6");//设置坐标显示的位数
}
else if (wkid === 4326 || wkid === 4269 || wkid === 4267)
{
// special default for geographic outputs
m_template = configXML.labels.template[0] || configXML.label.@template[0] || getDefaultString("latitudeLabel") + ":{1} " + getDefaultString("longitudeLabel") + ":{0}";
numberFormatter.precision = parseFloat(configXML.numberformatter.@precision || "6");
}
}
else if (outputUnit === "dms")//经纬度显示单位为度分秒形式情况下执行
{
if (wkid === 102100 || wkid === 102113 || wkid === 3857)
{
m_func = mercatorToDMS;
}
else if (wkid === 4326 || wkid === 4269 || wkid === 4267)
{
m_func = geographicToDMS;
}
}
map.addEventListener(MouseEvent.MOUSE_MOVE, map_mouseMoveHandler);//监听地图鼠标移动事件,用来获取地图经纬度的
}
} private function toNumber(value:String):int//转换单位计算
{
if (value.substr(0, 2) == "0x")
{
return parseInt(value, 16);
}
return parseInt(value, 10);
} private function mercatorToGeographic(web:MapPoint):String//墨卡托转换地理坐标系的函数
{
const geo:MapPoint = WebMercatorUtil.webMercatorToGeographic(web) as MapPoint;//arcgis api封装好的转换函数
return StringUtil.substitute(m_template,
numberFormatter.format(geo.x),
numberFormatter.format(geo.y));
} private function mercatorToDMS(web:MapPoint):String//墨卡托转换经纬度度分秒形式的函数
{
const geo:MapPoint = WebMercatorUtil.webMercatorToGeographic(web) as MapPoint;
return StringUtil.substitute(m_template, DegToDMS.format(geo.x, DegToDMS.LON), DegToDMS.format(geo.y, DegToDMS.LAT));
} private function geographicToMercator(geo:MapPoint):String//地理坐标系转换墨卡托的函数
{
const web:MapPoint = WebMercatorUtil.geographicToWebMercator(geo) as MapPoint;
return StringUtil.substitute(m_template,
numberFormatter.format(web.x),
numberFormatter.format(web.y));
} private function substitute(mapPoint:MapPoint):String
{
return StringUtil.substitute(m_template,
numberFormatter.format(mapPoint.x),
numberFormatter.format(mapPoint.y));
} private function geographicToDMS(mapPoint:MapPoint):String
{
const x:String = DegToDMS.format(mapPoint.x, DegToDMS.LON);
const y:String = DegToDMS.format(mapPoint.y, DegToDMS.LAT);
return StringUtil.substitute(m_template, x, y);
} private function map_mouseMoveHandler(event:MouseEvent):void
{
const mapPoint:MapPoint = map.toMapFromStage(event.stageX, event.stageY);//获取鼠标移动的地图经纬度
coords.text = m_func(mapPoint);
}
]]>
</fx:Script> <fx:Declarations>
<mx:NumberFormatter id="numberFormatter"/>
</fx:Declarations>
<viewer:filters>
<mx:GlowFilter alpha="1"
blurX="3"
blurY="3"
color="0xFFFFFF"
strength="7"/>
</viewer:filters>
<s:Label id="coords" color="0x000000"/>//显示经纬度的位置,显示label
</viewer:BaseWidget>

(3)DegToDMS.as

 package widgets.Coordinate
{ /**
* Utility class to pretty print decimal degree numbers.
* @private
*/
public final class DegToDMS
{
// Constants to define the format.
public static const LAT:String = "lat"; public static const LON:String = "lon"; /**
* Utility function to format a decimal degree number into a pretty string with degrees, minutes and seconds.
* @param decDeg the decimal degree number.
* @param decDir "lat" for a latitude number, "lon" for a longitude value.
* @return A pretty print string with degrees, minutes and seconds.
*/
public static function format(decDeg:Number, decDir:String):String//这个函数主要是用来把经纬度转换度分秒的形式来展示经纬度,比如113度23分23秒等等
{
var d:Number = Math.abs(decDeg);
var deg:Number = Math.floor(d);
d = d - deg;
var min:Number = Math.floor(d * 60);
var av:Number = d - min / 60;
var sec:Number = Math.floor(av * 60 * 60);
if (sec == 60)
{
min++;
sec = 0;
}
if (min == 60)
{
deg++;
min = 0;
}
var smin:String = min < 10 ? "0" + min + "' " : min + "' ";
var ssec:String = sec < 10 ? "0" + sec + "\" " : sec + "\" ";
var sdir:String = (decDir == LAT) ? (decDeg < 0 ? "S" : "N") : (decDeg < 0 ? "W" : "E");
return deg + "\xB0 " + smin + ssec + sdir;
}
} }

备注:

GIS技术交流QQ群:432512093

上一篇:JAVA (集合和数据结构)


下一篇:新建linux组、用户命令