Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google maps 在谷歌地图中添加多个标记_Google Maps_Ionic4 - Fatal编程技术网

Google maps 在谷歌地图中添加多个标记

Google maps 在谷歌地图中添加多个标记,google-maps,ionic4,Google Maps,Ionic4,我正在尝试将谷歌地图集成到爱奥尼亚项目中,并成功地在爱奥尼亚页面上显示谷歌地图。但是我想在谷歌地图上显示多个标记。尝试了不同的代码,但没有得到这个多标记的东西。 下面是我的代码: html: .ts文件: import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core'; declare var google; @Component({ selector: 'app-home',

我正在尝试将谷歌地图集成到爱奥尼亚项目中,并成功地在爱奥尼亚页面上显示谷歌地图。但是我想在谷歌地图上显示多个标记。尝试了不同的代码,但没有得到这个多标记的东西。 下面是我的代码:

html:


.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));
}

}

 }
从'@angular/core'导入{AfterViewInit,Component,ElementRef,OnInit,ViewChild};
谷歌公司;
@组成部分({
选择器:“应用程序主页”,
templateUrl:'home.page.html',
样式URL:['home.page.scss'],
})
导出类主页实现OnInit、AfterViewInit{
@ViewChild('mapElement',{static:true})mapNativeElement:ElementRef;
构造函数(){}
恩戈尼尼特(){
}
ngAfterViewInit():void{
变量位置=[
[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]
];
var map=new google.maps.map(document.getElementById('map'){
缩放:10,
中心:新谷歌地图LatLng(-33.92151.25),
mapTypeId:google.maps.mapTypeId.ROADMAP
});
var infowindow=new google.maps.infowindow();
var标记,i;
对于(i=0;i
我想使用类似于下面参考图像的纬度和经度值在地图上添加不同的位置标记


请帮我怎么做。谢谢

您可以这样定义一些标记:

new google.maps.Marker({
  map: map,
  position: {lat: -34.397, lng: 150.644},
  icon: {
    url: "http://maps.google.com/mapfiles/ms/icons/blue-dot.png"
  }
});

有关更多信息,请参见此处的教程:

在一个项目中,我需要执行相同的操作,因此我创建了此函数。 因此,您只需将for/foreach与要打印的列表放在一起,就可以了

希望这对你有帮助

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");
}
}

使用loop,您应该能够使用上述功能正确创建标记:

this.markers.forEach(element => {

  this.infomarkers.forEach(info => {

    if (element.id == info.id) {

      this.createMarker(element.position, element.label, info, element.icon);

    }

  });

});

您好@yaennuh,谢谢您的回答,在您的参考资料中,它使用javascript,在我的案例中,在我的ionic项目中使用Angular。在在线搜索后,我更新了问题中的代码,添加了多个标记,但它不起作用,也没有显示任何错误。请看一下并帮助我。谢谢你,谢谢你的回答,在显示多个标记的代码中,我应该在哪里放置不同的纬度和经度值。我是爱奥尼亚和安格的新手,你能在我的代码里编辑一下吗。谢谢你hi@Saif,在这种情况下,我有一个提供程序,它有如下对象:{position:{lat:lat,lng:lng};如果您将其与我的代码一起使用,这应该可以正常工作。我将编辑代码并给出一个示例。您好@jservera,我尝试在我的项目中插入您的代码,但没有得到输出。作为我是这个爱奥尼亚和安格尔的新手,您能在我的完整代码中进行编辑吗?谢谢
this.markers.forEach(element => {

  this.infomarkers.forEach(info => {

    if (element.id == info.id) {

      this.createMarker(element.position, element.label, info, element.icon);

    }

  });

});