// 获取定位
// 方法一
getMyLocation() {
var geolocation = new qq.maps.Geolocation("CQPBZ-QRWCU-2RGVJ-44CGR-5WCY6-WVB5Q", "myapp_ay");
geolocation.getIpLocation(
(position) => {
console.log(position);
this.getAddress(position.lat, position.lng)
},
(err) => {
console.log("定位失败");
this.autoLocation = '安阳市';
this.getMyLocation();
},
);
// geolocation.getIpLocation(this.showPosition, this.showErr);
},
showPosition(position) {
console.log(position);
this.autoLocation = position.addr || position.city || '安阳市';
},
showErr() {
console.log("定位失败");
this.autoLocation = '安阳市';
this.getMyLocation(); //定位失败再请求定位,测试使用
},
// 方法二
getPosition(){
let vm = this
let data = {
key: "CQPBZ-QRWCU-2RGVJ-44CGR-5WCY6-WVB5Q", //申请的密钥
output: "jsonp",
};
let url = "https://apis.map.qq.com/ws/location/v1/ip"
this.$jsonp(url, data).then(res => {
console.log(res)
}).catch(err => {
this.$toast(err.message)
})
},
// 方法三
getCurLocation() {
var geolocation = new qq.maps.Geolocation("CQPBZ-QRWCU-2RGVJ-44CGR-5WCY6-WVB5Q", "myapp_ay");
/**
* getLocation(sucCallback, errCallback, [options: {timeout: number, failTipFlag: boolean}])
* sucCallback为定位成功回调函数,必填;
* errCallback为定位失败回调函数,选填,如果不填,请设为null;
* options为定位选项,选填,可以通过timeout参数设置定位的超时时间,默认值为10s;
* failTipFlag: 是否在定位失败时给出提示引导用户打开授权或打开定位开关。(即将支持)
*/
var options = {
timeout: 10000,
failTipFlag: true
};
geolocation.getLocation(
function (position) {
console.log(position);
},
null,
options
);
},
//方法四
走微信公众号接口获取地理位置接口,需要有服务号配合然后使用!
完整简单实例体现
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no">
</head>
<body>
<script type="text/javascript" src="https://3gimg.qq.com/lightmap/components/geolocation/geolocation.min.js"></script>
<script src="https://cdn.nikm.cn/js/jquery.js"></script>
<script type="text/JavaScript">
var geolocation = new qq.maps.Geolocation("YNFBZ-N5BRV-ZCJPS-USFGL-BSMHZ-YZFNZ", "开发调试");
function showPosition(position) { console.log(position);
console.log('您当前的位置是'+position.province+position.city+position.district+position.addr);
console.log('您当前的经纬度是'+position.lat+','+position.lng); };
geolocation.getLocation(showPosition);
</script>
</body>
</html>