【发布时间】:2019-04-07 01:00:05
【问题描述】:
我正在尝试使用 Google 地图距离矩阵服务 (https://developers.google.com/maps/documentation/javascript/distancematrix)。
我把这行放在我的public/index.html:<script src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY"></script>
在 react 的 js 文件中,我可以打印 window.google 并查看全局变量。我还可以看到它有一个函数window.google.maps.DistanceMatrixService() 来创建服务。但是,当我使用var service = window.google.maps.DistanceMatrixService() 创建该服务时,service 是undefined。知道为什么吗?
【问题讨论】:
-
尝试
var service = new window.google.maps.DistanceMatrixService()(不同之处在于new这个词)根据您链接到的文档 - 您也不需要window.
标签: javascript reactjs google-maps-api-3