如何在Angular单页应用中的两个不同页面中显示两个地图。

huangapple go评论67阅读模式
英文:

How to show two maps in two different pages in an Angular SPA

问题

I am trying to insert two Google Maps in two different pages in a single page application in Angular. When I open the first page I see the map correctly, when I go to the second the map is not visible. Same thing on the contrary, if I refresh the site and open the second page first I see the map correctly, if I then go to the first page I don't see the map. It is as if it only initializes the first map that is displayed and the second one does not.

Do you know how I can do to see two different maps on two different pages?

This is the code, in the index.html I put the google script at the bottom of the body.

<script src="https://maps.googleapis.com/maps/api/js?key=[KEY]=weekly" defer ></script>

The first component ts:

export class ExploreComponent implements OnInit, AfterViewInit {
  ngAfterViewInit(): void {
    this.initMap();
  }
  
  map?: google.maps.Map;
  infoWindow?: google.maps.InfoWindow;
  initMap() {
    const myLatLng = { lat: 45.902780, lng: 16.496370 };
    
    this.map = new google.maps.Map(
      document.getElementById("map") as HTMLElement,
      {
        zoom: 6,
        center: myLatLng,
        // disableDefaultUI:true,
        streetViewControl: false,
        fullscreenControl: false
      }
    );
    
    const locationButton = document.createElement("button");
    
    locationButton.textContent = "Pan to Current Location";
    locationButton.classList.add("custom-map-control-button");
    
    this.map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(locationButton);
    
    locationButton.addEventListener("click", () => {
      this.getActualPosition();
    });
    
    this.getActualPosition();
  };
}

The HTML of the first component:

<div id="map" style="width: 100%; height: 95%;"></div>

The second component ts is structured practically the same:

ngAfterViewInit(): void {
  this.initMap();
}
  
map?: google.maps.Map;
initMap() {
  const myLatLng = { lat: 41.902780, lng: 12.496370 };
    
  this.map = new google.maps.Map(
    document.getElementById("map") as HTMLElement,
    {
      zoom: 6,
      center: myLatLng,
    }
  );
}

HTML of the second component:

<div id="map" style="width: 100%; height: 250px; margin: auto; border-radius: 5px; margin-top: 5px;"></div>

Please note that you have used the same HTML id="map" for both maps. You should use unique IDs for each map element to display two different maps on two different pages.

英文:

I am trying to insert two Google Maps in two different pages in a single page application in Angular. When I open the first page I see the map correctly, when I go to the second the map is not visible. Same thing on the contrary, if I refresh the site and open the second page first I see the map correctly, if I then go to the first page I don't see the map. It is as if it only initializes the first map that is displayed and the second one does not.

Do you know how I can do to see two different maps on two different pages?

This is the code, in the index.html I put the google script at the bottom of the body.

&lt;script src=&quot;https://maps.googleapis.com/maps/api/js?key=[KEY]=weekly&quot; defer &gt;

the first component ts:

export class ExploreComponent implements OnInit, AfterViewInit {
ngAfterViewInit(): void {
this.initMap();
}

map?: google.maps.Map;
infoWindow?: google.maps.InfoWindow;
initMap() {
const myLatLng = { lat: 45.902780, lng: 16.496370 };

    this.map = new google.maps.Map(
      document.getElementById(&quot;map&quot;) as HTMLElement,
      {
        zoom: 6,
        center: myLatLng,
        // disableDefaultUI:true,
        streetViewControl: false,
        fullscreenControl: false
      }
    );
    
    const locationButton = document.createElement(&quot;button&quot;);
    
     locationButton.textContent = &quot;Pan to Current Location&quot;;
     locationButton.classList.add(&quot;custom-map-control-button&quot;);
    
    this.map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(locationButton);
    
    locationButton.addEventListener(&quot;click&quot;, () =&gt; {
      this.getActualPosition();
    });
    
    this.getActualPosition();

};
}

The HTML of the first component:

&lt;div id=&quot;map&quot; style=&quot;width: 100%; height: 95%;&quot;&gt;&lt;/div&gt;

The second component ts is structured practically the same:

ngAfterViewInit(): void {
this.initMap();
}

map?: google.maps.Map;
initMap() {
const myLatLng = { lat: 41.902780, lng: 12.496370 };

    this.map = new google.maps.Map(
      document.getElementById(&quot;map&quot;) as HTMLElement,
      {
        zoom: 6,
        center: myLatLng,
      }
    );

}

HTML of the second component:

&lt;div id=&quot;map&quot; style=&quot;width: 100%; height: 250px; margin: auto; border-radius: 5px;margin-top: 5px;&quot;\&gt;\&lt;/div&gt;

答案1

得分: 1

每当我遇到这个问题时,通常是因为两个地图div同时存在于DOM中。如果一个地图位于另一个子组件中,或者如果其中一个地图是隐藏的而不是不存在(例如,如果您使用[hidden]而不是*ngIf),那么就会出现这种情况。

我建议尝试更改第二个地图的ID,将其设置为其他值,然后当然要记得更新第二个地图的构造函数以使用新的ID。

英文:

Whenever I run into this, it's because both map divs are present in the DOM at the same time. This would be the case if one map was in a sub-component of another, or if one of the maps was hidden instead of not present (e.g. if you use [hidden] instead of *ngIf).

I would try changing the ID of the second map, to be something else, then of course remember to update your second map's constructor to use the new ID.

huangapple
  • 本文由 发表于 2023年5月14日 16:47:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定