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