Vue中使用google map (china)

  1. 獲取 google key
  2. index 文件中 引入
<script src="http://www.google.cn/maps/api/js?key="></script>
  1. vue.config.js 中配置 ( 適用于vue-cli3以后版本 )
 configureWebpack: config => {
        return {
            externals: {
                'google': 'google'
            }
        }
    }  
  1. 相關(guān)組件中引入
import google from 'google'
  1. 初始定義
        initMaps() {
            this.maps = new google.maps.Map(document.getElementById("allmap"), {
                zoom: 8,
                //設(shè)置地圖中心點(diǎn)
                center: { lng: 107.3951, lat: 34.491 },
                //為了關(guān)閉默認(rèn)控件集,設(shè)置地圖的disableDefaultUI的屬性為true
                disableDefaultUI: true,
                // 啟用縮放和平移
                gestureHandling: "greedy",
                // hybrid包含衛(wèi)星和地名
                mapTypeId: "hybrid",
                //語言可選值:en,zh_en, zh_cn
                language: "zh_cn"
            });
        }

加載

mounted() {
this.initMaps();
}
  1. 添加多個標(biāo)記 / 彈窗
        addMarkers() {
            this.clearMarkers();
            this.markers.forEach(item => {
                let marker = new google.maps.Marker({
                    position: item.position,
                    map: this.map,
                    label: {
                        text: item.ame
                    }
                });
                let contentString = ` ${item.content} `;
                var infowindow = new google.maps.InfoWindow({
                    content: contentString
                });
                //鼠標(biāo)覆蓋時顯示彈框,鼠標(biāo)移開隱藏彈框
                marker.addListener("mouseover", () => {
                    infowindow.open(this.maps, marker);
                });
                marker.addListener("mouseout", () => {
                    infowindow.close(this.maps, marker);
                });
                this.markers.push(marker);
            });
        },
        // 清除標(biāo)記
        clearMarkers() {
            for (var i = 0; i < this.markers.length; i++) {
                this.markers[i].setMap(null);
            }
            this.markers = [];
        },
  1. 添加地圖是否加載完畢
            this.map.addListener("tilesloaded", () => {
                this.loading = false;
            });
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容