/
markers.html
197 lines (165 loc) · 6.82 KB
/
markers.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html>
<html>
<head>
<title>ModestMaps JS: GeoJSON Markers + Spotlight Effect</title>
<script type="text/javascript" src="../../modestmaps.js"></script>
<script type="text/javascript" src="modestmaps.markers.js"></script>
<script type="text/javascript" src="spotlight.js"></script>
<script type="text/javascript">
var MM = com.modestmaps,
map,
markers,
spotlight,
locationsByType = {};
function initMap() {
var provider = new MM.TemplatedLayer("http://ecn.t{S}.tiles.virtualearth.net/tiles/r{Q}?g=689&mkt=en-us&lbl=l0&stl=m", [0,1,2,3,4,5,6,7]);
map = new MM.Map("map", provider);
map.setCenterZoom(new MM.Location(37.764, -122.419), 12);
spotlight = new SpotlightLayer();
map.addLayer(spotlight);
markers = new MM.MarkerLayer();
map.addLayer(markers);
loadMarkers();
}
// ghetto JSON-P
function loadMarkers() {
var script = document.createElement("script");
script.src = "http://sanfrancisco.crimespotting.org/crime-data?dstart=2011-12-24&dend=2011-12-25&format=json&callback=onLoadMarkers";
document.getElementsByTagName("head")[0].appendChild(script);
}
function onLoadMarkers(collection) {
// onLoadMarkers() gets a GeoJSON FeatureCollection:
// http://geojson.org/geojson-spec.html#feature-collection-objects
var features = collection.features,
len = features.length,
locations = [];
// for each feature in the collection, create a marker and add it
// to the markers layer
for (var i = 0; i < len; i++) {
var feature = features[i],
type = feature.properties.crime_type,
marker = document.createElement("a");
marker.feature = feature;
marker.type = type;
// give it a title
marker.setAttribute("title", [
type, "on", feature.properties.date_time
].join(" "));
// add a class
marker.setAttribute("class", "report");
// set the href to link to crimespotting's crime page
marker.setAttribute("href", "http://sanfrancisco.crimespotting.org/crime/" + [
feature.properties.date_time.substr(0, 10),
type.replace(/ /g, "_"),
feature.id
].join("/"));
// create an image icon
var img = marker.appendChild(document.createElement("img"));
img.setAttribute("src", "../geojson/icons/" + type.replace(/ /g, "_") + ".png");
markers.addMarker(marker, feature);
// add the marker's location to the extent list
locations.push(marker.location);
if (type in locationsByType) {
locationsByType[type].push(marker.location);
} else {
locationsByType[type] = [marker.location];
}
// listen for mouseover & mouseout events
MM.addEvent(marker, "mouseover", onMarkerOver);
MM.addEvent(marker, "mouseout", onMarkerOut);
}
// tell the map to fit all of the locations in the available space
map.setExtent(locations);
}
function getMarker(target) {
var marker = target;
while (marker && marker.className != "report") {
marker = marker.parentNode;
}
return marker;
}
function onMarkerOver(e) {
var marker = getMarker(e.target);
if (marker) {
var type = marker.type;
console.log("over:", type);
if (type in locationsByType) {
spotlight.addLocations(locationsByType[type] || []);
spotlight.parent.className = "active";
} else {
spotlight.parent.className = "inactive";
}
}
}
function onMarkerOut(e) {
var marker = getMarker(e.target);
if (marker) {
var type = marker.type;
console.log("out:", type);
spotlight.removeAllLocations();
spotlight.parent.className = "inactive";
}
}
</script>
<style type="text/css">
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 16px;
padding: 20px;
margin: 0;
}
h1 {
margin: 0 0 20px 0;
}
#map {
width: 100%;
height: 500px;
}
.report {
margin-left: -13px;
margin-top: -13px;
width: 26px;
height: 26px;
}
.report img {
border: none !important;
}
.report:hover {
z-index: 1000;
}
#map canvas {
transition-property: opacity;
-webkit-transition-property: opacity;
-moz-transition-property: opacity;
-ms-transition-property: opacity;
-o-transition-property: opacity;
transition-duration: .6s;
-webkit-transition-duration: .6s;
-moz-transition-duration: .6s;
-ms-transition-duration: .6s;
-o-transition-duration: .6s;
transition-delay: .1s;
-webkit-transition-delay: .1s;
-moz-transition-delay: .1s;
-ms-transition-delay: .1s;
-o-transition-delay: .1s;
opacity: 0;
}
#map canvas.active {
opacity: 1;
}
</style>
</head>
<body onload="initMap()">
<h1>ModestMaps JS: GeoJSON Markers + Spotlight Effect</h1>
<p>This example mimics the behavior of <a href="http://sanfrancisco.crimespotting.org">Crimespotting</a>
(inspired by <a href="http://arstechnica.com/apple/reviews/2005/04/macosx-10-4.ars/20#system-preferences">the Mac OS X feature</a>),
highlighting reports of the same type whenever you mouse over one.</p>
<p><strong>This example requires a web browser with
<a href="http://en.wikipedia.org/wiki/Canvas_element">HTML canvas</a>
support.</strong></p>
<div id="map">
</div>
</body>
</html>