-
Notifications
You must be signed in to change notification settings - Fork 0
/
geolocation.html
113 lines (105 loc) · 4.19 KB
/
geolocation.html
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!DOCTYPE html>
<html lang="en"><head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>获取位置信息</title></head>
<script src="js/external.js" type="text/javascript"></script>
<script src="https://api.map.baidu.com/api?v=2.0&ak=C93b5178d7a8ebdb830b9b557abce78b&s=1" type="text/javascript"></script>
<body>
获取位置信息<div class="content-display"></div>
百度信息:<div id="baidu_geo"></div>
谷歌信息: <div id="google_geo"></div>
回到主页:<a href="/">点击回到主页</a>
<script type="text/javascript" >
function getLocation() {
var options = {
enableHighAccuracy: true,
maximumAge: 1000
} ;
if (navigator.geolocation) {
//浏览器支持geolocation
navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
} else {
//浏览器不支持geolocation
alert('您的浏览器不支持地理位置定位');
}
};
//成功时
function onSuccess(position) {
//返回用户位置 //经度
var contentEle = document.getElementsByClassName('content-display')[0];
var $baiduGeo = $('#baidu_geo');
var longitude = position.coords.longitude;
//纬度
var latitude = position.coords.latitude;
contentEle.innerHTML = contentEle.innerHTML + '<br>当前地址的经纬度:经度' + longitude + ',纬度' + latitude;
//根据经纬度获取地理位置,不太准确,获取城市区域还是可以的
var map = new BMap.Map("allmap");
var point = new BMap.Point(longitude, latitude);
var gc = new BMap.Geocoder();
gc.getLocation(point, function(rs) {
var addComp = rs.addressComponents;
contentEle.innerHTML = contentEle.innerHTML + '<br>' + addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber;
$baiduGeo.html('<br>省份(province):' + addComp.province + '<br>城市(city):' + addComp.city + '<br>地区(district):' + addComp.district + '<br>街道(street):' + addComp.street + '<br>街道号(streetNumber):' + addComp.streetNumber);
});
// 这里后面可以写你的后续操作了
// postData(longitude, latitude);
//谷歌地图信息
showPosition(position);
}
//失败时
function onError(error) {
switch (error.code) {
case 1:
alert("位置服务被拒绝");
break;
case 2:
alert("暂时获取不到位置信息");
break;
case 3:
alert("获取信息超时");
break;
case 4:
alert("未知错误");
break;
}
// 这里后面可以写你的后续操作了
//经度
var longitude = 23.1823780000;
//纬度
var latitude = 113.4233310000;
// postData(longitude, latitude);
};
function showPosition(position) {
var latlon = position.coords.latitude + ',' + position.coords.longitude;
//google
var url = 'https://maps.google.cn/maps/api/geocode/json?latlng=' + latlon + '&language=CN';
$.ajax({
type: "GET",
url: url,
beforeSend: function() {
$("#google_geo").html('正在定位...');
},
success: function(json) {
if (json.status == 'OK') {
console.log(json);
var results = json.results;
$.each(results, function(index, array) {
if (index == 0) {
$("#google_geo").html(array['formatted_address']);
}
});
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$("#google_geo").html(latlon + "地址位置获取失败");
}
});
};
window.onload = function() {
// html5获取地理位置
getLocation();
};
</script>
</body>
</html>