获取位置性息:先通过微信公众号获得经纬度,然后根据经纬度换取位置
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.4"></script>
<script>
var signature = '@ViewBag.signature';
//微信
wx.config({
debug: false,
appId: '211',
timestamp:12 ,
nonceStr: '121',
signature: signature,
jsApiList: [
'getLocation']
});
wx.ready(function () {
wx.getLocation({
success: function (res) {
var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
var speed = res.speed; // 速度,以米/每秒计
var accuracy = res.accuracy; // 位置精度
getAddress(latitude, longitude);
}, cancel: function (res) {
alert('用户拒绝授权获取地理位置');
},
fail: function (res) {
alert("获取位置失败");
}
});
});
function getAddress(latitude, longitude) {
var map = new BMap.Map("allmap");
var point = new BMap.Point(longitude, latitude);
var gc = new BMap.Geocoder();
gc.getLocation(point, function (rs) {
var addComp = rs.addressComponents;
var mapAddress = addComp.province + addComp.city + addComp.district
+ addComp.street + addComp.streetNumber;
vm.address = mapAddress;
});
}
计算距离:
/**
* 计算两点间的距离
* pt1 {lng:"12.34",lat:"3423"}第一个点的经纬度
* pt2 {lng:"12.34",lat:"3423"}第二个点的经纬度
* */
getDistance:function(pt1,pt2){
var map = new BMap.Map("container");
var point1 = new BMap.Point(pt1.lng,pt1.lat);
var point2 = new BMap.Point(pt2.lng,pt2.lat);
var distance = map.getDistance(point1,point2);
return distance;
},