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

View File

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

View File

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