Commit 6394d990 authored by Deployer's avatar Deployer

Added alt_name handler in address locater demo file. Handled addresses more…

Added alt_name handler in address locater demo file. Handled addresses more gracefully. Fixed a bug regarding auto complete module in address locater demo file.
parent c107a4da
...@@ -5,13 +5,15 @@ ...@@ -5,13 +5,15 @@
<meta charset=utf-8 /> <meta charset=utf-8 />
<title>CedarMaps - Address Locator Tool</title> <title>CedarMaps - Address Locator Tool</title>
<!-- Importing **local** version of cedarmaps -->
<script src='../dist/v1.8.0/cedarmaps.js'></script> <script src='../dist/v1.8.0/cedarmaps.js'></script>
<script src='../access-token.js'></script> <script src='../access-token.js'></script>
<!-- Third party libraries used in this demo --> <!-- Third party libraries used in this demo -->
<script src='./js/auto-complete.js'></script> <script src='./js/auto-complete.js'></script>
<script src='./js/L.Control.Locate.min.js'></script> <script src='./js/L.Control.Locate.min.js'></script>
<script src='./js/tiniAjax.js'></script>
<link href='../dist/v1.8.0/cedarmaps.css' rel='stylesheet' /> <link href='../dist/v1.8.0/cedarmaps.css' rel='stylesheet' />
<link href='./css/address-locator.css' rel='stylesheet' /> <link href='./css/address-locator.css' rel='stylesheet' />
...@@ -309,15 +311,31 @@ ...@@ -309,15 +311,31 @@
var autoCompelete = new autoComplete({ var autoCompelete = new autoComplete({
selector: 'input[name="streets"]', selector: 'input[name="streets"]',
minChars: 2, minChars: 2,
cache: 0, // set to 1 if you need caching and making less requests
source: function(term, response){ source: function(term, response){
// Let's use Cedarmaps' geocoder as the data source for our autocomplete module. // Since we wont have full control over ajax responses and caching problems may occure if we directly
geocoder.query({query: term, ne: globalSearchBoundingBox.ne, sw: globalSearchBoundingBox.sw}, function(err, res){ // use geocoder.query, we build the request URL instead and then we use it our custom auto complete module.
// As of autocompelete's signature, the results should be wrapped in `response` function. // It's a workaround for not getting cached results in our list.
if (typeof res != 'undefined') response(res.results); var queryURL = geocoder.queryURL({query: term, ne: globalSearchBoundingBox.ne, sw: globalSearchBoundingBox.sw});
// In this demo we use a tiny 3rd party library for AJAX requests as we don't use jQuery or other bulky libraries.
// So feel free to replace it with your preferred one. jQuery syntax for example would be like: $.ajax()
Tini.ajax({
url: queryURL,
type:'GET',
success:function(res){
var json = JSON.parse(res);
if (window.console) console.log(json);
if (typeof json != 'undefined') {
if (json.results) response(json.results);
//no results
else {}
}
}
}); });
}, },
renderItem: function (item, search){ renderItem: function (item, search){
var img, label; var img, label, addressParts = [];
switch (item.type) { switch (item.type) {
case 'street': case 'street':
...@@ -359,13 +377,21 @@ ...@@ -359,13 +377,21 @@
} }
if(!item.address || item.address == '') { if(!item.address || item.address == '') {
item.address = item.components.province + '، ' + item.components.city if (item.components.province) addressParts.push(item.components.province);
if (item.components.city) addressParts.push(item.components.city);
item.address = addressParts.join('، ');
} else { } else {
item.address = item.components.province + '، ' + item.address; if (item.components.province) addressParts.push(item.components.province);
if (item.address) addressParts.push(item.address);
item.address = addressParts.join('، ');
} }
var altName = item.alt_name ? ' ('+item.alt_name+')': '';
return '<div class="autocomplete-suggestion" data-name="'+item.name+'" data-center="'+item.location.center+'">\ return '<div class="autocomplete-suggestion" data-name="'+item.name+'" data-center="'+item.location.center+'">\
<div class="address">'+item.name+'<span>'+item.address+'</span></div>\ <div class="address">'+item.name+altName+'<span>'+item.address+'</span></div>\
<div class="street-type">\ <div class="street-type">\
<img src="img/'+img+'" alt="" width="20" height="20">\ <img src="img/'+img+'" alt="" width="20" height="20">\
<span>'+label+'</span>\ <span>'+label+'</span>\
......
//Very light ajax lib that replaces jQuery. By Jordan Wambaugh. V1.3 github.com/martamius/tiniAjax
var Tini={};
Tini.r=function(){try{return new ActiveXObject('Msxml2.XMLHTTP')}catch(e){try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){return new XMLHttpRequest()}}};
Tini.s=function(u,f,m,a){var r=Tini.r();r.open(m,u,true);r.onreadystatechange=function(){if(r.readyState==4 && f){f(r.responseText)}};if(m=='POST')r.setRequestHeader('Content-type','application/x-www-form-urlencoded');r.send(a)};
Tini.p=function(o,k){var p='';for(var x in o){var d=o[x];var l;if(k!=undefined)l=k+"["+x+"]";else l=x;if(typeof(d)=='object')p+=Tini.p(d,l);else p+=escape(l)+"="+escape(d)+'&'}return p};
Tini.ajax=function(c){var p="";if(c.data!=undefined){p=Tini.p(c.data)}if(c.type.toLowerCase()=='post'){Tini.s(c.url,c.success,'POST',p)}else{if(c.url.indexOf('?')==-1)c.url+='?';else c.url+='&';Tini.s(c.url+p,c.success,'GET')}};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment