arcgis for flex全国地图天气预报的具体实现过程解析
系统架构是B/S,开发语言是flex,开发工具是myeclise或者flashbuild,通过调用百度提供的在线天气预报web api接口的方式来实现。
采用地图是ArcGIS全国地图,开发接口为arcgis api for flex,但是程序可以拓展api 接口,可以采用其他GIS api,地图数据也可以更换其他地图数据,拓展性比较好。
先谈谈主界面的布局,只是简单的设计了大概的布局,如下:
首先是两行,第一行是标题;第二行再分为两列,第一列是用来天气预报查询以及显示查询的结果,第二列用来显示全国地图。
贴上界面布局的代码:
<s:layout>
<s:VerticalLayout gap="0"/>
</s:layout>
<s:BorderContainer width="100%" backgroundColor="#D7D7D7">
<s:layout>
<s:VerticalLayout paddingBottom="7"
paddingLeft="10"
paddingRight="10"
paddingTop="7"/>
</s:layout>
<s:RichText width="100%">
flex for arcgis----天气预报专题
</s:RichText>
</s:BorderContainer>
<s:HGroup width="100%" height="100%"
paddingLeft="1"
paddingTop="1">
<s:Group width="30%" height="100%" >
<s:Rect width="100%" height="100%">
<s:stroke>
<s:SolidColorStroke color="0xCECECE"/>
</s:stroke>
</s:Rect>
<s:VGroup width="100%" height="100%" horizontalAlign="center">
<s:HGroup horizontalAlign="center" width="100%" paddingTop="10">
<s:Label text="城市名称:" paddingTop="4"/>
<s:TextInput id="txt_userName" text="广州"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Button label="查询" id="btn_login"/>
<s:Button label="查询所有" click="all_clickHandler(event)"/>
</s:HGroup>
<s:Line width="100%">
<s:stroke>
<s:SolidColorStroke color="0xCECECE" weight="1"/>
</s:stroke>
</s:Line>
<s:VGroup width="100%" height="100%" horizontalAlign="center" id="hidepanel" visible="false">
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="星期:" paddingTop="4"/>
<s:Label text="" paddingTop="4" id="date1"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="温度:" paddingTop="4"/>
<s:Label text="" paddingTop="4" id="temperature1"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="白天:" paddingTop="4"/>
<s:Image id="dayimg1" width="25" height="15"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="黑夜:" paddingTop="4"/>
<s:Image id="nightimg1" width="25" height="15"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="气候:" paddingTop="4"/>
<s:Label text="" paddingTop="4" id="weather1"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="风向:" paddingTop="4"/>
<s:Label text="" paddingTop="4" id="wind1"/>
</s:HGroup>
<s:Line width="100%">
<s:stroke>
<s:SolidColorStroke color="0xCECECE" weight="1"/>
</s:stroke>
</s:Line>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="星期:" paddingTop="4"/>
<s:Label text="" paddingTop="4" id="date2"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="温度:" paddingTop="4"/>
<s:Label text="" paddingTop="4" id="temperature2"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="白天:" paddingTop="4"/>
<s:Image id="dayimg2" width="25" height="15"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="黑夜:" paddingTop="4"/>
<s:Image id="nightimg2" width="25" height="15"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="气候:" paddingTop="4"/>
<s:Label text="" paddingTop="4" id="weather2"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="风向:" paddingTop="4"/>
<s:Label text="" paddingTop="4" id="wind2"/>
</s:HGroup>
<s:Line width="100%">
<s:stroke>
<s:SolidColorStroke color="0xCECECE" weight="1"/>
</s:stroke>
</s:Line>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="星期:" paddingTop="4"/>
<s:Label text="" paddingTop="4" id="date3"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="温度:" paddingTop="4"/>
<s:Label text="" paddingTop="4" id="temperature3"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="白天:" paddingTop="4"/>
<s:Image id="dayimg3" width="25" height="15"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="黑夜:" paddingTop="4"/>
<s:Image id="nightimg3" width="25" height="15"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="气候:" paddingTop="4"/>
<s:Label text="" paddingTop="4" id="weather3"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="风向:" paddingTop="4"/>
<s:Label text="" paddingTop="4" id="wind3"/>
</s:HGroup>
<s:Line width="100%">
<s:stroke>
<s:SolidColorStroke color="0xCECECE" weight="1"/>
</s:stroke>
</s:Line>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="星期:" paddingTop="4"/>
<s:Label text="" paddingTop="4" id="date4"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="温度:" paddingTop="4"/>
<s:Label text="" paddingTop="4" id="temperature4"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="白天:" paddingTop="4"/>
<s:Image id="dayimg4" width="25" height="15"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="黑夜:" paddingTop="4"/>
<s:Image id="nightimg4" width="25" height="15"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="气候:" paddingTop="4"/>
<s:Label text="" paddingTop="4" id="weather4"/>
</s:HGroup>
<s:HGroup horizontalAlign="center" width="100%">
<s:Label text="风向:" paddingTop="4"/>
<s:Label text="" paddingTop="4" id="wind4"/>
</s:HGroup>
</s:VGroup>
<s:Label width="100%"
paddingBottom="0"
text="地理位置: {mapCoordX.toFixed(4)},{mapCoordY.toFixed(4)}"/>
</s:VGroup>
</s:Group>
<esri:Map id="myMap" wrapAround180="true">
<esri:extent>
<esri:Extent xmin="71.6184779133381" ymin="29.726061424023065" xmax="135.50700591988112" ymax="54.94834808582403">
<esri:SpatialReference wkid="4326"/>
</esri:Extent>
</esri:extent>
<esri:infoWindowContent>
<s:TextArea id="myTextArea"
width="250" height="75"/>
</esri:infoWindowContent>
<esri:ArcGISDynamicMapServiceLayer url="http://localhost:6080/arcgis/rest/services/weather/MapServer"/>
<esri:GraphicsLayer id="myGraphicsLayer"/>
</esri:Map>
</s:HGroup>
红色背景的url是地图发布的服务,调用显示全国地图。后台的核心实现:
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<s:HTTPService id="httpSer" method="POST">
</s:HTTPService>
<esri:PictureMarkerSymbol id="sfs"/>
<!-- <esri:PictureMarkerSymbol id="sfs1"/>-->
<!-- Layer with US States -->
<esri:QueryTask id="queryTask" url="http://localhost:6080/arcgis/rest/services/weather/MapServer/0"/>
<esri:Query id="query"
outSpatialReference="{myMap.spatialReference}"
returnGeometry="true"
text="{str}">
<esri:outFields>
<fx:String>Name</fx:String>
<fx:String>X</fx:String>
<fx:String>Y</fx:String>
</esri:outFields>
</esri:Query>
</fx:Declarations>
httpSer是声明HTTPService,因为天气预报是调用在线的http形式来实现的;queryTask是用来query查询的条件设置的,作用是为了每个城市的天气预报图标定位在地图上显示,url是指定用哪个图层来进行query查询。
程序运行时候显示的是全国城市的天气预报图标以及信息,所以在初始化时候定义并调用CityCodeDB.xml文件来获取所有的城市名称。
public const xmlUrl:String = "CityCodeDB.xml";
初始化函数:
private function init():void{
getallcityinfo();//获取所有的城市信息
}
/*
* 获取所有城市信息以及相关的天气预报信息
*/
private function getallcityinfo():void{
myGraphicsLayer.clear();
//HTTPService,为了监听初始化时候加载所有的城市天气预报,从配置文件xml获取所有的城市信息
var service:HTTPService = new HTTPService();
service.url = xmlUrl;
service.addEventListener(ResultEvent.RESULT, resultHandler);
service.send();
}
private function resultHandler(event:ResultEvent):void{
citys = event.result.CityCode.City; //获取到所有的配置文件城市名称集合
var service:HTTPService = new HTTPService();
service.addEventListener(ResultEvent.RESULT, cityresultHandler);//为了监听具体的每个城市天气预报信息
for(var i:int;i<citys.length;i++){
str=citys[i].Name;
var params:URLVariables = new URLVariables();
params.name =encodeURIComponent(citys[i].Name); //编码
service.url = "http://api.map.baidu.com/telematics/v3/weather?location="+ params.name+ "&ak=YknGmxIoPugT7YrNrG955YLS";//调用百度提供天气预报的api接口
service.send();
}
private function cityresultHandler(event:ResultEvent):void{
var weather:Weather = new Weather();//自定义的类,主要是定义跟天气预报信息接口对应的字段,比如温度等
if(event.result.CityWeatherResponse.results){
weather.cityname = event.result.CityWeatherResponse.results.currentCity;//当前的城市名称
weather.date =event.result.CityWeatherResponse.results.weather_data.date; //星期
weather.dayPictureUrl=event.result.CityWeatherResponse.results.weather_data.dayPictureUrl;//白天图标
weather.nightPictureUrl=event.result.CityWeatherResponse.results.weather_data.nightPictureUrl;//黑夜图标
weather.temperature=event.result.CityWeatherResponse.results.weather_data.temperature;//温度
weather.weather=event.result.CityWeatherResponse.results.weather_data.weather;//气候
weather.wind=event.result.CityWeatherResponse.results.weather_data.wind;//风向
query.text = weather.cityname;//为了动态的更新query查询text条件
queryTask.execute(query, new AsyncResponder(onResult, onFault));
function onResult(featureSet:FeatureSet, token:Object = null):void
{
if (featureSet.features.length == 0)
{
Alert.show("地图上没有搜索到相关数据定位");
}
else
{
//动态的改变显示在地图上的图标以及graphic
var sps:PictureMarkerSymbol = new PictureMarkerSymbol();
sps.source = weather.dayPictureUrl[0];
var graphic:Graphic = new Graphic(featureSet.features[0].geometry,sps,null);
graphic.toolTip = "当前城市:"+weather.cityname+"\n"+"星期:" + weather.date[0] + "\n" + "温度:" + weather.temperature[0] +"\n"+"气候:"+weather.weather[0]+"\n"+"风向:"+weather.wind[0];
myGraphicsLayer.add(graphic);
}
}
function onFault(info:Object, token:Object = null):void
{
Alert.show(info.toString());
}
}
else{
myGraphicsLayer.clear();
hidepanel.visible = false;
Alert.show("搜索不到城市相关的天气预报数据");
}
}
到这里全国的城市天气预报信息以及图标都实现了。
下面是查询某个城市的天气预报信息,首先在初始化函数init添加httpservice监听事件:
//监听查询按钮单击事件
btn_login.addEventListener(MouseEvent.CLICK,login);
//加载HTTPService的返回监听,为了监听单个城市查询使用
httpSer.addEventListener(ResultEvent.RESULT,httpSerResultHandler);
httpSer.addEventListener(FaultEvent.FAULT,httpSerFaultHandler);
然后在查询按钮事件添加:
//查询天气预报
private function weather(event:MouseEvent):void{
str = txtcity.text; //文本框输入城市名称值
var params:URLVariables = new URLVariables(); //编码
params.name =encodeURIComponent(txt_userName.text);
httpSer.url = "http://api.map.baidu.com/telematics/v3/weather?location="+ params.name+ "&ak=YknGmxIoPugT7YrNrG955YLShttpSer.send();
}
//返回成功事件
private function httpSerResultHandler(event:ResultEvent):void{
var weather:Weather = new Weather();
if(event.result.CityWeatherResponse.results){
hidepanel.visible = true;
weather.cityname = event.result.CityWeatherResponse.results.currentCity;
weather.date =event.result.CityWeatherResponse.results.weather_data.date;
weather.dayPictureUrl=event.result.CityWeatherResponse.results.weather_data.dayPictureUrl;
weather.nightPictureUrl=event.result.CityWeatherResponse.results.weather_data.nightPictureUrl;
weather.temperature=event.result.CityWeatherResponse.results.weather_data.temperature;
weather.weather=event.result.CityWeatherResponse.results.weather_data.weather;
weather.wind=event.result.CityWeatherResponse.results.weather_data.wind;
date1.text = weather.date[0]; //第一天天气预报详情
temperature1.text = weather.temperature[0];
weather1.text = weather.weather[0];
wind1.text = weather.wind[0];
dayimg1.source = weather.dayPictureUrl[0];
nightimg1.source = weather.nightPictureUrl[0];
date2.text = weather.date[1];//第二天天气预报详情
temperature2.text = weather.temperature[1];
weather2.text = weather.weather[1];
wind2.text = weather.wind[1];
dayimg2.source = weather.dayPictureUrl[1];
nightimg2.source = weather.nightPictureUrl[1];
date3.text = weather.date[2];//第三天天气预报详情
temperature3.text = weather.temperature[2];
weather3.text = weather.weather[2];
wind3.text = weather.wind[2];
dayimg3.source = weather.dayPictureUrl[2];
nightimg3.source = weather.nightPictureUrl[2];
date4.text = weather.date[3];//第四天天气预报详情
temperature4.text = weather.temperature[3];
weather4.text = weather.weather[3];
wind4.text = weather.wind[3];
dayimg4.source = weather.dayPictureUrl[3];
nightimg4.source = weather.nightPictureUrl[3];
// clear the graphics layer
myGraphicsLayer.clear();
queryTask.execute(query, new AsyncResponder(onResult, onFault));
function onResult(featureSet:FeatureSet, token:Object = null):void
{
if (featureSet.features.length == 0)
{
Alert.show("地图上没有搜索到相关数据定位");
}
else
{
sfs.source = weather.dayPictureUrl[0];
var graphic:Graphic = new Graphic(featureSet.features[0].geometry,sfs,null);
graphic.toolTip = "当前城市:"+weather.cityname+"\n"+"星期:" + weather.date[0] + "\n" + "温度:" + weather.temperature[0] +"\n"+"气候:"+weather.weather[0]+"\n"+"风向:"+weather.wind[0];
myGraphicsLayer.add(graphic);
myMap.centerAt(featureSet.features[0].geometry as MapPoint);
myMap.zoomIn();
}
}
function onFault(info:Object, token:Object = null):void
{
Alert.show(info.toString());
}
}
else{
myGraphicsLayer.clear();
hidepanel.visible = false;
Alert.show("搜索不到城市相关的天气预报数据");
}
}
//返回失败事件
private function httpSerFaultHandler(event:FaultEvent):void{
Alert.show(event.fault.message as String,"请求天气预报信息失败提示");
}
到这里,某个城市的天气预报信息查询也实现了。效果图:
备注:
源代码下载:http://download.csdn.net/detail/liguoweioo/8440727
GIS技术交流QQ群:432512093
GIS论坛:http://arcgis.c.ev123.com/vip_arcgis.html
arcgis for flex全国地图天气预报的具体实现过程解析的更多相关文章
- arcgis for flex或silverlight全国地图天气预报的实现
系统架构是B/S,目前有两个不同的版本,1.开发语言是C#.silverlight,开发平台是.NET:2.开发语言是java.flex,开发平台是myeclise. 采用地图是ArcGIS全国地图, ...
- Arcgis flex 切片地图麻点
在arcgis server中发布地图切片完成后,有时候在访问地图的时候会出现很多麻点, 其实是你切片的时候没有注意到一些选项.... 默认的切片是PNG8,说到这可能就明白了吧,png8的色彩范围: ...
- ArcGIS for Flex中引入google map作底图
上篇文章到在ArcGIS View中引入google map,这里讲ArcGIS for Flex中引入google map作底图. 同样道理,以google map作底图,需要编写继承自TiledM ...
- “ArcGIS数据应用和地图打印输出
中国科学院计算技术研究所教育中心 关于开展“ArcGIS数据应用和地图打印输出” 培训班的通知 各相关单位: 随着信息化.网络化.数字化向纵深发展,互联网与空间地理信息系统相互交织,数字地球.“智慧地 ...
- OpenLayers调用arcgis server发布的地图服务
有两种方式可以调用arcgis server发布的地图服务,一种是rest,一种是wms. 地图的投影为900913,arcgis server为10.0版本,地图服务的空间参考为3857. 与 ...
- Google Maps 学习笔记(二)地图天气预报服务 2014.06.04
地图天气预报服务:一,获取天气预报信息:二,解析天气预报信息:三,在地图上加载天气预报信息: Yahoo!提供的天气预报服务采用流行的RSS输出结果,接口地址如下: http://weather.ya ...
- MVC中使用Echart后台加载数据 实现饼图、折线图、全国地图数据,单击双击事件等
@{ Layout = null; } @if (false) { <script src="~/Js/jquery-easyui-1.5/jquery.min.js"> ...
- arcgis 加载高德地图 es6的方式
目前很多arcgis 加载高德地图是dojo的方式 外部引入文件,现在改成这种方式 /** * Created by Administrator on 2018/5/14 0014. */ impor ...
- echart 插件实现全国地图
最近的项目要用到一个能展现全国地图的功能,并且全国各个省份显示的颜色不同,点击省份后会返回省份名称.经过反复的查找最终确定了echart这个插件,最后的成果还不错,在这里写下来希望对大家有所帮助.话不 ...
随机推荐
- 苹果系统安装虚拟机 Mac如何安装虚拟机教程
1.前言 大家在用 Mac 系统的时候,可能有时难免还是要用到 Windows 系统.在 Mac 上使用 Windows 系统有二种方法.一种是在 Mac上安装双系统,适合要在机器上处理一些大型 ...
- 使用jQuery的animate方法制作滑动菜单
周末看Ziv小威的博客<制作滑动条菜单,如何延时处理滑动效果,避免动画卡顿>,参见地址:http://www.cnblogs.com/zivxiaowei/p/3462964.html.是 ...
- linux下遍历目录
遍历目录的主要思想 由于目录就是一颗树,所以遍历目录就转换为遍历一棵树.谈到树的遍历就再熟悉不过了,有树的前序.层次和后序遍历,我使用的是前序遍历,后序遍历和前序遍历本质上一样,而层次遍历要比前两个麻 ...
- 关于Docker目录挂载的总结
Docker容器启动的时候,如果要挂载宿主机的一个目录,可以用-v参数指定. 譬如我要启动一个centos容器,宿主机的/test目录挂载到容器的/soft目录,可通过以下方式指定: # docker ...
- Java继承与组合
Java继承与组合 继承 java 中使用extends关键字表示继承关系,当创建一个类时,如果没有明确指出要继承的类,则是隐式地从根类Object进行继承. 子类继承父类的成员变量 子类能够继承父类 ...
- 三步将Node应用部署到Heroku上
Heroku是一个提供快速部署服务的云平台.支持Node,Ruby,Java,PHP,Python,Go多种语言,今天体验了下,简直不要太爽.下面简单的介绍一下. 首先还是要注册一个账号:https: ...
- 搭建OpenGL环境-Windows/VS2013
对于opengl的环境,简单搭建的话其实和opencv差不多,你会看到下面的过程与opencv类似,不同的就是某些文件需要自己找(因为不是集成的,各个拓展需要单独下载) 1.首先,对于opengl头文 ...
- gradle学习笔记
一直想着花时间学习下gradle,今天有空.入门一下.参考:极客学院gradle使用指南,官方文档:gradle-2.12/docs/userguide/installation.html,以及百度阅 ...
- iOS的一些面试题分析总结(1)
本篇主要说一下多线程的东西,因为东西比较多,所以直接拿出一整个篇幅来说它了. 3. 多线程的底层实现? 1.先说一下什么是进程,什么是线程.什么是多线程. 进程是资源分配的单位,线程是调度运行的基本单 ...
- iOS 视图控制器 (内容根据iOS编程编写)
视图控制器是 UIViewController 类或其子类对象.每个视图控制器都负责管理一个视图层次结构,包括创建视图层级结构中的视图并处理相关用户事件,以及将整个视图层次结构添加到应用窗口. 创建 ...