1

Display distance to restaraunt in results

This commit is contained in:
Alex Yatskov 2014-11-17 16:29:02 +09:00
parent c7de890a8f
commit 51cafdf034
3 changed files with 32 additions and 16 deletions

View File

@ -83,6 +83,7 @@
<tr> <tr>
<th>Id</th> <th>Id</th>
<th>Name</th> <th>Name</th>
<th>Distance</th>
<th>Score</th> <th>Score</th>
</tr> </tr>
</thead> </thead>
@ -90,6 +91,7 @@
<tr> <tr>
<td>{{id}}</td> <td>{{id}}</td>
<td><a href="{{url}}">{{name}}</a></td> <td><a href="{{url}}">{{name}}</a></td>
<th>{{distance}} km</th>
<td>{{score}}</td> <td>{{score}}</td>
</tr> </tr>
{{/each}} {{/each}}

View File

@ -137,8 +137,10 @@
}; };
if (_ctx.geo) { if (_ctx.geo) {
_ctx.query.latitude = _ctx.geo.coords.latitude; _ctx.query.geo = {
_ctx.query.longitude = _ctx.geo.coords.longitude; latitude: _ctx.geo.coords.latitude,
longitude: _ctx.geo.coords.longitude
};
} }
if (!_.has(_ctx, 'grapher')) { if (!_.has(_ctx, 'grapher')) {
@ -231,7 +233,7 @@
var template = Handlebars.compile($('#template').html()); var template = Handlebars.compile($('#template').html());
$('#results').empty(); $('#results').empty();
$('#results').append(template({'results': results})); $('#results').append(template({results: results}));
} }
$(document).on({ $(document).on({
@ -246,7 +248,8 @@
ready: function() { ready: function() {
navigator.geolocation.getCurrentPosition( navigator.geolocation.getCurrentPosition(
function(geo) { onReady(geo); }, function(geo) { onReady(geo); },
function(err) { onReady(null); } function(err) { onReady(null); },
{enableHighAccuracy: true}
); );
} }
}); });

View File

@ -64,14 +64,20 @@ function countRecords(data, features, minScore) {
return count; return count;
} }
function findRecords(data, features, minScore) { function findRecords(data, geo, features, minScore) {
var results = []; var results = [];
walkMatches(data, features, minScore, function(record, score) { walkMatches(data, features, minScore, function(record, score) {
var distance = 0.0;
if (geo !== null) {
distance = geolib.getDistance(record.geo, geo) / 1000.0;
}
results.push({ results.push({
name: record.name, name: record.name,
url: 'http://www.tripadvisor.com' + record.relativeUrl, url: 'http://www.tripadvisor.com' + record.relativeUrl,
score: score, score: score,
id: record.id distance: distance,
id: record.id
}); });
}); });
@ -196,12 +202,16 @@ function getRecords(callback) {
name: row.name, name: row.name,
id: row.id, id: row.id,
relativeUrl: row.url, relativeUrl: row.url,
rating: { geo: {
latitude: row.latitude,
longitude: row.longitude
},
rating: {
food: row.food, food: row.food,
service: row.service, service: row.service,
value: row.value, value: row.value,
atmosphere: row.atmosphere atmosphere: row.atmosphere
} },
}; };
}); });
@ -233,6 +243,7 @@ function execQuery(query, callback) {
getData(function(data) { getData(function(data) {
var searchResults = findRecords( var searchResults = findRecords(
data, data,
query.geo,
query.features, query.features,
query.minScore query.minScore
); );
@ -264,9 +275,9 @@ function execQuery(query, callback) {
} }
module.exports = { module.exports = {
'loadDb': loadDb, loadDb: loadDb,
'addKeyword': addKeyword, addKeyword: addKeyword,
'removeKeyword': removeKeyword, removeKeyword: removeKeyword,
'getParameters': getParameters, getParameters: getParameters,
'execQuery': execQuery execQuery: execQuery
}; };