js实现百度地图定位于地址逆解析,显示自己当前的地理位置
作者:myYouth 时间:2024-05-08 10:12:04
话不多说,随小编一起看看实例代码吧
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>//这个需要单独申请,只有获取到这个密匙那么才可以使用百度地图
//获取密钥地址:http://lbsyun.baidu.com/index.php?title=jspopular进入之后点击获取密钥
<title>浏览器定位</title>
</head>
<body>
<div id="allmap"></div>
</body>
</html>
<script type="text/javascript">
// 百度地图API功能
var map = new BMap.Map("allmap");
var point = new BMap.Point(116.331398,39.897445);
map.centerAndZoom(point,12);
//以上参数不用设置
//以下是获取当前的地理位置
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(r){
if(this.getStatus() == BMAP_STATUS_SUCCESS){
//表示获取成功那么 r 这个参数就包含有当前的地理位置经纬度
//逆地址解析,就是要把当前的经纬度转为当前具体地理位置
//逆地址解析
var geoc = new BMap.Geocoder();
var pt = new BMap.Point(p_x, p_y);//实例化这两个点
geoc.getLocation(pt, function (rs) {
var addComp = rs.addressComponents;
$('.its-place').html(addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber)
alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
//对应的省市区、县街道,街道号
});
}else {
alert('failed'+this.getStatus());
}
},{enableHighAccuracy: true})
//关于状态码
//BMAP_STATUS_SUCCESS 检索成功。对应数值“0”。
//BMAP_STATUS_CITY_LIST 城市列表。对应数值“1”。
//BMAP_STATUS_UNKNOWN_LOCATION 位置结果未知。对应数值“2”。
//BMAP_STATUS_UNKNOWN_ROUTE 导航结果未知。对应数值“3”。
//BMAP_STATUS_INVALID_KEY 非法密钥。对应数值“4”。
//BMAP_STATUS_INVALID_REQUEST 非法请求。对应数值“5”。
//BMAP_STATUS_PERMISSION_DENIED 没有权限。对应数值“6”。(自 1.1 新增)
//BMAP_STATUS_SERVICE_UNAVAILABLE 服务不可用。对应数值“7”。(自 1.1 新增)
//BMAP_STATUS_TIMEOUT 超时。对应数值“8”。(自 1.1 新增)
</script>
来源:http://www.cnblogs.com/-youth/p/6145379.html
标签:js,地图定位
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JavaScript观察者模式原理与用法实例详解
2024-04-19 10:02:48
![](https://img.aspxhome.com/file/2023/4/135924_0s.png)
python计算auc指标实例
2021-12-21 12:10:03
使用pycallgraph分析python代码函数调用流程以及框架解析
2023-06-05 08:52:56
![](https://img.aspxhome.com/file/2023/2/100842_0s.png)
asp如何检测HTTP表头信息?
2010-06-28 18:20:00
Keras设定GPU使用内存大小方式(Tensorflow backend)
2021-12-20 22:15:12
python实现词法分析器
2022-09-12 15:03:18
![](https://img.aspxhome.com/file/2023/6/93956_0s.png)
asp.net 为FCKeditor开发代码高亮插件实现代码
2023-09-26 00:30:16
![](https://img.aspxhome.com/file/2023/4/71284_0s.jpg)
mysql查询优化之100万条数据的一张表优化方案
2024-01-13 20:10:04
sql语句查询重复的数据(最新推荐)
2024-01-13 08:01:23
用python开发一款操作MySQL的小工具
2024-01-26 08:55:19
JS获取当前时间的实例代码(昨天、今天、明天)
2024-04-23 09:28:36
Python全景系列之模块与包全面解读
2022-12-09 19:26:48
解析Python中while true的使用
2022-07-23 21:19:53
Python时间戳转换为字符串与字符串转换为时间戳
2023-04-20 12:47:56
CSS实例讲解:地图提示
2007-05-11 17:04:00
![](https://img.aspxhome.com/file/uploadpic/20075/20075111742681.jpg)
python正则匹配查询港澳通行证办理进度示例分享
2021-05-09 19:29:55
SQLServer中JSON文档型数据的查询问题解决
2024-01-19 00:41:49
![](https://img.aspxhome.com/file/2023/7/102007_0s.png)
把论坛从ACCESS转成SQL版本
2009-04-13 15:59:00
Python 里最强的地图绘制神器
2023-07-17 12:36:43
![](https://img.aspxhome.com/file/2023/6/95416_0s.jpg)
解决mysql ERROR 1017:Can't find file: '/xxx.frm' 错误
2024-01-13 18:57:43