<p><button class="btn btn-success btns">点击获取坐标</button></p>
<p>经度: <input id="longitude" type="text" value="" /></p>
<p>纬度: <input id="latitude" type="text" value="" /></p>
通过获取GPS坐标转成百度坐标;
添加百度地图插件
https://api.map.baidu.com/api?v=2.0&s=1&ak=百度秘钥
$(".btns").click(function () {
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function success(pos) {
var crd = pos.coords;
console.log('GPS-Latitude : ' + crd.latitude);
console.log('GPS-Longitude: ' + crd.longitude);
var lat = crd.latitude;
var lng = crd.longitude;
var pointBak = new BMap.Point(lng, lat);
var convertor = new BMap.Convertor();
convertor.translate([pointBak], 1, 5, function (resPoint) {
if (resPoint && resPoint.points && resPoint.points.length > 0) {
var longitude = resPoint.points[0].lng;
var latitude = resPoint.points[0].lat;
console.log('百度纬度 : ' + latitude);
console.log('百度经度: ' + longitude);
var baseUrl = 'http://api.map.baidu.com/geocoder/v2/?ak=eIxDStjzbtH0WtU50gqdXYCz&callback=renderReverse&location='+latitude+','+longitude+'&output=json&pois=1';
$.ajax({
type: "POST",
url: baseUrl,
dataType: "JSONP",
async: false,
success: function (cname) {
if (parseInt(cname.status) === 0) {
var addr = cname.result.formatted_address;
$("#address").attr("value",addr);
//获取百度坐标详细地址
console.log(cname.result.formatted_address);
}
}
});
$("#longitude").attr("value",longitude);
$("#latitude").attr("value",latitude);
}
});
}
function error(err) {
console.warn('ERROR(' + err.code + '): ' + err.message);
}
navigator.geolocation.getCurrentPosition(success, error, options);
});