angular - 如何在 Angular 4 中实现 Google Maps API (initMap)?

标签 angular google-maps

我尝试在 Angular 中集成 Google map 。

index.html

<script async defer src="https://maps.googleapis.com/maps/api/js?key=api_key&callback=initMap"></script>

example.component.html

...
<div id="map"></div>
...

example.component.ts

...
ngAfterViewInit() {
        function initMap() {
            var uluru = {lat: -25.363, lng: 131.044};
            var map = new google.maps.Map(document.getElementById('map'), {
                zoom: 4,
                center: uluru
            });
            var marker = new google.maps.Marker({
                position: uluru,
                map: map
            });
        }
    }

我仍然得到错误“initMap 不是一个函数”。

最佳答案

它中断是因为当 Google 的脚本试图调用它时 initMap 函数不可用。它也不像 Google 的脚本所期望的那样在全局范围内可用(它的范围仅限于该类)。

有一个名为 Angular Google Maps 的现有库其目的是将 Google map 与 Angular 应用程序结合使用。

关于angular - 如何在 Angular 4 中实现 Google Maps API (initMap)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45662180/

相关文章:

Angular2 - 将类添加到具有特定 id 的元素

http - 错误 : . map() 不是函数

Angular - 使用 DTO 处理 HTTP 响应

javascript - 将参数传递给 Angular 中 @Injectable 的构造函数?

android - 获取 Traffic Jam Google Maps API

angular - 双倍绑定(bind) Angular 数组长度的简单方法,其中对象的特定属性为 'true'?

google-maps - API 用于显示距 Google map View 中的长/纬度位置最近的照片(如 Panoramio)

java - 在 WEAR OS 上启动 map 需要 WRITE_SETTINGS 权限

javascript - 谷歌街景 : Marker size and distance

javascript - 使用 google maps api v3 自定义信息窗口框?