javascript - 如何在 Node 中包含谷歌地图API?

标签 javascript node.js google-maps

我想通过 Node 访问谷歌地图 API,因为谷歌的最佳安全实践表明 API key 应该隐藏。

但是,所有 api 使用示例都使用

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

像这样访问google api

var directionsService = new google.maps.DirectionsService();

我想通过 Node 进行与上面类似的调用。如何将 google map api 包含在我的 Node 项目中?我知道有一个 npm googlemaps 包,它包装了 google map api,但它不如原始的 google map api 灵活。如何在 Node 中包含外部 src 文件?

最佳答案

这个Github将向您展示如何使用适用于 Node.js 的 Google Maps API

这是有关如何使用它的示例代码。

var publicConfig = {
key: '<YOUR-KEY>',
stagger_time: 1000, // for elevationPath
encode_polylines: false,
secure: true, // use https
proxy: 'http://127.0.0.1:9999' // optional, set a proxy for HTTP requests
};
var gmAPI = new GoogleMapsAPI(publicConfig);

// or in case you are using Google Maps for Work
var enterpriseConfig = {
google_client_id: '<YOUR-CLIENT-ID>', // to use Google Maps for Work
google_private_key: '<YOUR-PRIVATE-KEY>', // to use Google Maps for Work
google_channel: '<YOUR-CHANNEL>' // to use Google Maps for Work application usage tracking
stagger_time: 1000, // for elevationPath
encode_polylines: false,
secure: true, // use https
proxy: 'http://127.0.0.1:9999' // optional, set a proxy for HTTP requests
};
var gmAPI = new GoogleMapsAPI(enterpriseConfig);

// geocode API
var geocodeParams = {
"address": "121, Curtain Road, EC2A 3AD, London UK",
"components": "components=country:GB",
"bounds": "55,-1|54,1",
"language": "en",
"region": "uk"
};

gmAPI.geocode(geocodeParams, function(err, result){
console.log(result);
});

// reverse geocode API
var reverseGeocodeParams = {
"latlng": "51.1245,-0.0523",
"result_type": "postal_code",
"language": "en",
"location_type": "APPROXIMATE"
};

gmAPI.reverseGeocode(reverseGeocodeParams, function(err, result){
console.log(result);
});

它还展示了如何在 Statics Maps、Streetview 和其他 Google Maps API Web 服务中使用 Node.js。

对于node中的外部scr文件,我认为这个SO question可以帮助您。

关于javascript - 如何在 Node 中包含谷歌地图API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39067225/

相关文章:

google-maps - 加载任意地址的简单地理编码示例

android - 从 Google Play 服务加载 MapFragment 时出现致命信号 11

ruby - 当我尝试调用 Google Geocode API 时出现 SSL 错误

json - Node.js:以数组形式返回 JSON 子属性

javascript - Angular 2 - 如何在 addEventListener 处理函数中调用 typescript 函数?

javascript - Spring maven ,访问 CSS/JS 文件

javascript - React - 单击按钮转到视口(viewport)顶部

node.js - 使用基本身份验证重定向到另一个网站(在 Node 中)

javascript - 如何将passport js配置放在一个公共(public)文件中

javascript - 在哪一点上获取 View 所依赖的主干模型?