google-maps - 在组件内部但在特定元素内部初始化 Google 地图
问题描述
我使用本教程来使用 Google 地图 API https://markus.oberlehner.net/blog/using-the-google-maps-api-with-vue/并且运行良好。但它用于this.$el
在组件内加载 Google 地图实例,这会占用所有屏幕,我想在内部加载,.map
这样我就可以拥有更小的地图。
<template>
<div class="container-fluid">
init map
<div class="map">
</div>
</div>
</template>
<script>
import ProgressSpinner from '.././Preloader';
import { mapGetters } from 'vuex'
import gmapsInit from '../.././utils/gmaps';
export default {
name: 'Tracking',
data: () => ({
}),
async mounted() {
$('.container-fluid').bootstrapMaterialDesign();
try {
const google = await gmapsInit();
const geocoder = new google.maps.Geocoder();
const map = new google.maps.Map(this.$el);
geocoder.geocode({ address: 'Austria' }, (results, status) => {
if (status !== 'OK' || !results[0]) {
throw new Error(status);
}
map.setCenter(results[0].geometry.location);
map.fitBounds(results[0].geometry.viewport);
});
} catch (error) {
console.error('Error with calling Google maps API: ',error);
}
},
computed: {
...mapGetters([])
},
created(){
this.$store.dispatch ('allPoints')
.then(()=> console.log('points'))
.catch( () => console.log('no poiints ') )
},
methods: {
},
components: {
ProgressSpinner
}
}
</script>
<style scoped>
.map {
width: 150px;
}
</style>
解决方案
样式的问题,所以我应该给.map
元素增加高度。
推荐阅读
- vue.js - 运行测试时禁用 cypress 文件夹的生成
- php - sendmail 在 localhost 中有效,但在 heroku 中无效
- postgresql - 在 Windows 10 上使用 createdb 命令 postgresql 时出错
- azure-data-factory-2 - Copy Activity Source 的 REST 数据集给我错误 Invalid PaginationRule
- ios - UnifiedContacts(matching:keysToFetch:) 是否只返回统一联系人而忽略非统一联系人?
- arrays - 反应中的打字稿:包含字符串数组的对象数组,有时包含 JSX 元素
- javascript - 将文本区域值拆分为数组然后循环并运行函数
- visual-studio-2022 - 选择搜索时如何阻止 VS 2022 标准版突出显示代码
- amazon-web-services - 无法验证转移到 AWS 的域,因此我可以发送邮件
- javascript - 如何处理 Jest 测试用例中的 Promise 拒绝?