微信获取地理位置
wx.config({
debug: false,
appId: "", // 必填,公众号的唯一标识
timestamp: "", //必填,生成签名的时间戳
nonceStr: "", // 必填,生成签名的随机串
signature: "",// 必填,签名,见附录1
jsApiList: [ // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
"getLocation",
"openLocation"
]
});
wx.ready(function(){
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; // 位置精度
$('#location').click(function(){
position(latitude,longitude);
})
//position(latitude,longitude);
}
});
});
function position(latitude,longitude){
$.ajax({
type:"",
data: {
latitude:latitude,
longitude:longitude
},
async: false,
dataType:"json",
success:function(data){
x = data.position.x;
y = data.position.y;
}
})
var point = new BMap.Point(x,y);
// alert(point);
// map.centerAndZoom(point,12);
var geoc = new BMap.Geocoder();
var pt = point;
geoc.getLocation(pt, function(rs){
var addComp = rs.addressComponents;
var location = addComp.city + ", " + addComp.district + ", " + addComp.street;
$('#location').text(location);
// alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
});