先上核心代码:
<script>
AddressAutoInfoFun();
function AddressAutoInfoFun() {
//document.getElementById("loadingIcon").style.display="inline";
var geoc = new BMap.Geocoder();
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function (r) {
if (this.getStatus() == BMAP_STATUS_SUCCESS) {
var mk = new BMap.Marker(r.point);
//map.addOverlay(mk);
//map.panTo(r.point);
console.log("当前位置经度为:" + r.point.lng + "纬度为:" + r.point.lat);
$('#Longitude').val(r.point.lng);
$('#Latitude').val(r.point.lat);
wx.getLocation({
type: 'gcj02', // 默认为wgs84的gps座标,如果要返回直接给openLocation用的火星座标,可传入'gcj02'
success: function (res) {
r.point.lng = res.longitude;
r.point.lat = res.latitude;
setLocation(r.point);
}
});
} else {
console.log('无法定位到您的当前位置,导航失败,请手动输入您的当前位置!' + this.getStatus());
}
}, { enableHighAccuracy: true });
//获取地理位置的函数
function setLocation(point) {
geoc.getLocation(point, function (rs) {
var addComp = rs.addressComponents;
var result = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
$('#Location').val(result);
//alert(result);
});
}
}
</script>
微信公众号这边要用SDK导入,这个不懂的就私聊
,我这边的话思路是:
用微信弄出经纬度,然后再用百度地图去弄出我们的详细地址,但是这个地址也不是很详细那种。
代码分开如下:
<script type="text/javascript" src="http://api.map.baidu.com/api?ak=【你懂得,这个要去百度申请,是免费的】&v=2.0&services=false"></script>
<script>
AddressAutoInfoFun();
function AddressAutoInfoFun() {
//document.getElementById("loadingIcon").style.display="inline";
var geoc = new BMap.Geocoder();
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function (r) {
if (this.getStatus() == BMAP_STATUS_SUCCESS) {
var mk = new BMap.Marker(r.point);
//map.addOverlay(mk);
//map.panTo(r.point);
console.log("当前位置经度为:" + r.point.lng + "纬度为:" + r.point.lat);
$('#Longitude').val(r.point.lng);
$('#Latitude').val(r.point.lat);
setLocation(r.point);
} else {
console.log('无法定位到您的当前位置,导航失败,请手动输入您的当前位置!' + this.getStatus());
}
}, { enableHighAccuracy: true });
//获取地理位置的函数
function setLocation(point) {
geoc.getLocation(point, function (rs) {
var addComp = rs.addressComponents;
var result = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
$('#Location').val(result);
//alert(result);
});
}
}
</script>
wx.getLocation({
type: 'wgs84', // 默认为wgs84的gps座标,如果要返回直接给openLocation用的火星座标,可传入'gcj02'
success: function (res) {
var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
var speed = res.speed; // 速度,以米/每秒计
var accuracy = res.accuracy; // 位置精度
}
});