Skip to content

Commit 16b8b3b

Browse files
committed
v 1.1 reworked suggestions ordering, fixed address droping, example fixes
1 parent 78e4c6b commit 16b8b3b

File tree

4 files changed

+146
-24
lines changed

4 files changed

+146
-24
lines changed

angular-map-md.js

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
geocoding_datetime: null,
3939
approximate_address: null,
4040
inside_delivery_area: null,
41+
apartment: '',
42+
entrance: '',
43+
floor: '',
44+
entrance_code: '',
45+
is_private_building: null,
4146
comment: '',
4247
};
4348
var addressIsExact = function (address) {
@@ -255,11 +260,20 @@
255260
this.address.addressString = null;
256261
};
257262

263+
this.clearBuildingData = function(){
264+
this.address.apartment = null;
265+
this.address.entrance = null;
266+
this.address.floor = null;
267+
this.address.entrance_code = null;
268+
this.address.is_private_building = null;
269+
};
270+
258271
this.clearAddressFull = function () {
259272
this.address.street = null;
260273
this.address.number = null;
261274
this.address.addressString = null;
262275
this.clearPositionData();
276+
this.clearBuildingData();
263277
};
264278

265279
return this;
@@ -730,6 +744,27 @@
730744
similarity: similarity
731745
};
732746
}).service('addressNumberCompareService', function (stringCompare) {
747+
function splitNumberLetter(suggestResult) {
748+
var r = /([\d\/]+)([\D]+)?$/;
749+
let found = r.exec(suggestResult);
750+
if (found[0]){
751+
return {
752+
"number": found[1],
753+
"letter": found[2],
754+
};
755+
}
756+
return null;
757+
}
758+
759+
function _getNumberLetters(buildingNumber) {
760+
var r = /[\D]?$/;
761+
let found = r.exec(buildingNumber);
762+
if (found){
763+
return found[0];
764+
}
765+
return null;
766+
}
767+
733768
function _getDelta(numStr1, numStr2){
734769
var r = /^[\d]+/;
735770
let delta = Infinity;
@@ -750,6 +785,33 @@
750785
return 0
751786
}
752787

788+
function _getStringLikeness(string1, string2){
789+
let similarity = _getSimilarity(string1, string2);
790+
let closeness = _getDelta(string1, string2);
791+
let matchQuality = (similarity * 0.5) + (0.5 / (1 + closeness));
792+
return matchQuality;
793+
}
794+
795+
function _getMatchQuality(addressNumber, item){
796+
// if address number has letters in it it should be matched normally 80/7a
797+
// if it does not have letters (80/7) in it should be matched to all possible buildings
798+
// as if they dont have letters also so that 80/7A would be closer matched to 80/7 then 80/8
799+
let splitBuildNumber = splitNumberLetter(addressNumber);
800+
let matchQuality = 0;
801+
if (splitBuildNumber && !splitBuildNumber['letter']){
802+
// request does not have a letter so we compare only numbers
803+
let splitItem = splitNumberLetter(item);
804+
if (splitItem && splitItem['letter']){
805+
matchQuality = _getStringLikeness(addressNumber, splitItem['number']);
806+
}else{
807+
matchQuality = _getStringLikeness(addressNumber, item);
808+
}
809+
}else{
810+
matchQuality = _getStringLikeness(addressNumber, item);
811+
}
812+
return matchQuality;
813+
}
814+
753815
function findClosestMatch(numberList, addressNumber, minQuality){
754816
minQuality = minQuality || 0.25;
755817
let topMatch = {
@@ -758,9 +820,7 @@
758820
match: null,
759821
};
760822
_.each(numberList, function(item){
761-
let similarity = _getSimilarity(addressNumber, item);
762-
let closeness = _getDelta(addressNumber, item);
763-
let matchQuality = (similarity * 0.5) + (0.5 / (1 + closeness));
823+
let matchQuality = _getMatchQuality(addressNumber, item);
764824
if (matchQuality && matchQuality >= topMatch.quality){
765825
topMatch.match = item;
766826
topMatch.quality = matchQuality;
@@ -777,9 +837,7 @@
777837
maxNumbers = maxNumbers || 4;
778838
let matches = [];
779839
_.each(numberList, function(item){
780-
let similarity = _getSimilarity(addressNumber, item);
781-
let closeness = _getDelta(addressNumber, item);
782-
let matchQuality = (similarity * 0.5) + (0.5 / (1 + closeness));
840+
let matchQuality = _getMatchQuality(addressNumber, item);
783841
if (matchQuality && matchQuality >= minQuality){
784842
matches.push({
785843
match: item,
@@ -796,6 +854,7 @@
796854
return {
797855
findClosestMatch: findClosestMatch,
798856
findTopMatches: findTopMatches,
857+
findTopMatches: findTopMatches,
799858
};
800859
})
801860
})();

example/index.html

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,30 @@ <h2>MAP.MD API KEY: {{savedApiKey || 'map.md api key not set!'}}</h2>
2525
<hr>
2626
<fieldset>
2727
<div class="form-group">
28-
<label class="control-label col-xs-2">Forward geocode</label>
29-
<div class="col-xs-7">
30-
<input class="form-control" type="text" ng-model="forwardGeocodeString"/>
31-
</div>
32-
<div class="col-xs-12">
33-
<button class="btn btn-success" ng-click="doForwardGeocode()">GEOCODE!</button>
34-
<button class="btn btn-default" ng-click="clearForwardGeocode()">Clear</button>
28+
<div class="row">
29+
<label class="control-label col-xs-2">Forward geocode</label>
30+
<div class="col-xs-7">
31+
<input class="form-control" type="text" ng-model="forwardGeocodeString"/>
32+
</div>
3533
</div>
36-
<div class="col-xs-12" ng-if="forwardGeocodeResult">
37-
<div class="well">
38-
{{forwardGeocodeResult}}
34+
<div class="row">
35+
<div class="col-xs-12">
36+
<button class="btn btn-success" ng-click="doForwardGeocode()">GEOCODE!</button>
37+
<button class="btn btn-default" ng-click="clearForwardGeocode()">Clear</button>
3938
</div>
4039
</div>
40+
<div class="col-xs-12" ng-if="forwardGeocodeResult">
41+
<div class="well">
42+
{{forwardGeocodeResult}}
43+
</div>
44+
</div>
4145
</div>
4246
</fieldset>
4347

4448

4549
<fieldset>
4650
<div class="form-group">
47-
<label class="control-label col-xs-2">Suggestions</label>
51+
<label class="control-label col-xs-2">Reverse geocode (lat/long)</label>
4852
<div class="col-xs-5">
4953
<input class="form-control" type="text" ng-model="reverseGeocodeLat"/>
5054
</div>
@@ -71,7 +75,7 @@ <h2>MAP.MD API KEY: {{savedApiKey || 'map.md api key not set!'}}</h2>
7175

7276
<fieldset>
7377
<div class="form-group">
74-
<label class="control-label col-xs-2">Reverse geocode (lat/long)</label>
78+
<label class="control-label col-xs-2">Suggestions</label>
7579
<div class="col-xs-7">
7680
<input class="form-control" type="text" ng-model="suggestionsString"/>
7781
</div>

source/services/address-container.service.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
geocoding_datetime: null,
2020
approximate_address: null,
2121
inside_delivery_area: null,
22+
apartment: '',
23+
entrance: '',
24+
floor: '',
25+
entrance_code: '',
26+
is_private_building: null,
2227
comment: '',
2328
};
2429
var addressIsExact = function (address) {
@@ -236,11 +241,20 @@
236241
this.address.addressString = null;
237242
};
238243

244+
this.clearBuildingData = function(){
245+
this.address.apartment = null;
246+
this.address.entrance = null;
247+
this.address.floor = null;
248+
this.address.entrance_code = null;
249+
this.address.is_private_building = null;
250+
};
251+
239252
this.clearAddressFull = function () {
240253
this.address.street = null;
241254
this.address.number = null;
242255
this.address.addressString = null;
243256
this.clearPositionData();
257+
this.clearBuildingData();
244258
};
245259

246260
return this;

source/services/geolocate.service.js

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,27 @@
8383
similarity: similarity
8484
};
8585
}).service('addressNumberCompareService', function (stringCompare) {
86+
function splitNumberLetter(suggestResult) {
87+
var r = /([\d\/]+)([\D]+)?$/;
88+
let found = r.exec(suggestResult);
89+
if (found[0]){
90+
return {
91+
"number": found[1],
92+
"letter": found[2],
93+
};
94+
}
95+
return null;
96+
}
97+
98+
function _getNumberLetters(buildingNumber) {
99+
var r = /[\D]?$/;
100+
let found = r.exec(buildingNumber);
101+
if (found){
102+
return found[0];
103+
}
104+
return null;
105+
}
106+
86107
function _getDelta(numStr1, numStr2){
87108
var r = /^[\d]+/;
88109
let delta = Infinity;
@@ -103,6 +124,33 @@
103124
return 0
104125
}
105126

127+
function _getStringLikeness(string1, string2){
128+
let similarity = _getSimilarity(string1, string2);
129+
let closeness = _getDelta(string1, string2);
130+
let matchQuality = (similarity * 0.5) + (0.5 / (1 + closeness));
131+
return matchQuality;
132+
}
133+
134+
function _getMatchQuality(addressNumber, item){
135+
// if address number has letters in it it should be matched normally 80/7a
136+
// if it does not have letters (80/7) in it should be matched to all possible buildings
137+
// as if they dont have letters also so that 80/7A would be closer matched to 80/7 then 80/8
138+
let splitBuildNumber = splitNumberLetter(addressNumber);
139+
let matchQuality = 0;
140+
if (splitBuildNumber && !splitBuildNumber['letter']){
141+
// request does not have a letter so we compare only numbers
142+
let splitItem = splitNumberLetter(item);
143+
if (splitItem && splitItem['letter']){
144+
matchQuality = _getStringLikeness(addressNumber, splitItem['number']);
145+
}else{
146+
matchQuality = _getStringLikeness(addressNumber, item);
147+
}
148+
}else{
149+
matchQuality = _getStringLikeness(addressNumber, item);
150+
}
151+
return matchQuality;
152+
}
153+
106154
function findClosestMatch(numberList, addressNumber, minQuality){
107155
minQuality = minQuality || 0.25;
108156
let topMatch = {
@@ -111,9 +159,7 @@
111159
match: null,
112160
};
113161
_.each(numberList, function(item){
114-
let similarity = _getSimilarity(addressNumber, item);
115-
let closeness = _getDelta(addressNumber, item);
116-
let matchQuality = (similarity * 0.5) + (0.5 / (1 + closeness));
162+
let matchQuality = _getMatchQuality(addressNumber, item);
117163
if (matchQuality && matchQuality >= topMatch.quality){
118164
topMatch.match = item;
119165
topMatch.quality = matchQuality;
@@ -130,9 +176,7 @@
130176
maxNumbers = maxNumbers || 4;
131177
let matches = [];
132178
_.each(numberList, function(item){
133-
let similarity = _getSimilarity(addressNumber, item);
134-
let closeness = _getDelta(addressNumber, item);
135-
let matchQuality = (similarity * 0.5) + (0.5 / (1 + closeness));
179+
let matchQuality = _getMatchQuality(addressNumber, item);
136180
if (matchQuality && matchQuality >= minQuality){
137181
matches.push({
138182
match: item,
@@ -149,6 +193,7 @@
149193
return {
150194
findClosestMatch: findClosestMatch,
151195
findTopMatches: findTopMatches,
196+
findTopMatches: findTopMatches,
152197
};
153198
})
154199
})();

0 commit comments

Comments
 (0)