请协助将foursquare的场地API的结果上传到新的Mongo系列中。以下是我到目前为止:
<head>
<title>Places from Foursquare</title>
</head>
<body>
<script src='https://code.jquery.com/jquery-1.11.0.min.js'></script>
<script>
navigator.geolocation.getCurrentPosition(function(data) {
var lat = data['coords']['latitude'];
var lng = data['coords']['longitude'];
var CLIENT_ID = 'MyClientID';
var CLIENT_SECRET = 'MyClientSecret';
var API_ENDPOINT = 'https://api.foursquare.com/v2/venues/search' +
'?client_id=CLIENT_ID' +
'&client_secret=CLIENT_SECRET' +
'&v=20130815' +
'&ll=' + lat + ',' + lng +
'&query=coffee' +
'&callback=?';
Venues = new Mongo.Collection("venues");
Meteor.methods({
'fetchNearbyLocations': function(coords) {
if (Meteor.isServer) {
Venues.upsert(;
});
}
}
});
</script>
</body>
另外,我是否需要在任何位置添加API OAuth才能使查询生效?我应该使用.getJSON之类的东西(config.apiUrl +来查询API吗?
谢谢。
答案 0 :(得分:2)
您可以使用oleo:foresquare Pacakge
做这样的事情。
首次安装
meteor add oleh:foursquare
第二次提交凭据
//server/secret.js
Foursquare.init({
id: 'xxxxxxxxxxxxxxxxxxxxxxx',
secret: 'xxxxxxxxxxxxxxxxxxxxxxx',
authOnly: false // need auth for using or no?
})
三是进行场地查询。
<template name="example">
<input type="text" id="venueQuery">
<br>
{{#each venus}}
{{result}}
{{/each}}
</template>
现在是JS。
Venus = new Mongo.Collection(null) // client side collection to store the venus
Template.example.events({
'keypress #venueQuery':function(event,template){
if(event.keyCode === 13){
params = { //query to the params,
ll:"35.68949, 139.69171", //Your location use the geo result here
query:template.$('#venueQuery').val(),
limit:10, //the limit of the query
}
//Now the Find.
Foursquare.find(params, function(error, result) {
if(!error){ //if no error
if(result.response.venues.length === 0){ // if the query cant find anything
console.log("nothing find");
}else{
queryResult = result.response.venues //Taking the venues array.
queryResult.forEach(function(venues,index){
street = venues.location.formattedAddress
lat = venues.location.lat
lng = venues.location.lng
city = venues.location.city
venueName = venues.name;
var markerData = {
lat : lat,
lng : lng,
venueName :venueName,
query : params.query,
street : street,
city : city,
}
Venues.insert(markerData) //Inserting into the client side collection
});
}
}
});
}
}
})
源代码DEMO或Online DEMO