Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
Merge pull request #15 from pelias/geobias-toggle
Browse files Browse the repository at this point in the history
Geobias toggle
  • Loading branch information
hkrishna committed Feb 3, 2015
2 parents 162eb3f + 59517d0 commit 0597a3e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 27 deletions.
19 changes: 18 additions & 1 deletion css/demo.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ body {
-webkit-transition: none !important;
}

.aside .fa-location-arrow {
position: absolute;
top: 12px;
right: 5px;
font-size: 20px;
color: lightgray;
}

.aside.geobias-on .fa-location-arrow {
color:#428bca;
}

.aside.geobias-off .fa-location-arrow {
color:lightgray;
}

input[type="text"] {
padding: 10px;
border: solid 1px #dcdcdc;
Expand Down Expand Up @@ -93,7 +109,8 @@ ul.results li a {
.label-searchType {
position: absolute;
top: 10px;
right: 5px;
right: 25px;
text-transform: uppercase;
}

a {
Expand Down
9 changes: 6 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
<form role="search">
<div class="input">
<input type="text" data-ng-model="search" class="form-control" ng-focus="onFocus($event)" ng-blur="onBlur($event)" ng-keydown="keyPressed($event)" placeholder="Search" />
<div class="aside">
<div class="aside geobias-{{geobias}}">
<a data-ng-click="switchGeobias(geobias)">
<span class="fa fa-location-arrow"></span>
</a>
<a data-ng-click="switchType(searchType)">
<span class="label label-default label-searchType">{{searchType}}</span>
</a>
Expand All @@ -44,7 +47,7 @@
<ul id="suggestresults" class="results">
<li data-ng-repeat="result in suggestresults">
<a data-ng-click="selectResult(result, true)">
<i class="glyphicon glyphicon-{{result.icon}}"></i>
<i title="{{result.type}}" class="glyphicon glyphicon-{{result.icon}}"></i>
<span data-ng-bind-html="result.htmltext"></span>
<div class="aside">
<em class="label label-info">{{result.distance}}km</em>
Expand All @@ -56,7 +59,7 @@
<ul id="searchresults" class="results">
<li data-ng-repeat="result in searchresults">
<a data-ng-click="selectResult(result)">
<i class="glyphicon glyphicon-{{result.icon}}"></i>
<i title="{{result.type}}" class="glyphicon glyphicon-{{result.icon}}"></i>
<span data-ng-bind-html="result.htmltext"></span>
<div class="aside">
<em class="label label-info">{{result.distance}}km</em>
Expand Down
63 changes: 41 additions & 22 deletions js/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ app.controller('SearchController', function($scope, $rootScope, $sce, $http) {
remove_markers();
});

$rootScope.$on( 'fullTextSearch', function( ev, text, searchType ){
$rootScope.$on( 'fullTextSearch', function( ev, text, searchType, geoBias ){
$(document).trigger({
'type': "pelias:fullTextSearch",
'text' : text,
'searchType' : searchType
'searchType' : searchType,
'geoBias': geoBias
});
});

Expand Down Expand Up @@ -148,25 +149,30 @@ app.controller('SearchController', function($scope, $rootScope, $sce, $http) {
};

var getResults = function(url, resultkey) {
var bounds = map.getBounds();
var bbox = [];
bbox.push(bounds._northEast.lat);
bbox.push(bounds._northEast.lng);
bbox.push(bounds._southWest.lat);
bbox.push(bounds._southWest.lng);
var params = {
input: $scope.search,
// datasets: $scope.queryDatasets.join(','),
size: 10
}

if ($scope.geobias === 'on') {
var bounds = map.getBounds();
var bbox = [];
bbox.push(bounds._northEast.lat);
bbox.push(bounds._northEast.lng);
bbox.push(bounds._southWest.lat);
bbox.push(bounds._southWest.lng);

params.lat = $rootScope.geobase ? $rootScope.geobase.lat : 0;
params.lon = $rootScope.geobase ? $rootScope.geobase.lon : 0;
params.zoom= $rootScope.geobase ? $rootScope.geobase.zoom : 12;
params.bbox= bbox.length === 4 ? bbox.join(',') : '';
}

$http({
url: $scope.api_url+url,
method: 'GET',
params: {
input: $scope.search,
// datasets: $scope.queryDatasets.join(','),
lat: $rootScope.geobase ? $rootScope.geobase.lat : 0,
lon: $rootScope.geobase ? $rootScope.geobase.lon : 0,
zoom:$rootScope.geobase ? $rootScope.geobase.zoom : 12,
bbox:bbox.length === 4 ? bbox.join(',') : '',
size: 10
},
params: params,
headers: { 'Accept': 'application/json' }
}).success(function (data, status, headers, config) {
if( data ){
Expand All @@ -177,6 +183,7 @@ app.controller('SearchController', function($scope, $rootScope, $sce, $http) {
$scope[resultkey] = data.features.map( function( res ){
res.htmltext = $sce.trustAsHtml(highlight( res.properties.text, $scope.search ));
res.icon = icon( res.properties.type || 'search' );
res.type = res.properties.type;
res.distance = computeDistance(res.geometry.coordinates);
return res;
});
Expand All @@ -192,11 +199,18 @@ app.controller('SearchController', function($scope, $rootScope, $sce, $http) {
$scope.search = '';
$scope.searchresults = [];
$scope.suggestresults = [];
$scope.searchType = 'FINE';
$scope.geobias = 'on';
$scope.searchType = 'fine';
$scope.api_url = '//pelias.mapzen.com';

$scope.switchType = function(type) {
$scope.searchType = type === 'FINE' ? 'COARSE' : 'FINE';
$scope.searchType = type === 'fine' ? 'coarse' : 'fine';
$rootScope.$emit( 'hideall' );
$scope.fullTextSearch();
};

$scope.switchGeobias = function(geobias) {
$scope.geobias = geobias === 'on' ? 'off' : 'on';
$rootScope.$emit( 'hideall' );
$scope.fullTextSearch();
};
Expand Down Expand Up @@ -243,7 +257,7 @@ app.controller('SearchController', function($scope, $rootScope, $sce, $http) {
return;
}

var url = $scope.searchType === 'FINE' ? '/suggest' : '/suggest/coarse';
var url = $scope.searchType.toLowerCase() === 'fine' ? '/suggest' : '/suggest/coarse';
getResults(url, 'suggestresults');
}

Expand All @@ -253,9 +267,9 @@ app.controller('SearchController', function($scope, $rootScope, $sce, $http) {
$rootScope.$emit( 'hideall' );
return;
}
$rootScope.$emit('fullTextSearch', $scope.search, $scope.searchType);
$rootScope.$emit('fullTextSearch', $scope.search, $scope.searchType, $scope.geobias);

var url = $scope.searchType === 'FINE' ? '/search' : '/search/coarse';
var url = $scope.searchType.toLowerCase() === 'fine' ? '/search' : '/search/coarse';
getResults(url, 'searchresults');
}

Expand All @@ -274,6 +288,11 @@ app.controller('SearchController', function($scope, $rootScope, $sce, $http) {
$scope.searchType = hash_search_type;
$scope.keyPressed({ 'which': 13});
}
var hash_geobias = hash_params ? hash_params.gb : false;
if (hash_geobias){
$scope.geobias = hash_geobias;
$scope.keyPressed({ 'which': 13});
}

$(document).on('new-location', $scope.suggest);
})
14 changes: 13 additions & 1 deletion js/leaflet-hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
} else {
var query = hash_obj.q ? hash_obj.q : null;
var searchType = hash_obj.t ? hash_obj.t : null;
var geoBias = hash_obj.gb ? hash_obj.gb : null;
var return_obj = {
center: new L.LatLng(lat, lon),
zoom: zoom
Expand All @@ -71,6 +72,10 @@
this.lastSearchType = searchType;
return_obj['t'] = searchType;
}
if ( geoBias && this.lastGeoBias != geoBias ) {
this.lastGeoBias = geoBias;
return_obj['gb'] = geoBias;
}
return return_obj;
}
} else {
Expand All @@ -92,14 +97,16 @@

var query = this.lastSearchQuery ? "&q=" + this.lastSearchQuery : "";
var searchType = this.lastSearchType ? "&t=" + this.lastSearchType : "";
return loc + query + searchType;
var geoBias = this.lastGeoBias ? "&gb=" + this.lastGeoBias : "";
return loc + query + searchType + geoBias;
},

L.Hash.prototype = {
map: null,
lastHash: null,
lastSearchQuery: null,
lastSearchType: null,
lastGeoBias: null,

parseHash: L.Hash.parseHash,
formatHash: L.Hash.formatHash,
Expand All @@ -113,6 +120,7 @@
this.lastHash = null;
this.lastSearchQuery = null;
this.lastSearchType = null;
this.lastGeoBias = null;
this.onHashChange();

if (!this.isListening) {
Expand Down Expand Up @@ -196,6 +204,10 @@
that.lastSearchType = e.searchType;
that.onMapMove();
}
if (that.lastGeoBias != e.geoBias) {
that.lastGeoBias = e.geoBias;
that.onMapMove();
}
});

if (HAS_HASHCHANGE) {
Expand Down

0 comments on commit 0597a3e

Please sign in to comment.