最推荐的方式
var geolocation = new qq.maps.Geolocation("ECFBZ-ZJEE6-P43S2-ED7CA-XDGO7-Q6FAD", "myapp");
geolocation.getLocation(showPosition, showErr, options)
var options = {timeout: 8000};
function showPosition(position) {
let loc2={
longitude:position.lng,
latitude:position.lat
}
vm.$store.dispatch('pushlocation',loc2)
localStorage.setItem(
'loc',
JSON.stringify(loc2)
)
localStorage.setItem(
'position',
JSON.stringify(position)
)
};
function showErr() {
alert( "定位失败!");
};
第二种方式(腾讯的微信地图接口,但是需要配置jssdk)
wx.getLocation({
type: 'gcj02',
success(res){
vm.$store.dispatch('pushlocation', res)
localStorage.setItem(
'loc2',
JSON.stringify(res)
)
}
获取城市的接口
var url = "https://apis.map.qq.com/ws/geocoder/v1/?";
data.output = "jsonp";
$.ajax({
type: "get",
async: false,
dataType: 'jsonp',
data: data,
jsonp: "callback",
jsonpCallback: "QQmap",
url: url,
success: json => {
/*json对象转为文本 var aToStr=JSON.stringify(a);*/
// debugger
alert('是否成功')
let city = json.result.address_component.city
let citydata = { value: this.site.city, change: false }
this.$store.dispatch('pushcity', citydata)
localStorage.setItem(
'city',
JSON.stringify(json.result.address_component.city)
)
},
error: err => { alert("服务端错误,请刷新浏览器后重试") }
})
网友评论