Skip to content

Commit 1ca482e

Browse files
committed
Add numbered marker support in reverse geocoding demo
1 parent bef519a commit 1ca482e

File tree

3 files changed

+75
-14
lines changed

3 files changed

+75
-14
lines changed

demo/static/img/marker_hole.png

4.48 KB
Loading

demo/static/js/address.coffee

+34-13
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,29 @@ $("#address-input").typeahead(
2424
process_cb(ret)
2525
)
2626
)
27-
find_nearby_addresses = (coords) ->
28-
url = API_PREFIX + "v1/address/?format=json&lat=#{ coords[0] }&lon=#{ coords[1] }"
27+
28+
nearby_markers = []
29+
30+
find_nearby_addresses = (target_coords) ->
31+
url = API_PREFIX + "v1/address/?format=json&lat=#{target_coords[0]}&lon=#{target_coords[1]}"
2932
$.getJSON(url, (data) ->
3033
objs = data.objects
3134
el = $("#nearby-addr-list")
3235
el.empty()
36+
for m in nearby_markers
37+
map.removeLayer m
38+
nearby_markers = []
39+
index = 1
3340
for addr in objs
3441
name = addr.name
3542
distance = Math.round(addr.distance)
36-
el.append($("<li>#{ addr.name } #{ distance } m</li>"))
43+
coords = addr.location.coordinates
44+
m = new L.Marker [coords[1], coords[0]],
45+
icon: new L.NumberedDivIcon {number: index.toString()}
46+
m.addTo map
47+
nearby_markers.push m
48+
el.append($("<li>#{addr.name} #{distance} m</li>"))
49+
index++
3750
)
3851
$("#address-input").on 'change', ->
3952
match_obj = null
@@ -87,13 +100,6 @@ $("#district-input").on 'change', ->
87100
map.fitBounds borders.getBounds()
88101

89102
show_plans = false
90-
$("#show-plans").on 'click', ->
91-
show_plans = true
92-
if map.getZoom() < 13
93-
map.setZoom 13
94-
# refresh_plans() will be called automatically through the 'moveend' event.
95-
else
96-
refresh_plans()
97103

98104
map.on 'moveend', (ev) ->
99105
if not show_plans
@@ -117,8 +123,6 @@ hover_style =
117123
color: "orange"
118124

119125
plan_click = (ev) ->
120-
console.log ev
121-
console.log this
122126

123127
plan_hover_start = (ev) ->
124128
@.setStyle hover_style
@@ -145,6 +149,7 @@ draw_plans = (new_plans) ->
145149
geom.on 'mouseover', plan_hover_start
146150
geom.on 'mouseout', plan_hover_end
147151
geom.addTo map
152+
obj.geom = geom
148153

149154
plan_refresher = null
150155
refresh_plans = ->
@@ -174,8 +179,24 @@ class PlanRefresher
174179
return
175180
draw_plans data.objects
176181
next = data.meta.next
177-
console.log data.meta
178182
if next
179183
@current_xfer = $.getJSON next, receive_plans
180184

181185
@current_xfer = $.getJSON url, params, receive_plans
186+
187+
$("#show-plans").on 'click', ->
188+
if show_plans
189+
for plan_id of plans
190+
plan = plans[plan_id]
191+
map.removeLayer plan.geom
192+
plans = {}
193+
show_plans = false
194+
$("#show-plans").html 'Show plans'
195+
return
196+
show_plans = true
197+
if map.getZoom() < 13
198+
map.setZoom 13
199+
# refresh_plans() will be called automatically through the 'moveend' event.
200+
else
201+
refresh_plans()
202+
$("#show-plans").html 'Hide plans'

demo/templates/demo.html

+41-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@
1717
padding-top: 20px;
1818
padding-bottom: 40px;
1919
}
20+
.leaflet-numbered-div-icon {
21+
background: transparent;
22+
border: none;
23+
}
24+
.leaflet-marker-icon .number {
25+
position: relative;
26+
top: -37px;
27+
font-size: 12px;
28+
width: 25px;
29+
text-align: center;
30+
}
2031
</style>
2132
<script>
2233
API_PREFIX = "{{ API_PREFIX }}";
@@ -28,6 +39,35 @@
2839
<script src="{% static 'js/tile.stamen.js' %}"></script>
2940
<script src="{% static 'js/moment.min.js' %}"></script>
3041
<script src="{% static 'js/moment-lang-fi.js' %}"></script>
42+
<script>
43+
L.NumberedDivIcon = L.Icon.extend({
44+
options: {
45+
// EDIT THIS TO POINT TO THE FILE AT http://www.charliecroom.com/marker_hole.png (or your own marker)
46+
iconUrl: '{% static "img/marker_hole.png" %}',
47+
number: '',
48+
shadowUrl: null,
49+
iconSize: new L.Point(25, 41),
50+
iconAnchor: new L.Point(13, 41),
51+
popupAnchor: new L.Point(0, -33),
52+
className: 'leaflet-numbered-div-icon'
53+
},
54+
createIcon: function () {
55+
var div = document.createElement('div');
56+
var img = this._createImg(this.options['iconUrl']);
57+
var numdiv = document.createElement('div');
58+
numdiv.setAttribute ( "class", "number" );
59+
numdiv.innerHTML = this.options['number'] || '';
60+
div.appendChild ( img );
61+
div.appendChild ( numdiv );
62+
this._setIconStyles(div, 'icon');
63+
return div;
64+
},
65+
//you could change this to add a shadow like in the normal marker if you really wanted
66+
createShadow: function () {
67+
return null;
68+
}
69+
});
70+
</script>
3171
</head>
3272
<body>
3373
<!--[if lt IE 7]>
@@ -52,7 +92,7 @@ <h1>Paikkatieto-demo</h1>
5292
</div>
5393
<div style="clear: both"></div>
5494
<div class="row">
55-
<ul id="nearby-addr-list"></ul>
95+
<ol id="nearby-addr-list"></ol>
5696
</div>
5797
</div> <!-- /container -->
5898

0 commit comments

Comments
 (0)