Google maps 添加多个gmap标记
我有一个显示地图的vue2谷歌地图应用程序 在此之前,我使用此代码Google maps 添加多个gmap标记,google-maps,vue.js,Google Maps,Vue.js,我有一个显示地图的vue2谷歌地图应用程序 在此之前,我使用此代码 <GmapMarker ref="myMarker" :position="google && new google.maps.LatLng(1.38, 103.8)" /> <GmapMarker ref="myMarker" :position="google &a
<GmapMarker
ref="myMarker"
:position="google && new google.maps.LatLng(1.38, 103.8)"
/>
<GmapMarker
ref="myMarker"
:position="google && new google.maps.LatLng(3.1488976, 101.6126774)"
/>
并将{{latlng}}
应用到
但是我有问题把{{latlng}}应用到标记中。
有人能解释一下如何应用它们吗?要生成多个标记,您需要在标记集合中推送纬度和经度,然后迭代标记。我已经根据需要更新了代码
<template>
<div>
<GmapMap
ref="gmap"
:center="getCenter()"
:zoom="11"
style="height: 500px;margin: 0px -8px;"
class="no-padding"
:options="getOptions()"
>
<GmapMarker
:key="index"
v-for="(m, index) in getMarkers()"
:position="m.position"
:clickable="true"
:draggable="false"
:title="m.title"
:icon="m.icon"
@click="clicked()"
/>
</GmapMap>
</div>
</template>
<script>
import MarkerClusterer from "marker-clusterer-plus";
export default {
name: "SiteMap",
props: [],
methods: {
getOptions() {
return {
zoomControl: true,
mapTypeControl: false,
scaleControl: false,
streetViewControl: false,
rotateControl: false,
fullscreenControl: false,
disableDefaultUi: false
};
},
clicked() {
// after click
},
getCenter() {
return {
lat: 37.12523,
lng: -122.1252
};
},
getMarkers() {
// generating markers for site map
var markers = [];
// remove this after lat long received from api.
const tempLatLong = [
{ lat: 37.9068361, lng: -122.116971 },
{ lat: 37.9168362, lng: -122.076972 },
{ lat: 37.9268363, lng: -122.136973 },
{ lat: 37.9368364, lng: -122.146974 },
{ lat: 37.9468365, lng: -122.106975 },
{ lat: 37.9568366, lng: -122.166976 },
{ lat: 37.9668367, lng: -122.176977 },
{ lat: 37.9768368, lng: -122.016978 },
{ lat: 37.9868369, lng: -122.196979 }
];
for(let i=0;i<tempLatLong.length;i++){
markers.push({
position: tempLatLong[i],
title: 'test title',
icon: this.getSiteIcon(1) // if you want to show different as per the condition.
});
}
return markers;
},
getSiteIcon(status) {
try {
switch (status) {
case 1:
return require("@/assets/images/icons/map-marker-online.svg");
break;
case 2:
return require("@/assets/images/icons/map-marker-critical.svg");
break;
case 3:
return require("@/assets/images/icons/map-marker-offline.svg");
break;
case 4:
return require("@/assets/images/icons/map-marker-lifesafety.svg");
break;
default:
return require("@/assets/images/icons/map-marker-online.svg");
}
} catch (e) {
return null;
}
},
},
components: {},
created() {},
mounted() {
}
};
</script>
<style scoped></style>
从“marker clusterer plus”导入marker clusterer;
导出默认值{
名称:“网站地图”,
道具:[],
方法:{
getOptions(){
返回{
动物控制:对,
mapTypeControl:false,
scaleControl:false,
街景控制:错误,
旋转控制:错误,
全屏控制:错误,
disableDefaultUi:false
};
},
点击(){
//点击后
},
getCenter(){
返回{
纬度:37.12523,
液化天然气:-122.1252
};
},
getMarkers(){
//为站点地图生成标记
var标记=[];
//在从api收到lat long后删除此项。
const tempLatLong=[
{拉丁美洲:37.9068361,液化天然气:-122.116971},
{拉丁美洲:37.9168362,液化天然气:-122.076972},
{拉丁美洲:37.9268363,液化天然气:-122.136973},
{拉丁美洲:37.9368364,液化天然气:-122.146974},
{拉丁美洲:37.9468365,液化天然气:-122.106975},
{拉丁美洲:37.9568366,液化天然气:-122.166976},
{拉丁美洲:37.9668367,液化天然气:-122.176977},
{拉丁美洲:37.9768368,液化天然气:-122.016978},
{拉丁美洲:37.9868369,液化天然气:-122.196979}
];
对于(让我=0;它工作得很好,谢谢您的分享。我可以参考这段代码来更改标记图标>乐于帮助!!如果我的lat和lng从api检索,但是变量是“纬度”和“经度”而不是lat/lng,怎么样?我可以在哪里设置标记来读取“纬度”和“经度”,而不是lat/lngI未测试的但是你可以按照gmap规范修改数据。是的,我已经修改了代码,它成功地使用了API数据,我使用for循环检索所有API数据,并将纬度和经度存储到它的新变量中。再次感谢你的上述解释。
<template>
<div>
<GmapMap
ref="gmap"
:center="getCenter()"
:zoom="11"
style="height: 500px;margin: 0px -8px;"
class="no-padding"
:options="getOptions()"
>
<GmapMarker
:key="index"
v-for="(m, index) in getMarkers()"
:position="m.position"
:clickable="true"
:draggable="false"
:title="m.title"
:icon="m.icon"
@click="clicked()"
/>
</GmapMap>
</div>
</template>
<script>
import MarkerClusterer from "marker-clusterer-plus";
export default {
name: "SiteMap",
props: [],
methods: {
getOptions() {
return {
zoomControl: true,
mapTypeControl: false,
scaleControl: false,
streetViewControl: false,
rotateControl: false,
fullscreenControl: false,
disableDefaultUi: false
};
},
clicked() {
// after click
},
getCenter() {
return {
lat: 37.12523,
lng: -122.1252
};
},
getMarkers() {
// generating markers for site map
var markers = [];
// remove this after lat long received from api.
const tempLatLong = [
{ lat: 37.9068361, lng: -122.116971 },
{ lat: 37.9168362, lng: -122.076972 },
{ lat: 37.9268363, lng: -122.136973 },
{ lat: 37.9368364, lng: -122.146974 },
{ lat: 37.9468365, lng: -122.106975 },
{ lat: 37.9568366, lng: -122.166976 },
{ lat: 37.9668367, lng: -122.176977 },
{ lat: 37.9768368, lng: -122.016978 },
{ lat: 37.9868369, lng: -122.196979 }
];
for(let i=0;i<tempLatLong.length;i++){
markers.push({
position: tempLatLong[i],
title: 'test title',
icon: this.getSiteIcon(1) // if you want to show different as per the condition.
});
}
return markers;
},
getSiteIcon(status) {
try {
switch (status) {
case 1:
return require("@/assets/images/icons/map-marker-online.svg");
break;
case 2:
return require("@/assets/images/icons/map-marker-critical.svg");
break;
case 3:
return require("@/assets/images/icons/map-marker-offline.svg");
break;
case 4:
return require("@/assets/images/icons/map-marker-lifesafety.svg");
break;
default:
return require("@/assets/images/icons/map-marker-online.svg");
}
} catch (e) {
return null;
}
},
},
components: {},
created() {},
mounted() {
}
};
</script>
<style scoped></style>