与其依赖第三方库/模块来实现地图,不如直接加载脚本以便在marker icons 上使用谷歌地图的官方文档会更容易更好。为此,只需在 index.html 中加载 JS 地图脚本即可。
这是一个工作示例供您参考:https://stackblitz.com/edit/angular-map-custom-icons-64402069
index.html
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>
<my-app>loading</my-app>
style.css
html, body {
height: 100%;
margin: 0;
padding: 0;
}
.map {
height:100%;
}
map-with-marker.component.html
<div #map class="map"></div>
map-with-marker.component.ts
import { Component, OnInit, ViewChild } from "@angular/core";
declare const google: any;
@Component({
selector: "my-maps",
templateUrl: "./map-with-marker.component.html",
styleUrls: ["./map-with-marker.component.css"]
})
export class MapComponent implements OnInit {
@ViewChild("map", { static: true }) mapElement: any;
map: any;
image =
"https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png";
constructor() {}
ngOnInit() {
var center = { lat: -33.8569, lng: 151.2152 };
const mapProperties = {
center: center,
zoom: 11,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
this.map = new google.maps.Map(
this.mapElement.nativeElement,
mapProperties
);
var marker = new google.maps.Marker({
position: center,
map: this.map,
icon: this.image
});
//adding markers by click
this.map.addListener("click", e => {
//Determine the location where the user has clicked.
this.addMarker(e.latLng);
});
}
addMarker(latLng) {
var marker = new google.maps.Marker({
position: latLng,
map: this.map,
icon: this.image
});
}
}