| 1 | = 2009-02-04 = |
| 2 | |
| 3 | * 原本 waue 發表的[wiki:oid/WorkLog/GoogleMPL Google Map的網址模組轉換],在官方的名稱為 [http://code.google.com/intl/zh-TW/apis/maps/documentation/mapplets/ Google Mapplets API] |
| 4 | * "赤壁"原始碼 - http://code.google.com/p/redcliff/source/checkout |
| 5 | {{{ |
| 6 | svn checkout http://redcliff.googlecode.com/svn/trunk/ redcliff-read-only |
| 7 | }}} |
| 8 | * [[Image(redcliff.jpg)]] |
| 9 | * [[Image(people.json.jpg)]] |
| 10 | * [[Image(big_event.json.jpg)]] |
| 11 | * [[Image(event.json.jpg)]] |
| 12 | * [[Image(location.json.jpg)]] |
| 13 | * [[Image(element.json.jpg)]] |
| 14 | * [http://maps.google.com/maps/mpl?moduleurl=http://earthquake.usgs.gov/eqcenter/mapplets/earthquakes.xml 國外的地震中心google maps] 原始碼解析 |
| 15 | {{{ |
| 16 | #!js |
| 17 | <?xml version="1.0" encoding="UTF-8"?> |
| 18 | <Module> |
| 19 | <ModulePrefs title="Real-time Earthquakes" |
| 20 | description="USGS Earthquakes for the past 7 days" |
| 21 | author="Scott Haefner" |
| 22 | author_email="shaefner@usgs.gov" |
| 23 | author_affiliation="U.S. Geological Survey" |
| 24 | screenshot="http://earthquake.usgs.gov/eqcenter/mapplets/ss-earthquakes.png" |
| 25 | thumbnail="http://earthquake.usgs.gov/eqcenter/mapplets/tn-earthquakes.png" |
| 26 | height="300"> |
| 27 | <Require feature="sharedmap"/> |
| 28 | <Require feature="analytics"/> |
| 29 | </ModulePrefs> |
| 30 | <Content type="html"> |
| 31 | <![CDATA[ |
| 32 | <style type="text/css"> |
| 33 | h1 { |
| 34 | font-size: 18px; |
| 35 | color: #333; |
| 36 | padding: 0; |
| 37 | margin: 0 5px; |
| 38 | } |
| 39 | h2 { |
| 40 | font-size: 14px; |
| 41 | color: #633; |
| 42 | padding: 0; |
| 43 | margin: 1em 5px; |
| 44 | } |
| 45 | p { |
| 46 | font-family: Georgia, serif; |
| 47 | font-size: 12px; |
| 48 | line-height: 18px; |
| 49 | color: #666; |
| 50 | padding: 0; |
| 51 | margin: 2px 0 6px 8px; |
| 52 | } |
| 53 | ol { |
| 54 | font-size: 12px; |
| 55 | margin: 1em 1em 1em 2.5em; |
| 56 | padding: 0; |
| 57 | } |
| 58 | li { |
| 59 | margin: .4em 0; |
| 60 | padding: 0; |
| 61 | } |
| 62 | img { |
| 63 | padding-left: 10px; |
| 64 | } |
| 65 | </style> |
| 66 | <h1>M 4+ Earthquakes, past 7 days</h1> |
| 67 | <div id="content"> |
| 68 | </div> |
| 69 | |
| 70 | <script type="text/javascript"> |
| 71 | |
| 72 | // Global variables |
| 73 | var map = null; |
| 74 | var eqs = []; |
| 75 | var icons = []; |
| 76 | var markers = []; |
| 77 | |
| 78 | var points = { |
| 79 | color: { // Marker: icon color |
| 80 | 'pasthour': 'red', |
| 81 | 'pastday': 'orange', |
| 82 | 'pastweek': 'yellow' |
| 83 | }, |
| 84 | size: { // Marker: icon size in pixels |
| 85 | 'M1': 11, |
| 86 | 'M2': 15, |
| 87 | 'M3': 17, |
| 88 | 'M4': 21, |
| 89 | 'M5': 25, |
| 90 | 'M6': 31, |
| 91 | 'M7': 37, |
| 92 | 'M8': 45, |
| 93 | 'M9': 55 |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | |
| 98 | function initMap() { |
| 99 | createMap(); |
| 100 | readMarkers(); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | function createMap() { |
| 105 | var map_center = new GLatLng(20, 170); |
| 106 | var map_zoom = 2; |
| 107 | var map_type = G_SATELLITE_MAP; //G_NORMAL_MAP |
| 108 | map = new GMap2({ logoPassive:true }); |
| 109 | map.setCenter(map_center, map_zoom, map_type); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | function readMarkers() { |
| 114 | |
| 115 | // Fetch RSS feed |
| 116 | var feed = "http://earthquake.usgs.gov/eqcenter/catalogs/eqs7day-M4.xml"; |
| 117 | _IG_FetchXmlContent(feed, function(response) { |
| 118 | if (response == null || typeof(response) != "object" || response.firstChild == null) { |
| 119 | _gel("content").innerHTML = "<i>Invalid data.</i>"; |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | var pubDate = response.getElementsByTagName("pubDate"); |
| 124 | var feed_pubDate = pubDate.item(0).firstChild.nodeValue; |
| 125 | |
| 126 | eqs = response.getElementsByTagName("item"); |
| 127 | |
| 128 | var html = "<p>" + eqs.length + " earthquakes on this map<br />Updated " + feed_pubDate + "</p>"; |
| 129 | html += '<img src="http://earthquake.usgs.gov/eqcenter/mapplets/legend.png" alt="legend" />'; |
| 130 | html += "<h2>10 Most Recent Earthquakes</h2>"; |
| 131 | html += "<ol>"; |
| 132 | |
| 133 | // Loop through eq list (<item> nodes) |
| 134 | for (var i = 0; i < eqs.length; i++) { |
| 135 | |
| 136 | // For each eq, get child nodes |
| 137 | var item_nodes = eqs.item(i).childNodes; |
| 138 | |
| 139 | dcsubject_count = 1; |
| 140 | |
| 141 | // Loop through child nodes |
| 142 | for (var j = 0; j < item_nodes.length; j++) { |
| 143 | |
| 144 | node = item_nodes.item(j); |
| 145 | |
| 146 | switch (node.nodeName) { |
| 147 | case 'title': |
| 148 | title = node.firstChild.nodeValue; |
| 149 | break; |
| 150 | case 'description': |
| 151 | description = node.firstChild.nodeValue; |
| 152 | break; |
| 153 | case 'link': |
| 154 | link = node.firstChild.nodeValue; |
| 155 | break; |
| 156 | case 'geo:lat': |
| 157 | lat = parseFloat(node.firstChild.nodeValue); |
| 158 | break; |
| 159 | case 'geo:long': |
| 160 | long = parseFloat(node.firstChild.nodeValue); |
| 161 | break; |
| 162 | case 'dc:subject': |
| 163 | switch (dcsubject_count) { |
| 164 | case 3: //third dc:subject tag |
| 165 | var depth = node.firstChild.nodeValue; |
| 166 | break; |
| 167 | case 2: //second dc:subject tag |
| 168 | var age = node.firstChild.nodeValue; |
| 169 | dcsubject_count ++; |
| 170 | break; |
| 171 | case 1: //first dc:subject tag |
| 172 | var mag = node.firstChild.nodeValue; |
| 173 | dcsubject_count ++; |
| 174 | break; |
| 175 | } |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (i < 10) { // Show first 10 earthquakes in sidebar |
| 181 | html += '<li><a href="javascript:showBubble(' + i + ')" title="View earthquake on map">' + title + "</a></li>"; |
| 182 | } |
| 183 | |
| 184 | var bubble = '<div style=\"width:250px;\"><p><strong>' + title + '</strong><br />' + description + '</p><p><a href="' + link + '" target="_new">Earthquake Details</a> »</p></div>'; |
| 185 | var titletip = title; |
| 186 | var icon = getIcon(age, parseInt(mag)); |
| 187 | |
| 188 | createMarker(lat, long, icon, titletip, bubble, i); |
| 189 | |
| 190 | } |
| 191 | |
| 192 | html += "</ol>"; |
| 193 | |
| 194 | // Display HTML string in <div> |
| 195 | _gel('content').innerHTML = html; |
| 196 | |
| 197 | }, { refreshInterval: (60 * 5) }); // Allow Google to cache feed for only 5-minutes |
| 198 | } |
| 199 | |
| 200 | |
| 201 | // Create marker object for earthquake overlay |
| 202 | |
| 203 | function createMarker(lat, long, icon, titletip, bubble, count) { |
| 204 | var marker = new GMarker(new GLatLng(lat, long), {icon:icon, title:titletip}); |
| 205 | GEvent.addListener(marker, "click", function() { |
| 206 | marker.openInfoWindowHtml(bubble, {disableGoogleLinks:true}); |
| 207 | }); |
| 208 | map.addOverlay(marker); |
| 209 | markers[count] = marker; // Save for use in sidebar link |
| 210 | } |
| 211 | |
| 212 | |
| 213 | // Set icon properties based on age, magnitude |
| 214 | |
| 215 | function getIcon(age, mag) { |
| 216 | var ref = 'M' + mag + '-' + points.color[age]; |
| 217 | if (!icons[ref]) { |
| 218 | var center_pt = parseInt(points.size['M'+mag]/2); |
| 219 | var icon = new GIcon(); |
| 220 | icon.image = 'http://earthquake.usgs.gov/eqcenter/maps/images/' + ref + '.png'; |
| 221 | icon.iconSize = new GSize(points.size['M'+mag], points.size['M'+mag]); |
| 222 | icon.iconAnchor = new GPoint(center_pt, center_pt); |
| 223 | icon.infoWindowAnchor = new GPoint(center_pt, center_pt); |
| 224 | icons[ref] = icon; |
| 225 | } |
| 226 | return icons[ref]; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | // Show bubble window when eq clicked from sidebar |
| 231 | |
| 232 | function showBubble(i) { |
| 233 | GEvent.trigger(markers[i], "click"); |
| 234 | } |
| 235 | |
| 236 | _IG_RegisterOnloadHandler(initMap); |
| 237 | _IG_Analytics("UA-2265247-1", "/eqcenter/mapplets/earthquakes.xml"); |
| 238 | |
| 239 | </script> |
| 240 | |
| 241 | ]]></Content> |
| 242 | </Module> |
| 243 | }}} |