有没有办法从 node.js 访问 google.maps.LatLng?
Is there a way to access google.maps.LatLng from node.js?
当 html 页面的一部分时,我有这段代码 运行 没问题。触发搜索后,它会在传入的点的一定半径内找到感兴趣的地方。
(index.html)
...
<script src="https://apis.google.com/js/api.js" type="text/javascript"></script>
<script src="https://maps.googleapis.com/maps/api/js?key={myapikey}&libraries=places&callback=initMap"></script>
...
(script.js)
function googlePlaces(points) {
points = [
[2.627365, 49.215369],
[2.760591, 49.647163],
[2.952975, 50.057504],
[3.344742, 50.280862],
[3.768293, 50.451306],
[4.21659, 50.534029] // for sake of example
var i=0;
var placesOfInterest = [];
for (point of points){
var latLng = new google.maps.LatLng(point[1],point[0])
var request = {
location: latLng,
radius: '10000'
};
service = new google.maps.places.PlacesService(map);
service.nearbySearch(request,
function(results,status){
if (status == google.maps.places.PlacesServiceStatus.OK) {
placesOfInterest.push(results);
};
});
i++;
}
return placesOfInterest;
}
我现在想在 node.js 设置(没有 html)中使用此代码来 return placesOfInterest 作为 JSON。我已将所有相关代码移动到我的 API.
的 'controller.js' 文件中
但现在我得到错误 'google' 未定义。
我试过的
所以我尝试以这种方式导入 google 库 https://github.com/googlemaps/google-maps-services-js,添加一行
var googleMapsClient = require('@google/maps').createClient({
key: 'myAPIkey'
});
并将我的 google
引用更改为...
// service.nearbySearch(request,
googleMapsClient.nearbySearch(request,
//var latLng = new google.maps.LatLng(point[1],point[0])
var latLng = googleMapsClient.LatLng(point[1],point[0])
但我得到一个错误,提示 googleMapsClient.LatLng 不是一个函数。如何将 google 库直接导入我的 js 文件?
您尝试在一个地方混合 Google 地图服务的 Node.js 客户端和 Google 地图 JavaScript API v3 代码。请注意,用于 HTTP Web 服务的 NodeJs 客户端库是为服务器端代码设计的,通常不适用于客户端代码 Google Maps JavaScript API v3.
NodeJs 客户端库为
中描述的 LatLng 定义了自己的接口
https://googlemaps.github.io/google-maps-services-js/docs/LatLng.html
Interface: LatLng
A latitude, longitude pair. The API methods accept either:
a two-item array of [latitude, longitude];
a comma-separated string;
an object with 'lat', 'lng' properties; or
an object with 'latitude', 'longitude' properties.
因此,在您的 NodeJs 代码中,您可以执行以下操作
var latLng = [41.3848421,2.1841461];
var latLng = "41.3848421,2.1841461";
var latLng = {lat: 41.3848421, lng: 2.1841461};
var latLng = {latitude: 41.3848421, longitude: 2.1841461};
然后将此值传递给附近的搜索方法。
var googleMapsClient = require('@google/maps').createClient({
key: 'myAPIkey'
});
googleMapsClient.placesNearby({
location: latLng,
radius: 10000,
type: myType
}, function(err, response) {
....
});
希望对您有所帮助!
当 html 页面的一部分时,我有这段代码 运行 没问题。触发搜索后,它会在传入的点的一定半径内找到感兴趣的地方。
(index.html)
...
<script src="https://apis.google.com/js/api.js" type="text/javascript"></script>
<script src="https://maps.googleapis.com/maps/api/js?key={myapikey}&libraries=places&callback=initMap"></script>
...
(script.js)
function googlePlaces(points) {
points = [
[2.627365, 49.215369],
[2.760591, 49.647163],
[2.952975, 50.057504],
[3.344742, 50.280862],
[3.768293, 50.451306],
[4.21659, 50.534029] // for sake of example
var i=0;
var placesOfInterest = [];
for (point of points){
var latLng = new google.maps.LatLng(point[1],point[0])
var request = {
location: latLng,
radius: '10000'
};
service = new google.maps.places.PlacesService(map);
service.nearbySearch(request,
function(results,status){
if (status == google.maps.places.PlacesServiceStatus.OK) {
placesOfInterest.push(results);
};
});
i++;
}
return placesOfInterest;
}
我现在想在 node.js 设置(没有 html)中使用此代码来 return placesOfInterest 作为 JSON。我已将所有相关代码移动到我的 API.
的 'controller.js' 文件中但现在我得到错误 'google' 未定义。
我试过的
所以我尝试以这种方式导入 google 库 https://github.com/googlemaps/google-maps-services-js,添加一行
var googleMapsClient = require('@google/maps').createClient({
key: 'myAPIkey'
});
并将我的 google
引用更改为...
// service.nearbySearch(request,
googleMapsClient.nearbySearch(request,
//var latLng = new google.maps.LatLng(point[1],point[0])
var latLng = googleMapsClient.LatLng(point[1],point[0])
但我得到一个错误,提示 googleMapsClient.LatLng 不是一个函数。如何将 google 库直接导入我的 js 文件?
您尝试在一个地方混合 Google 地图服务的 Node.js 客户端和 Google 地图 JavaScript API v3 代码。请注意,用于 HTTP Web 服务的 NodeJs 客户端库是为服务器端代码设计的,通常不适用于客户端代码 Google Maps JavaScript API v3.
NodeJs 客户端库为
中描述的 LatLng 定义了自己的接口https://googlemaps.github.io/google-maps-services-js/docs/LatLng.html
Interface: LatLng
A latitude, longitude pair. The API methods accept either:
a two-item array of [latitude, longitude];
a comma-separated string;
an object with 'lat', 'lng' properties; or
an object with 'latitude', 'longitude' properties.
因此,在您的 NodeJs 代码中,您可以执行以下操作
var latLng = [41.3848421,2.1841461];
var latLng = "41.3848421,2.1841461";
var latLng = {lat: 41.3848421, lng: 2.1841461};
var latLng = {latitude: 41.3848421, longitude: 2.1841461};
然后将此值传递给附近的搜索方法。
var googleMapsClient = require('@google/maps').createClient({
key: 'myAPIkey'
});
googleMapsClient.placesNearby({
location: latLng,
radius: 10000,
type: myType
}, function(err, response) {
....
});
希望对您有所帮助!