【问题标题】:How to include google maps api in node?如何在节点中包含谷歌地图 api?
【发布时间】:2016-08-21 17:57:02
【问题描述】:

我想通过节点访问 google maps api,因为 google 的最佳安全实践表明 api 密钥应该被隐藏。

但是,所有的 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();

我想拨打类似于上述电话,但通过节点。如何在我的节点项目中包含谷歌地图 API?我知道有一个 npm googlemaps 包,它包装了 google maps api,但它不如原始的 google maps api 灵活。如何在节点中包含外部 src 文件?

【问题讨论】:

标签: javascript node.js google-maps


【解决方案1】:

Github 将向您展示如何使用 Google Maps API for Node.js

这是一个关于如何使用它的示例代码。

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);
});

它还展示了如何在静态地图、街景和其他 Google Maps API Web 服务中使用 Node.js。

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

【讨论】:

    猜你喜欢
    • 2012-09-26
    • 1970-01-01
    • 2015-12-24
    • 2018-01-13
    • 2014-11-18
    • 2019-06-23
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多