在离子Google地图中添加多个标记 [英] Adding multiple markers in ionic google map
问题描述
我正在尝试将Google Map集成到ionic项目中,并成功在ionic页面上显示google map.但是我想在这个谷歌地图上显示多个标记.尝试了不同的代码,但没有得到这个多个标记的东西. 下面是我的代码:
I am trying to integrate google map in ionic project and got succeeded in displaying the google map on ionic page. But i want to display multiple markers on the this google map. tried different code but not getting this multiple markers thing. below is my code:
html:
<ion-content>
<div #mapElement class="map" >
</div>
.ts文件:
import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core';
declare var google;
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage implements OnInit, AfterViewInit{
@ViewChild('mapElement', { static: true }) mapNativeElement: ElementRef;
constructor() {}
ngOnInit() {
}
ngAfterViewInit(): void {
var locations = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
}
}
我想使用类似于下面参考图像的纬度和经度值在地图上添加不同的位置标记
i want to add diffent location markers on the map using latitude and longitude values similar to below reference image
请帮助我该怎么做.谢谢
please help me how can i do this. thank you
推荐答案
在一个项目上,我需要执行相同的操作,因此我执行了此功能. 因此,您只需要在要打印的列表中放置一个for/foreach即可.
On one project i needed to do the same so i made this function. So you only need to put a for/foreach with the list you want to print and thats all.
希望这对您有所帮助.
private createMarker(position, label, element, icon) {
if (this.map != undefined) {
let marker = new google.maps.Marker({
position: position,
map: this.map,
icon: {
url: icon, // url
scaledSize: new google.maps.Size(40, 40), // scaled size
}
});
google.maps.event.addListener(marker, 'click', function () {
var infowindow = new google.maps.InfoWindow({
content: "content"
});
infowindow.open(this.map, marker);
});
} else {
console.log("map is undefined");
}
}
使用循环,您应该可以使用上述功能正确创建标记:
with loop you should be able to create the markers correctly with the function above:
this.markers.forEach(element => {
this.infomarkers.forEach(info => {
if (element.id == info.id) {
this.createMarker(element.position, element.label, info, element.icon);
}
});
});
这篇关于在离子Google地图中添加多个标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!