英文:
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.
<script src="https://maps.googleapis.com/maps/api/js?key=[KEY]=weekly" defer >
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>
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论