admin管理员组文章数量:1290983
I'm a newbie in google map api. I'm trying to implement the google maps geocoding api.
Geocoding API on Google Developers
exports.FindByKeyWord = function (req, res, next) {
var API_KEY = "SOMEDATA";
var BASE_URL = "=";
var address = "1600 Amphitheatre Parkway, Mountain View, CA";
var url = BASE_URL + address + "&key=" + API_KEY;
var map = new google.maps.Map();
var geocoder = new google.maps.Geocoder();
geocoder.geocode(url, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
res.json(marker);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
};
I want to response json format, however my function throw an error
google is not define
Can anyone help?
I'm a newbie in google map api. I'm trying to implement the google maps geocoding api.
Geocoding API on Google Developers
exports.FindByKeyWord = function (req, res, next) {
var API_KEY = "SOMEDATA";
var BASE_URL = "https://maps.googleapis./maps/api/geocode/json?address=";
var address = "1600 Amphitheatre Parkway, Mountain View, CA";
var url = BASE_URL + address + "&key=" + API_KEY;
var map = new google.maps.Map();
var geocoder = new google.maps.Geocoder();
geocoder.geocode(url, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
res.json(marker);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
};
I want to response json format, however my function throw an error
google is not define
Can anyone help?
Share Improve this question edited May 3, 2016 at 10:33 Oscar 14k9 gold badges52 silver badges82 bronze badges asked May 3, 2016 at 10:31 LointLoint 3,9489 gold badges28 silver badges46 bronze badges 7- Is the google maps api script correctly imported? – Koen Commented May 3, 2016 at 10:34
-
1
I think you're getting confused between the Geocoding API and the client-side JavaScript API. You're using the latter here, but that's designed to be run in a browser, not on a server, hence why you're getting an error. You need to make REST calls to the URL you've specified in
url
instead. Basically everything fromvar map = new google.maps.Map();
onwards in your code will not work in Node. – Joe Clay Commented May 3, 2016 at 10:34 - You should import google library at the beginwitha require('google'); or similar statement. – Oscar Commented May 3, 2016 at 10:35
-
1
@Oscar: There is no official Google Maps Node.js library, they won't be able to require it. The API he's trying to use is designed to be embedded with a
<script>
tag. – Joe Clay Commented May 3, 2016 at 10:37 - @JoeClay I'm implementing server-side. I mean, how to have a response json data format to a text string. – Loint Commented May 3, 2016 at 11:19
1 Answer
Reset to default 10I think you're getting confused between the Geocoding REST API and the client-side JavaScript API. You're using the latter here, but that's designed to be run in a browser, not on a server, hence why you're getting an error.
Using the REST API is pretty simple in this case - all you have to do is make a HTTP request to the URL you've already created in your example code, and then pass the result through to your server's response. I'd remend using a library like Request or SuperAgent to simplify this.
Here's an (untested) example, using Request:
// npm install request --save
var request = require("request");
exports.FindByKeyWord = function (req, res, next) {
var API_KEY = "SOMEDATA";
var BASE_URL = "https://maps.googleapis./maps/api/geocode/json?address=";
var address = "1600 Amphitheatre Parkway, Mountain View, CA";
var url = BASE_URL + address + "&key=" + API_KEY;
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
res.json(body);
}
else {
// The request failed, handle it
}
});
};
Effectively, your server acts as a middleman between your users and the Google API - this can be really handy, as it means you can modify the request before it gets sent off to be geocoded, and allows you to do stuff like caching results (you only get 2,500 free requests a day on the REST API, so this is very important if you expect non-trivial amounts of traffic!).
本文标签: nodejsHow to implement geocoding responses using nodejsjavascriptStack Overflow
版权声明:本文标题:node.js - How to implement geocoding responses using nodejsjavascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741513927a2382771.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论