相关疑难解决方法(0)

如何在Angular 2组件中集成Google Maps API

我有一个Angular 2组件,在文件comp.ts中以这种方式定义,如下所示:

import {Component} from 'angular2/core';

@component({
    selector:'my-comp',
    templateUrl:'comp.html'
})

export class MyComp{
    constructor(){
    }
}
Run Code Online (Sandbox Code Playgroud)

因为我希望组件显示谷歌地图,我已在comp.html文件中插入以下代码:

<!DOCTYPE html>
<html>
<head>
<script src="http://maps.googleapis.com/maps/api/js"></script>
<script>
function initialize() {
  var mapProp = {
    center:new google.maps.LatLng(51.508742,-0.120850),
    zoom:5,
    mapTypeId:google.maps.MapTypeId.ROADMAP
  };
  var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>

<body>
<div id="googleMap" style="width:500px;height:380px;"></div>
</body>

</html> 
Run Code Online (Sandbox Code Playgroud)

此代码已从此链接http://www.w3schools.com/googleAPI/google_maps_basic.asp中复制.问题是组件不显示地图.我做错了什么?

google-maps angular2-template angular

20
推荐指数
2
解决办法
4万
查看次数

标签 统计

angular ×1

angular2-template ×1

google-maps ×1