参数 | 含义 | 取值范围 | 是否必须 | 默认取值 |
---|---|---|---|---|
coords | 源坐标 | 是 | 无 | |
ak | 开发者密钥 | 是 | 无 | |
sn | 用户的权限签名 | 否 | 若用户所用ak的校验方式为sn校验时该参数必须。(sn生成算法) | |
from | 源坐标类型 | 否 | 默认为1,即GPS设备获取的坐标 | |
to | 目的坐标类型 | 否 | 默认为5,即bd09ll(百度坐标) | |
output | 返回结果格式 | 否 | json |
名称 | 类型 | 含义 | 取值范围 | |
---|---|---|---|---|
status | init | 状态码 | 正常0,异常非0,详细见状态码说明 | |
result | json或者xml数组 | 转换结果 | 与输入顺序一致 | |
| x | float | 横坐标 | |
y | float | 纵坐标 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
{
status : 0,
result :
[
{
x : 114.23074871003,
y : 29.579084787993
},
{
x : 114.23075628327,
y : 29.579081090996
}
]
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
function
initdata(){
//页面初始化
if
(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition,showError);
//HTML5获取GPS设备地理位置信息
}
else
{
document.getElementById(
"allmap"
).innerHTML=
"Geolocation is not supported by this browser."
;
}
}
function
showPosition(position){
var
x=position.coords.latitude;
//获取纬度
var
y=position.coords.longitude;
//获取经度
//转为百度地图坐标
//注意点:1、coords的经度、纬度顺序(可多组坐标转换,以;(分号)隔开)。2、from与to的准确性。3、callback为回调函数
var
positionUrl =
"http://api.map.baidu.com/geoconv/v1/?coords="
+y+
","
+x+
"&from=1&to=5&ak=你的密钥&callback=getMap"
;
var
script = document.createElement(
'script'
);
script.src = positionUrl;
document.getElementsByTagName(
"head"
)[0].appendChild(script);
}
function
getMap(data){
//返回的状态码,0为正常;1为内部错误;21为from非法;22为to非法;24为coords格式非法;25为coords个数非法,超过限制
if
(data.status!=0){
alert(
"地图坐标转换出错"
);
return
;
}
//result为数组
var
result = data.result;
var
lon = result[0].x;
//经度
var
lat = result[0].y;
//纬度
// 百度地图API功能
var
map =
new
BMap.Map(
"allmap"
);
// 创建Map实例
var
point =
new
BMap.Point(lon,lat);
map.centerAndZoom(point, 14);
map.addControl(
new
BMap.ZoomControl());
//添加地图缩放控件
var
marker1 =
new
BMap.Marker(point);
// 创建标注
map.addOverlay(marker1);
// 将标注添加到地图中
//创建信息窗口
var
infoWindow1 =
new
BMap.InfoWindow(
"您当前所处的位置,经度:"
+lon+
";纬度:"
+lat);
marker1.addEventListener(
"click"
,
function
(){
this
.openInfoWindow(infoWindow1);});
}
//HTML5获取地理位置信息错误处理
function
showError(error)
{
switch
(error.code)
{
case
error.PERMISSION_DENIED:
document.getElementById(
"allmap"
).innerHTML=
"User denied the request for Geolocation."
break
;
case
error.POSITION_UNAVAILABLE:
document.getElementById(
"allmap"
).innerHTML=
"Location information is unavailable."
break
;
case
error.TIMEOUT:
document.getElementById(
"allmap"
).innerHTML=
"The request to get user location timed out."
break
;
case
error.UNKNOWN_ERROR:
document.getElementById(
"allmap"
).innerHTML=
"An unknown error occurred."
break
;
}
}
|