本文介绍了如何将Google Maps API集成到Angular 2组件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  import {}我有一个Angular 2组件在文件comp.ts中以这种方式定义:组件}来自'angular2 / core'; 

@component({
选择器:'my-comp',
templateUrl:'comp.html'
})

导出class MyComp {
constructor(){
}
}

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

 < !DOCTYPE html> 
< html>
< head>
< script src =http://maps.googleapis.com/maps/api/js>< / script>
< script>
函数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 =googleMapstyle =width:500px; height:380px;>< / div>
< / body>

< / html>

此代码已从此链接复制。问题是组件不显示地图。我做错了什么?

解决方案

是的,你可以这样做。但是在打字稿编译器中会出现错误,所以不要忘记隐式声明谷歌变量。

  declare var google:any ; 

在组件导入后添加此指令。

 从'@ angular / core'导入{Component,OnInit}; 
declare var google:any;

@Component({
moduleId:module.id,
selector:'app-root',
templateUrl:'app.component.html',
styleUrls:['app.component.css']
})
export class AppComponent实现OnInit {
ngOnInit(){
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(gmap),mapProp);
}
}


I have an Angular 2 component that is defined in the file comp.ts in this way like this:

import {Component} from 'angular2/core';

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

export class MyComp{
    constructor(){
    }
}

Because I want the component to show a google map, I have inserted in the file comp.html the following code:

<!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>

This code has been copied from this link http://www.w3schools.com/googleAPI/google_maps_basic.asp. The problem is that the component doesn't show the map. What have I done wrong?

解决方案

Yeah, you could do so. But there would be an error in typescript compiler, so don't forget to implicitly declare google variable.

declare var google: any;

add this directive right after component import.

import { Component, OnInit } from '@angular/core';
declare var google: any;

@Component({
  moduleId: module.id,
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css']
})
export class AppComponent implements OnInit {
  ngOnInit() {
    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("gmap"), mapProp);
  }
}

这篇关于如何将Google Maps API集成到Angular 2组件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 03:16