|
| 1 | +window.opentrace = { |
| 2 | + Hops: [], |
| 3 | + reset: function (hideMapPopup = false) { |
| 4 | + if (typeof gmap == "undefined") window.gmap = new google.maps.Map(document.getElementById("map"), { center: { lat: 0, lng: 0 }, zoom: 2 }); |
| 5 | + gmap.overlayMapTypes.clear(); |
| 6 | + this.Hops = []; |
| 7 | + this.hideMapPopup = hideMapPopup; |
| 8 | + if (document.getElementById("opentracePopup")) document.getElementById("opentracePopup").remove(); |
| 9 | + }, |
| 10 | + |
| 11 | + addHop: function (hop) { |
| 12 | + |
| 13 | + // Parse the JSON string into an object |
| 14 | + const hopData = JSON.parse(hop); |
| 15 | + |
| 16 | + console.log(hopData); |
| 17 | + |
| 18 | + // Clear any existing overlays |
| 19 | + gmap.overlayMapTypes.clear(); |
| 20 | + |
| 21 | + // Add the new hop to the list of hops |
| 22 | + this.Hops.push(hopData); |
| 23 | + |
| 24 | + // Loop through all hops and add markers |
| 25 | + for (let i = 0; i < this.Hops.length; i++) { |
| 26 | + if (this.Hops[i].Longitude == 0 || this.Hops[i].Latitude == 0 || this.Hops[i].Longitude == "" || this.Hops[i].Latitude == "") continue; |
| 27 | + let h = this.Hops[i]; |
| 28 | + let latLng = new google.maps.LatLng(h.Latitude, h.Longitude); |
| 29 | + let marker = new google.maps.Marker({ |
| 30 | + position: latLng, |
| 31 | + map: gmap, |
| 32 | + title: `#${h.No}: ${h.Geolocation}` |
| 33 | + }); |
| 34 | + let markerIndex = i; |
| 35 | + google.maps.event.addListener(marker, 'mouseover', () => { |
| 36 | + this.showPopup(markerIndex); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + // Connect the markers with a polyline |
| 41 | + let path = []; |
| 42 | + for (let i = 0; i < this.Hops.length; i++) { |
| 43 | + if (this.Hops[i].Longitude == 0 || this.Hops[i].Latitude == 0 || this.Hops[i].Longitude == "" || this.Hops[i].Latitude == "") continue; |
| 44 | + let h = this.Hops[i]; |
| 45 | + let latLng = new google.maps.LatLng(h.Latitude, h.Longitude); |
| 46 | + path.push(latLng); |
| 47 | + } |
| 48 | + let polyline = new google.maps.Polyline({ |
| 49 | + path: path, |
| 50 | + strokeColor: '#FF0000', |
| 51 | + strokeOpacity: 1.0, |
| 52 | + strokeWeight: 2 |
| 53 | + }); |
| 54 | + polyline.setMap(gmap); |
| 55 | + |
| 56 | + if (path.length > 1) { |
| 57 | + // Get the bounds of the path |
| 58 | + let bounds = new google.maps.LatLngBounds(); |
| 59 | + for (let i = 0; i < path.length; i++) { |
| 60 | + bounds.extend(path[i]); |
| 61 | + } |
| 62 | + // Fit the map to those bounds |
| 63 | + gmap.fitBounds(bounds); |
| 64 | + } |
| 65 | + }, |
| 66 | + |
| 67 | + focusHop: function (hopNo) { |
| 68 | + var hop = this.Hops[hopNo]; |
| 69 | + if (hop == null) return; |
| 70 | + this.showPopup(hopNo); |
| 71 | + if (hop.Longitude == 0 || hop.Latitude == 0 || hop.Longitude == "" || hop.Latitude == "") return; |
| 72 | + let hopPos = new google.maps.LatLng(hop.Latitude, hop.Longitude); |
| 73 | + gmap.setCenter(hopPos); |
| 74 | + }, |
| 75 | + |
| 76 | + showPopup: function (hopNo) { |
| 77 | + var hop = this.Hops[hopNo]; |
| 78 | + if (hop == null) return; |
| 79 | + if (document.getElementById("opentracePopup")) document.getElementById("opentracePopup").remove(); |
| 80 | + if (this.hideMapPopup == true) return; |
| 81 | + // 创建左上角的悬浮提示 |
| 82 | + var popupElement = document.createElement("div"); |
| 83 | + popupElement.id = "opentracePopup"; |
| 84 | + popupElement.style = |
| 85 | + "position: absolute; bottom: 10px; max-width: 33%; left: 10px; padding: 5px; background-color: rgba(255,255,255,0.85); border: 1px solid #ccc; border-radius: 5px; z-index: 9999;"; |
| 86 | + popupElement.innerHTML = ` |
| 87 | + <style> |
| 88 | + td:first-child { |
| 89 | + font-weight:bold; |
| 90 | + padding-right:5px; |
| 91 | + } |
| 92 | + </style> |
| 93 | + <table> |
| 94 | + <tr> |
| 95 | + <td style="white-space: nowrap;">IP #${hop.No}</td> |
| 96 | + <td>${hop.IP}</td> |
| 97 | + </tr> |
| 98 | + <tr> |
| 99 | + <td>Time</td> |
| 100 | + <td>${hop.Time}</td> |
| 101 | + </tr> |
| 102 | + <tr> |
| 103 | + <td>Geo</td> |
| 104 | + <td>${hop.Geolocation}</td> |
| 105 | + </tr> |
| 106 | + <tr> |
| 107 | + <td>Org</td> |
| 108 | + <td>${hop.Organization}</td> |
| 109 | + </tr> |
| 110 | + <tr> |
| 111 | + <td>AS</td> |
| 112 | + <td>${hop.AS}</td> |
| 113 | + </tr> |
| 114 | + </table>`; |
| 115 | + document.body.appendChild(popupElement); |
| 116 | + }, |
| 117 | +}; |
| 118 | + |
0 commit comments