Angular 如何在javascript谷歌地图上添加多个标记?
您好,我正在尝试在我的应用程序上添加标记,但仍然无法在地图上查看标记,以下是我的代码: TS:Angular 如何在javascript谷歌地图上添加多个标记?,angular,google-maps,google-maps-markers,Angular,Google Maps,Google Maps Markers,您好,我正在尝试在我的应用程序上添加标记,但仍然无法在地图上查看标记,以下是我的代码: TS: loadMap(){ 变量位置=[ [Bondi Beach',-33.890542151.274856,4], [Coogee Beach',-33.923036151.259052,5], [Cronulla Beach',-34.028249151.157507,3], [‘曼利海滩’,-33.80010128657071151.28747820854187,2], [‘马鲁布拉海滩’,-33.
loadMap(){
变量位置=[
[Bondi Beach',-33.890542151.274856,4],
[Coogee Beach',-33.923036151.259052,5],
[Cronulla Beach',-34.028249151.157507,3],
[‘曼利海滩’,-33.80010128657071151.28747820854187,2],
[‘马鲁布拉海滩’,-33.950198151.259302,1]
];
//警报(“hi”);
//让latLng=newgoogle.maps.latLng(13.08784880.27847);
让mapOptions={
中心:新谷歌地图LatLng(-33.92151.25),
缩放:10,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
var infowindow=new google.maps.infowindow();
var标记,i;
对于(i=0;i
如何在谷歌地图上查看标记?有什么帮助吗?请参阅评论
函数加载映射(){
变量位置=[
[Bondi Beach',-33.890542151.274856,4],
[Coogee Beach',-33.923036151.259052,5],
[Cronulla Beach',-34.028249151.157507,3],
[‘曼利海滩’,-33.80010128657071151.28747820854187,2],
[‘马鲁布拉海滩’,-33.950198151.259302,1]
];
让mapOptions={
中心:新谷歌地图LatLng(-33.92151.25),
缩放:10,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
//把这条线移到这里
this.map=new google.maps.map(document.getElementById('map'),mapOptions);//为了本例,将“原生元素”更改为标准DOM元素
var infowindow=new google.maps.infowindow();
var标记,i;
对于(i=0;i
#地图{
高度:180像素;
}
您试图在创建此.map之前使用它。查看Javascript控制台并弄明白这一点有多难?我看不出您在地图中实际添加了标记。我希望调用marker.setMap(map)@除了我在之前的评论中提到的以外,代码看起来是正确的。无需使用setMap
@MrUpsidown我声明它为Map,但仍然无法获取Markers@guru,是的,你有,但是在你使用它之后。在创建标记时,您使用的是map:this.map
,但之后您声明的是this.map=new google.maps.map(…)
。@MrUpsidedown谢谢,成功了。
loadMap(){
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]
];
// alert('hi');
// let latLng = new google.maps.LatLng(13.08784, 80.27847);
let mapOptions = {
center: new google.maps.LatLng(-33.92, 151.25),
zoom: 10,
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: this.map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(Map, marker);
}
})(marker, i));
this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);
}
}