谷歌浏览器暂不支持
1、引入百度地图api和jq
<script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=你的秘钥&s=1"></script>
<script src="https://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
2、获取经纬度和省市区
$(function () {
// 页面加载时执行getLocation函数
window.onload = getLocation;
if (supportsGeoLocation()) {
//alert("你的浏览器支持 GeoLocation.");
} else {
alert("不支持 GeoLocation.")
}
// 检测浏览器是否支持HTML5
function supportsGeoLocation() {
return !!navigator.geolocation;
}
// 单次位置请求执行的函数
function getLocation() {
navigator.geolocation.getCurrentPosition(mapIt, locationError);
}
//定位成功时,执行的函数
function mapIt(position) {
lon = position.coords.longitude;
lat = position.coords.latitude;
// alert("您位置的经度是:" + lon + " 纬度是:" + lat);
}
// 定位失败时,执行的函数
function locationError(error) {
console.log(error);
switch (error.code) {
case error.PERMISSION_DENIED:
alert("无法定位.");
break;
case error.POSITION_UNAVAILABLE:
alert("位置信息不可用.");
break;
case error.TIMEOUT:
alert("请求超时.");
break;
case error.UNKNOWN_ERROR:
alert("未知错误.");
break;
}
}
})
注:代码仅供参考