javascript - 谷歌地图地理编码意外的财产位置
问题描述
我正在尝试为我的 vue.js 应用程序设置地理编码。我没有使用任何第三方库,只有通过 npm 安装的谷歌地图。
调用 geocode 方法时,它会抛出一个错误,指出属性位置是意外的。我试过“位置”、“纬度”、坐标本身等等。经度和纬度来自navigator.geolocation
对象。
这是我的设置脚本的一部分:
Vue.component('catalog', require('./components/Catalog'));
Vue.component('address-autocomplete', require('./components/AddressAutoComplete'));
window.googleMapsClient = require('@google/maps').createClient({
key: "MY_API_KEY"
});
还有我的 Vue 组件:
getLocationFromGeoData() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
this.setLocation,
this.showError
);
} else {
showLocationError("Geolocation wird nicht unterstützt!");
}
},
setLocation(position) {
var self = this;
this.location.latitude = position.coords.latitude;
this.location.longitude = position.coords.longitude;
var latLng = {
lat: parseFloat(this.location.latitude),
lng: parseFloat(this.location.longitude)
};
googleMapsClient.geocode({'location': latLng}).asPromise()
.then((response) => {
self.location.address = results[1].formatted_address;
}).catch((err) => {
showLocationError(err);
});
},
解决方案
请注意,您尝试将坐标解析为地址。此过程通常称为反向地理编码。这意味着您必须使用.reverseGeocode(query, callback)
方法而不是.geocode(query, callback)
方法。
您的代码应更改为类似于
var latLng = {
lat: parseFloat(this.location.latitude),
lng: parseFloat(this.location.longitude)
};
googleMapsClient.reverseGeocode({'latlng': latLng}).asPromise()
.then((response) => {
self.location.address = results[1].formatted_address;
}).catch((err) => {
showLocationError(err);
});
有关更多详细信息,请查看 Node.js 的 Maps Web Services 客户端库的文档,网址为
https://googlemaps.github.io/google-maps-services-js/docs/GoogleMapsClient.html
我希望这有帮助!
推荐阅读
- css - 有没有办法直接在类属性中使用 bootstrap 5 自定义颜色?
- python-3.x - 在 Python 中实现链表
- javascript - 我只想在模型查看器中播放带有音频的动画
- nlp - 使用 Huggingface TextClassificationPipeline 时如何设置标签名称?
- django - Djanog 在主模型和连接模型中以一对多的关系在其余 API 上发布数据
- reactjs - 如何在 react-table 单元格中实现 react-markdown?
- javascript - 为什么这个 XOR 加密不起作用?我正在尝试传递一些值,但它不起作用
- excel - 在 Excel 中选择要通过电子邮件发送的范围时需要 VBA 运行时错误 424 对象
- r - 在 R studio (ggplot, facet_wrap) 上创建折线图的插图
- java - 计算非二叉树的元素个数