javascript+mapbar实现地图定位


Posted in Javascript onApril 09, 2010

本文地图使用的是图地图 
图吧地图在线API地址
http://union.mapbar.com/apidoc/
离线CHM格式 下载地址: 
http://union.mapbar.com/apidoc/chm/mapbarapi.rar

效果图:

javascript+mapbar实现地图定位 

Mapbar 地图 API 让您可以使用 JavaScript 将 Mapbar地图嵌入您自己的网页中。API 提供了许多方法与地图交互(正如http://www.mapbar.com/localsearch/index.html 网页上显示的),以及一系列向地图添加内容的服务,从而使您可以在自己的网站上创建稳定的地图应用程序。
公共测试密钥:

http://union.mapbar.com/apis/maps/free?f=mapi&v=31.2&k=aCW9cItqL7sqT7AxaB0zdHZoZSWmbBsuT7JhMHTsMeD6ZIl9NzFsZHT=@JBL979@Iu7lJJZWWq0IDu9xZMzMxq7I9AhH7LAAA6hqzZHZZLTbZZauxlDz7C7DD9ZCFGT=

如果您想试试 Mapbar 地图,省略申请密钥的步骤,可以使用公共测试密钥在本地(http://localhost)进行测试。
Internet Explorer 8.0 版本中存在兼容问题,需要在网页 <head> 标签间增加 <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> 标签以保证地图折线功能正确执行。
这里只有前台部分源码
你需要在你的项目中ajax来实现定位持久化
代码如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title> 地图测试 </title> 
<script type = "text/javascript" src = "http://union.mapbar.com/apis/maps/free?f=mapi&v=31.2&k=aCW9cItqL7..."></script> 
<script type="text/javascript"> 
var maplet=null;//地图对象 
var marker=null;//标记对象 
var le=null;//缩放级别 
var myEventListener=null;//地图click事件句柄 
function initMap()//初始化函数 
{ //转帖请注明出处 http://Qbit.cnblogs.com 
le=10; //默认缩放级别 
maplet = new Maplet("mapbar"); 
//这里可以初始化地图坐标比如从数据库中读取 然后在页面上使用小脚本的形式 
//如: maplet.centerAndZoom(new MPoint(<%=维度%>, <%=经度%>),<%=缩放级别%>); 
maplet.centerAndZoom(new MPoint(116.38672, 39.90805), le);//初始化地图中心点坐标并设定缩放级别 
maplet.addControl(new MStandardControl()); 
} 
function setp() 
{ 
if (marker)//判定是否已经添加标记 
{ 
alert("已经添加过标记了"); 
return; 
} 
maplet.setMode("bookmark");//设定为添加标记模式 
maplet.setCursorIcon("tb1.gif"); //添加鼠标跟随标签 
myEventListener = MEvent.bind(maplet, "click", this, addp); //注册click事件句柄 
} 
//这里的参数要写全即使你不使用event 
function addp(event,point){ 
if(!marker){ 
marker = new MMarker( point, //坐标 
new MIcon("mark.gif", 24, 24),//标签ICO(图片,大小) 
new MInfoWindow("蔡瑞福庄河市", "史上最佳"),//标注对象 
new MLabel("蔡瑞福")//小标签 
); 
marker.bEditable=true; 
marker.dragAnimation=true; 
maplet.addOverlay(marker);//添加标注 
marker.setEditable(true); //设定标注编辑状态 
maplet.setMode("pan"); //设定地图为拖动(正常)状态 
le= maplet.getZoomLevel(); //获取当前缩放级别 
document.getElementById("findp").style.display="block"; 
document.getElementById("delp").style.display="block"; 
document.getElementById("savep").style.display="block"; 
MEvent.removeListener(myEventListener);//注销事件 
} 
} 
//查找标记 
function find(){ 
maplet.centerAndZoom(marker.pt, le);//定位标记 
} 
//移除所有标记 
function del(){ 
//移除已经设定的坐标 
maplet.clearOverlays(true); 
location.reload(); //在重新添加的时候有点bug 我这里是直接刷新页面 来重置 
/*document.getElementById("findp").style.display="none"; 
document.getElementById("delp").style.display="none"; 
document.getElementById("savep").style.display="none"; 
maplet=null; 
marker=null; 
myEventListener=null; 
initMap();*/ 
} 
//提取标记数据 
function savep() 
{ 
alert("当前坐标点\n经度:"+marker.pt.lon+"\n维度:"+marker.pt.lat+"\n缩放级别:"+le); 
} 
</script> 
</head> 
<body onload="initMap()"> 
<table width="501"> 
<tr><td><input type="button" value="添加标注" onclick="setp()"/></td> 
<td><input type="button" id="findp" value="查看标记" style="display:none;" onclick="find()"/></td> 
<td><input type="button" id="delp" value="删除标记" style="display:none;" onclick="del()"/></td> 
<td><input type="button" id="savep" value="保存" style="display:none;" onclick="savep()"/></td> 
</tr> 
<tr><td colspan="4"><div id="mapbar" style="width:500px;height:300px"></div> 
</td></tr> 
</table> 
</body> 
</html>

源码下载地址: http://xiazai.3water.com/201004/yuanma/mapbar.rar
Javascript 相关文章推荐
artDialog 4.1.5 Dreamweaver代码提示/补全插件 附下载
Jul 31 Javascript
JavaScript实现select添加option
Jul 03 Javascript
js实现简单的手风琴效果
Feb 27 Javascript
详解Angular-Cli中引用第三方库
May 21 Javascript
iframe高度自适应及隐藏滚动条的实例详解
Sep 29 Javascript
React为 Vue 引入容器组件和展示组件的教程详解
May 03 Javascript
vsCode安装使用教程和插件安装方法
Aug 24 Javascript
JavaScript实现数字前补“0”的五种方法示例
Jan 03 Javascript
jQuery ajax仿Google自动提示SearchSuggess功能示例
Mar 28 jQuery
详解webpack-dev-middleware 源码解读
Mar 23 Javascript
详解在Vue.js编写更好的v-for循环的6种技巧
Apr 14 Javascript
如何在vue中使用jointjs过程解析
May 29 Javascript
innerHTML 和 getElementsByName 在IE下面的bug 的解决
Apr 09 #Javascript
Javascript string 扩展库代码
Apr 09 #Javascript
JavaScript 设计模式之组合模式解析
Apr 09 #Javascript
跟着Jquery API学Jquery之一 选择器
Apr 07 #Javascript
基于JQuery的cookie插件
Apr 07 #Javascript
JQuery为textarea添加maxlength属性的代码
Apr 07 #Javascript
JavaScript和JQuery实用代码片段(一)
Apr 07 #Javascript
You might like
js限制checkbox勾选的个数以及php获取多个checkbbox的方法深入解析
2013/07/18 PHP
PHP面向对象精要总结
2014/11/07 PHP
thinkPHP数据库增删改查操作方法实例详解
2016/12/06 PHP
php获取是星期几的的一些常用姿势
2019/12/15 PHP
php解析非标准json、非规范json的方式实例
2020/12/10 PHP
用 JavaScript 迁移目录
2006/12/18 Javascript
用一段js程序来实现动画功能
2007/03/06 Javascript
去掉gridPanel表头全选框的小例子
2013/07/18 Javascript
Js放到HTML文件中的哪个位置有什么区别
2013/08/21 Javascript
js实现点击左右按钮轮播图片效果实例
2015/01/29 Javascript
javascript遇到html5的一些表单属性
2015/07/05 Javascript
jQuery实现的选择商品飞入文本框动画效果完整实例
2016/08/10 Javascript
JSONP跨域请求
2017/03/02 Javascript
vue组件中点击按钮后修改输入框的状态实例代码
2017/04/14 Javascript
Layer弹出层动态获取数据的方法
2018/08/20 Javascript
vue 组件中使用 transition 和 transition-group实现过渡动画
2019/07/09 Javascript
js 实现ajax发送步骤过程详解
2019/07/25 Javascript
解决layui数据表格排序图标被超出的表头挤出去的问题
2019/09/19 Javascript
node.js express捕获全局异常的三种方法实例分析
2019/12/27 Javascript
[15:23]教你分分钟做大人:虚空假面
2014/10/30 DOTA
win7 下搭建sublime的python开发环境的配置方法
2014/06/18 Python
Python访问MySQL封装的常用类实例
2014/11/11 Python
fastcgi文件读取漏洞之python扫描脚本
2017/04/23 Python
python 每天如何定时启动爬虫任务(实现方法分享)
2018/05/21 Python
Python常见读写文件操作实例总结【文本、json、csv、pdf等】
2019/04/15 Python
Python利用myqr库创建自己的二维码
2020/11/24 Python
python+opencv实现车道线检测
2021/02/19 Python
Athleta官网:购买女士瑜伽服、技术运动服和休闲运动服
2020/11/12 全球购物
css animation配合SVG制作能量流动效果
2021/03/24 HTML / CSS
生产内勤岗位职责
2013/12/07 职场文书
共筑中国梦演讲稿
2014/04/23 职场文书
最美家庭活动方案
2014/08/31 职场文书
党的群众路线对照检查材料思想汇报
2014/09/25 职场文书
导游词之神仙居景区
2019/11/15 职场文书
导游词之南京汤山温泉
2019/11/26 职场文书
Android Studio实现简易进制转换计算器
2022/05/20 Java/Android