【问题标题】:Adding multiple markers in ionic google map在离子谷歌地图中添加多个标记
【发布时间】:2020-01-19 00:14:00
【问题描述】:

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

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

}

 }

我想使用类似于下面参考图像的纬度和经度值在地图上添加不同的位置标记

请帮助我如何做到这一点。谢谢你

【问题讨论】:

    标签: google-maps ionic4


    【解决方案1】:

    在一个项目上我需要做同样的事情,所以我做了这个功能。 因此,您只需在要打印的列表中放置一个 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");
    }
    

    }

    使用循环,您应该能够使用上述函数正确创建标记:

    this.markers.forEach(element => {
    
      this.infomarkers.forEach(info => {
    
        if (element.id == info.id) {
    
          this.createMarker(element.position, element.label, info, element.icon);
    
        }
    
      });
    
    });
    

    【讨论】:

    • 嗨@jhervera,感谢您的回答,我应该在代码中的哪里放置不同的纬度和经度值以显示多个标记。我是离子和角度的新手,请您在我的代码中进行编辑。谢谢
    • 嗨@Saif,在这种情况下,我有一个提供者,其中包含一些类似的对象:{ position: { lat: lat , lng: lng };如果您将它与我的代码一起使用,它应该可以正常工作。我将编辑代码并举个例子。
    • 嗨@jhervera,我尝试在我的项目中插入你的代码,但没有得到输出。您能否以我是这个 ionic 和 angualr 的新手的身份在我的完整代码中进行编辑。谢谢
    【解决方案2】:

    你可以像这样定义一些标记:

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

    有关更多信息,请参阅此处的教程:https://medium.com/free-code-camp/how-to-change-javascript-google-map-marker-color-8a72131d1207

    【讨论】:

    • 嗨@yaennuuH,谢谢你的回答,在你的参考中,它使用的是javascript,在我的例子中,我的离子项目使用了Angular。在在线搜索以添加多个标记后,我更新了问题中的代码,但它不起作用并且也没有显示任何错误。请看看并帮助我。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 2018-02-16
    • 2013-04-21
    • 2011-09-11
    • 2021-09-12
    • 1970-01-01
    • 2015-09-11
    相关资源
    最近更新 更多