先上核心代碼:
<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; // 位置精度
}
});