转载 一个屏幕坐标和地图坐标转换的js代码收藏

一个屏幕坐标和地图坐标转换的js代码

关键词javascript                                          

一个屏幕坐标和地图坐标转换的js代码

    在开发webgis的时候 在客户端 经常要处理 屏幕坐标和地图坐标的转换关系,特别是现在 web2.0时代,要求ie 交互和展现更加丰富,对坐标转换要求就更多了。因此在项目中写了个 通用的 转换 javascript代码。在实际项目 得到了较好的应用
具体代码如下 (需要prototype.js的支持)


/*
*共有三个类
*whpoint 点
*whrect 矩形
*whcoordchange坐标转换类
*/

whpoint=Class.create();

whpoint.prototype={
initialize:function(x,y){
this.x=x;
this.y=y;
}
};


whrect=Class.create();
whrect.prototype={
initialize:function(pointmin,pointmax){
this.point1=pointmin;
this.point2=pointmax;
this.width=this.point2.x-this.point1.x;
this.height=this.point2.y-this.point1.y;

},

getwidth:function(){
return this.point2.x-this.point1.x;
},

setwidth:function(newwidth){

this.width=newwidth;
},

getheight:function(){
return this.point2.y-this.point1.y;
},

setheight:function(newheight){
this.height=newheight;
},

setpointmax:function(pointmax){
this.point2=pointmax;
},

setpointmin:function(pointmin){
this.point1=pointmin;
},

getminx:function(){

return this.point1.x;
},
getminy:function(){
return this.point1.y;
},
getmaxx:function(){

return this.point2.x
},

getmaxy:function(){

return this.point2.y;
}


};


whcoordchange=Class.create();

whcoordchange.prototype={
initialize:function(screenrect,maprect){
this.screenrect=screenrect;
this.maprect=maprect;

},
getmapextent:function(screenpoint,twidth,theight){
var screenbl_x=screenpoint.x-twidth/2;
var screenbl_y=screenpoint.y+theight/2;
var tempscreenblp=new whpoint(screenbl_x,screenbl_y);
var resmapblp=this.screentomap(tempscreenblp);

var screenur_x=screenpoint.x+twidth/2;
var screenur_y=screenpoint.y-theight/2;
var tempscreenurp=new whpoint(screenur_x,screenur_y);
var resmapurp=this.screentomap(tempscreenurp);

return new whrect(resmapblp,resmapurp);
 
},

screentomap:function(screenpoint){

var resmapx=this.maprect.getminx()+

this.maprect.getwidth()*(screenpoint.x-this.screenrect.getminx())/this.screenrect.getwidth();
var resmapy=this.maprect.getmaxy()-

this.maprect.getheight()*(screenpoint.y-this.screenrect.getminy())/this.screenrect.getheight();

return new whpoint(resmapx,resmapy);
},

maptoscreen:function(mappoint){
var resscreenx=this.screenrect.getminx()+

this.screenrect.getwidth()*(mappoint.x-this.maprect.getminx())/this.maprect.getwidth();
var resscreeny=this.screenrect.getminy()+

this.screenrect.getheight()*(this.maprect.getmaxy()-mappoint.y)/this.maprect.getheight();
return new whpoint(Math.ceil(resscreenx),Math.ceil(resscreeny));

}

};


//////////////////////////////////////
/////////////////////////////////

实际页面中的应用实例
  
 
    var scminp=new whpoint(10,10);
    var scmaxp=new whpoint(400,300);
    var screct=new whrect(scminp,scmaxp);
   
     var mminp=new whpoint(120.235,30.235);
    var  mmaxp=new whpoint(120.265,30.265);   
   
    var mrect=new whrect(mminp,mmaxp);
   
    //建立 屏幕坐标和 地图坐标的对应关系 注意应该是 鹰眼的范围 对应 地图的范围
    var mycoordchange=new whcoordchange(screct,mrect);
   
    var scpoint1=new whpoint(10,300);
    var mapscpoint1=mycoordchange.screentomap(scpoint1);
   
    alert(mapscpoint1.x+";"+mapscpoint1.y);
   
     var mapoint1=new whpoint(120.250,30.250);
    var scmapoint1=mycoordchange.maptoscreen(mapoint1);
   
    alert(scmapoint1.x+";"+scmapoint1.y);
   
    //根据屏幕坐标点 相应鹰眼redbox的 width 和 height 获取相应的 地图extent,
    var maprect=mycoordchange.getmapextent(scmapoint1,195,145);
   
    alert (maprect.getminx()+";" + maprect.getminy()+";"+maprect.getmaxx()+";"+ maprect.getmaxy());
    
 

发表于 @ 2007年04月15日 17:27:00|评论(loading...)

新一篇: 另辟渠道的webgis应用开发(附源码) | 旧一篇: 用 Spring MVC 轻松进行应用程序开发

用户操作
[即时聊天] [发私信] [加为好友]
luckisok
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
luckisok的公告
欢迎交流计算机技术 Free Counter
Free Counter
文章分类
收藏
程序设计
codeproject
JAVA开源大全
MSDN
sourceforge.net
vc知识库
中国协议分析网
中国开源网
代码中国
张孝祥专栏
微软学生中心
教程在线
易得资源网
游戏开发文摘
编程岁月
电子图书
(英语)计算机图书
BOOKD电脑书
COOLGO电子书
itebookhome
IT电子书论坛
NetYi电脑图书
中华电脑书库
中国信息网
公益电子书
小李书屋
我爱E书网
有声教育
计算机图书ENGLISH
科研资源
IBM大学合作部
丁香园
三思科学网
中国学术网
中国数学资源网
中国高等学校教育网
博研论坛
国家自然科学基金委员会
小木虫论坛
思想者
思想论坛
梦想三峡
研学论坛
网上读书园地
读书花园网
零点花园
青稞学习论坛
鸭绿江论坛
数据库
啄木鸟Python开发社区
数据库模型设计专栏
下载站点
51CT
中国下载站
中国破解联盟
中国绿软基地
全方位下载
天网千帆FTP搜索
我要去下载
木蚁软件园
未来软件
比特论坛
绿色软件
藏经阁
零度软件园
非凡软件
新闻站点
10ms快速网
全球中文论坛
博客中国
和讯
西西弗斯的博客
学习站点
美河学习在线
CSDN
good免费教程
IBM中国开发站
IT学习网
OOP 论坛
中国IT实验室
中国开放式教育资源共享协会
中国教程网
中国计算机教学网
代理中国
千源网
国家精品课程中心
大学课程在线
学习在线论坛
疯狂学习论坛
笨娃娃学习论坛
编程爱好者
美诺考试学习网
视频资源网
计算机教学
豆豆学习网
麻省理工开放式课件
音乐站点
ALL mp3
红雨音乐吧
音乐听厅
存档
软件项目交易
Csdn Blog version 3.1a
Copyright © luckisok