【问题标题】:Angular 2 Google Map with Multiple Markers具有多个标记的 Angular 2 Google 地图
【发布时间】:2019-02-15 17:52:38
【问题描述】:

我目前正在开发一个使用 Google 地图的 Angular2 项目。

我正在尝试在该区域周围显示标记和多个标记。 我的地图工作正常,但似乎无法标记。

<div class="google-maps"></div>



constructor(private _elementRef:ElementRef) {
  }
  ngAfterViewInit() {
    let el = this._elementRef.nativeElement.querySelector('.google-maps');
    GoogleMapsLoader.load((google) => {
      let myLatlng = new google.maps.LatLng(44.5403,-78.5463);
      new google.maps.Map(el, {
        center: new google.maps.LatLng(44.5403, -78.5463),
        zoom: 8,
        mapTypeId: google.maps.MapTypeId.ROADMAP

      });
      new google.maps.Marker(el,{ 
        draggable: true,
        animation: google.maps.Animation.DROP,
          position: myLatlng,
            title:"Hello World!"
          });
        }); 
  }

【问题讨论】:

标签: google-maps angular typescript


【解决方案1】:

试试这个代码,我的项目工作

谷歌官方文档here

import  GoogleMapsLoader  from 'google-maps';
import  { google } from 'google-maps';

ngAfterViewInit() {

let el = this._elementRef.nativeElement.querySelector('.contact-us');

GoogleMapsLoader.KEY = 'g4554645645645645645646';

GoogleMapsLoader.load((google) => {

let myLatlng = new google.maps.LatLng(23.5454, 90.8785);

let map =  new google.maps.Map(el, {

    center: new google.maps.LatLng(23.8787878, 90.87878),

    zoom: 18,

    mapTypeId: google.maps.MapTypeId.ROADMAP
  });

let marker =  new google.maps.Marker({ 
      draggable: true,
      animation: google.maps.Animation.DROP,
      position: myLatlng,
      map: map,
      title:"Hello World!"
    });

});

模板.html

 <div class="contact-us" style="position: relative; height: 470px;"></div>

【讨论】:

    【解决方案2】:

    标记功能似乎与官方文档here不匹配。

     let map = new google.maps.Map(el, {
            center: new google.maps.LatLng(44.5403, -78.5463),
            zoom: 8,
            mapTypeId: google.maps.MapTypeId.ROADMAP
    
          });
     let marker = new google.maps.Marker({ 
            draggable: true,
            animation: google.maps.Animation.DROP,
              position: myLatlng,
              map: map,//set map created here
                title:"Hello World!"
              });
    

    【讨论】:

    • 您可以在同一张地图上使用不同的positiontitle 值调用相同的new Marker。具有创建标记并在需要时调用的函数。
    • @suraj 嗨,您知道如何将 infowindow 添加到标记及其分配的内容吗? link
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    相关资源
    最近更新 更多