如何解析文件geoxml3中的字符串?

问题描述 投票:0回答:1

我想解析这个文件:http://www.emsc-csem.org/Earthquake/Map/earth/kml.phpgeoxml3

我通过代理下载此文件,但我不知道如何解析字符串

例如,我想解析一个字符串:

<name> ..... </name> 
<description>....</description>

你能帮助我吗?

javascript jquery parsing xml-parsing geoxml3
1个回答
0
投票

要使用[geoxml3](https://github.com/geocodezip/geoxml3)解析字符串(包含KML),请使用解析器的.parseKmlString方法。

map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
geoXml = new geoXML3.parser({
  map: map,
  singleInfoWindow: true
});
geoXml.parseKmlString(kmlData); // kmlData contains a string containing valid KML

proof of concept fiddle

screenshot of resulting map

代码段:

var geoxml = null;

function initialize() {
  var myLatlng = new google.maps.LatLng(39.397, -100.644);
  var myOptions = {
    zoom: 5,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
  geoXml = new geoXML3.parser({
    map: map,
    singleInfoWindow: true
  });
  geoXml.parseKmlString(kmlData);
}
google.maps.event.addDomListener(window, "load", initialize);
var kmlData = '<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2"><Document><name>EMSC - Last 2 Weeks earthquakes worldwide</name><Folder><name>2017 12 - 6</name><open>0</open><visibility>1</visibility><Placemark><name>ML 2.3 - SOUTHERN CALIFORNIA</name><description><![CDATA[ <h2>ML 2.3 - SOUTHERN CALIFORNIA</h2><br><b>2017-12-06   16:37:55.1 UTC</b><br /><br /><table style="width:100%;"><tr><td><b>Latitude</b></td><td style="padding-left:5px;">34.60 N<br></td></tr><tr><td><b>Longitude</b></td><td style="padding-left:5px;">116.30 W<br></td></tr><tr><td><b>Depth</b></td><td style="padding-left:5px;">1   Km<br></td></tr><tr><td><b>Magnitude</b></td><td style="padding-left:5px;">ML 2.3<br></td></tr></table><br /><br /><a href="https://www.emsc-csem.org/Earthquake/earthquake.php?id=634406">Link to the event </a>]]></description><Snippet maxLines="0"></Snippet><styleUrl>#rouge</styleUrl><Style><IconStyle><scale>0.25</scale></IconStyle></Style><Point><altitudeMode>absolute</altitudeMode><coordinates>-116.3006,34.5925,0</coordinates></Point><LookAt><longitude>-116.3006</longitude><latitude>34.5925</latitude><range>1000000</range></LookAt></Placemark><Placemark><name>ML 2.0 - GREECE</name><description><![CDATA[ <h2>ML 2.0 - GREECE</h2><br><b>2017-12-06   10:21:54.5 UTC</b><br /><br /><table style="width:100%;"><tr><td><b>Latitude</b></td><td style="padding-left:5px;">40.51 N<br></td></tr><tr><td><b>Longitude</b></td><td style="padding-left:5px;">22.53 E<br></td></tr><tr><td><b>Depth</b></td><td style="padding-left:5px;">4   Km<br></td></tr><tr><td><b>Magnitude</b></td><td style="padding-left:5px;">ML 2.0<br></td></tr></table><br /><br /><a href="https://www.emsc-csem.org/Earthquake/earthquake.php?id=634358">Link to the event </a>]]></description><Snippet maxLines="0"></Snippet><styleUrl>#rouge</styleUrl><Style><IconStyle><scale>0.25</scale></IconStyle></Style><Point><altitudeMode>absolute</altitudeMode><coordinates>22.5358,40.5114,0</coordinates></Point><LookAt><longitude>22.5358</longitude><latitude>40.5114</latitude><range>1000000</range></LookAt></Placemark><Placemark><name>M 3.6 - OAXACA, MEXICO</name><description><![CDATA[ <h2>M 3.6 - OAXACA, MEXICO</h2><br><b>2017-12-06   10:19:47.0 UTC</b><br /><br /><table style="width:100%;"><tr><td><b>Latitude</b></td><td style="padding-left:5px;">16.64 N<br></td></tr><tr><td><b>Longitude</b></td><td style="padding-left:5px;">94.91 W<br></td></tr><tr><td><b>Depth</b></td><td style="padding-left:5px;">15   Km<br></td></tr><tr><td><b>Magnitude</b></td><td style="padding-left:5px;">M 3.6<br></td></tr></table><br /><br /><a href="https://www.emsc-csem.org/Earthquake/earthquake.php?id=634392">Link to the event </a>]]></description><Snippet maxLines="0"></Snippet><styleUrl>#rouge</styleUrl><Style><IconStyle><scale>0.5</scale></IconStyle></Style><Point><altitudeMode>absolute</altitudeMode><coordinates>-94.9192,16.6313,0</coordinates></Point><LookAt><longitude>-94.9192</longitude><latitude>16.6313</latitude><range>1000000</range></LookAt></Placemark><Placemark><name>M 3.5 - COSTA RICA</name><description><![CDATA[ <h2>M 3.5 - COSTA RICA</h2><br><b>2017-11-22   18:03:30.0 UTC</b><br /><br /><table style="width:100%;"><tr><td><b>Latitude</b></td><td style="padding-left:5px;">9.50 N<br></td></tr><tr><td><b>Longitude</b></td><td style="padding-left:5px;">84.57 W<br></td></tr><tr><td><b>Depth</b></td><td style="padding-left:5px;">15   Km<br></td></tr><tr><td><b>Magnitude</b></td><td style="padding-left:5px;">M 3.5<br></td></tr></table><br /><br /><a href="https://www.emsc-csem.org/Earthquake/earthquake.php?id=631799">Link to the event </a>]]></description><Snippet maxLines="0"></Snippet><styleUrl>#rouge</styleUrl><Style><IconStyle><scale>0.5</scale></IconStyle></Style><Point><altitudeMode>absolute</altitudeMode><coordinates>-84.5601,9.501,0</coordinates></Point><LookAt><longitude>-84.5601</longitude><latitude>9.501</latitude><range>1000000</range></LookAt></Placemark><Placemark><name>mb 4.3 - FOX ISLANDS, ALEUTIAN ISLANDS</name><description><![CDATA[ <h2>mb 4.3 - FOX ISLANDS, ALEUTIAN ISLANDS</h2><br><b>2017-11-22   17:38:20.4 UTC</b><br /><br /><table style="width:100%;"><tr><td><b>Latitude</b></td><td style="padding-left:5px;">53.81 N<br></td></tr><tr><td><b>Longitude</b></td><td style="padding-left:5px;">165.56 W<br></td></tr><tr><td><b>Depth</b></td><td style="padding-left:5px;">65   Km<br></td></tr><tr><td><b>Magnitude</b></td><td style="padding-left:5px;">mb 4.3<br></td></tr></table><br /><br /><a href="https://www.emsc-csem.org/Earthquake/earthquake.php?id=631805">Link to the event </a>]]></description><Snippet maxLines="0"></Snippet><styleUrl>#jaune</styleUrl><Style><IconStyle><scale>0.5</scale></IconStyle></Style><Point><altitudeMode>absolute</altitudeMode><coordinates>-165.5595,53.81,0</coordinates></Point><LookAt><longitude>-165.5595</longitude><latitude>53.81</latitude><range>1000000</range></LookAt></Placemark><Placemark><name>ML 3.2 - ALBANIA</name><description><![CDATA[ <h2>ML 3.2 - ALBANIA</h2><br><b>2017-11-22   17:08:15.6 UTC</b><br /><br /><table style="width:100%;"><tr><td><b>Latitude</b></td><td style="padding-left:5px;">40.35 N<br></td></tr><tr><td><b>Longitude</b></td><td style="padding-left:5px;">20.57 E<br></td></tr><tr><td><b>Depth</b></td><td style="padding-left:5px;">5 f Km<br></td></tr><tr><td><b>Magnitude</b></td><td style="padding-left:5px;">ML 3.2<br></td></tr></table><br /><br /><a href="https://www.emsc-csem.org/Earthquake/earthquake.php?id=631793">Link to the event </a>]]></description><Snippet maxLines="0"></Snippet><styleUrl>#rouge</styleUrl><Style><IconStyle><scale>0.375</scale></IconStyle></Style><Point><altitudeMode>absolute</altitudeMode><coordinates>20.5607,40.3405,0</coordinates></Point><LookAt><longitude>20.5607</longitude><latitude>40.3405</latitude><range>1000000</range></LookAt></Placemark><Placemark><name>ML 3.8 - ANTOFAGASTA, CHILE</name><description><![CDATA[ <h2>ML 3.8 - ANTOFAGASTA, CHILE</h2><br><b>2017-11-22   17:00:52.0 UTC</b><br /><br /><table style="width:100%;"><tr><td><b>Latitude</b></td><td style="padding-left:5px;">22.50 S<br></td></tr><tr><td><b>Longitude</b></td><td style="padding-left:5px;">69.00 W<br></td></tr><tr><td><b>Depth</b></td><td style="padding-left:5px;">108   Km<br></td></tr><tr><td><b>Magnitude</b></td><td style="padding-left:5px;">ML 3.8<br></td></tr></table><br /><br /><a href="https://www.emsc-csem.org/Earthquake/earthquake.php?id=631792">Link to the event </a>]]></description><Snippet maxLines="0"></Snippet><styleUrl>#vert</styleUrl><Style><IconStyle><scale>0.5</scale></IconStyle></Style><Point><altitudeMode>absolute</altitudeMode><coordinates>-68.9908,-22.4948,0</coordinates></Point><LookAt><longitude>-68.9908</longitude><latitude>-22.4948</latitude><range>1000000</range></LookAt></Placemark><Placemark><name>ML 2.1 - NORTHERN ITALY</name><description><![CDATA[ <h2>ML 2.1 - NORTHERN ITALY</h2><br><b>2017-11-22   16:55:07.0 UTC</b><br /><br /><table style="width:100%;"><tr><td><b>Latitude</b></td><td style="padding-left:5px;">44.65 N<br></td></tr><tr><td><b>Longitude</b></td><td style="padding-left:5px;">10.04 E<br></td></tr><tr><td><b>Depth</b></td><td style="padding-left:5px;">26   Km<br></td></tr><tr><td><b>Magnitude</b></td><td style="padding-left:5px;">ML 2.1<br></td></tr></table><br /><br /><a href="https://www.emsc-csem.org/Earthquake/earthquake.php?id=631788">Link to the event </a>]]></description><Snippet maxLines="0"></Snippet><styleUrl>#rouge</styleUrl><Style><IconStyle><scale>0.25</scale></IconStyle></Style><Point><altitudeMode>absolute</altitudeMode><coordinates>10.0312,44.6493,0</coordinates></Point><LookAt><longitude>10.0312</longitude><latitude>44.6493</latitude><range>1000000</range></LookAt></Placemark><Placemark><name>mb 4.6 - SUMBA REGION, INDONESIA</name><description><![CDATA[ <h2>mb 4.6 - SUMBA REGION, INDONESIA</h2><br><b>2017-11-22   16:53:10.7 UTC</b><br /><br /><table style="width:100%;"><tr><td><b>Latitude</b></td><td style="padding-left:5px;">10.56 S<br></td></tr><tr><td><b>Longitude</b></td><td style="padding-left:5px;">120.30 E<br></td></tr><tr><td><b>Depth</b></td><td style="padding-left:5px;">40   Km<br></td></tr><tr><td><b>Magnitude</b></td><td style="padding-left:5px;">mb 4.6<br></td></tr></table><br /><br /><a href="https://www.emsc-csem.org/Earthquake/earthquake.php?id=631789">Link to the event </a>]]></description><Snippet maxLines="0"></Snippet><styleUrl>#jaune</styleUrl><Style><IconStyle><scale>0.625</scale></IconStyle></Style><Point><altitudeMode>absolute</altitudeMode><coordinates>120.3089,-10.551,0</coordinates></Point><LookAt><longitude>120.3089</longitude><latitude>-10.551</latitude><range>1000000</range></LookAt></Placemark></Folder><Style id="inactive_rouge"><IconStyle><Icon><href>https://static2.emsc.eu/Images/evt/c8-rouge.png</href></Icon></IconStyle><LabelStyle><scale>0</scale></LabelStyle><BalloonStyle><text><![CDATA[<font face="Verdana"><table width="400"><tr><td><img src="https://www.emsc-csem.org/Css/img/logo.png" alt="EMSC" width="150" height="82" /><br/>$[description]</td></tr></table></font>]]></text></BalloonStyle></Style><Style id="active_rouge"><IconStyle><Icon><href>https://static2.emsc.eu/Images/evt/c8-rouge.png</href></Icon></IconStyle><LabelStyle><scale>1</scale></LabelStyle><BalloonStyle><text><![CDATA[<font face="Verdana"><table width="400"><tr><td><img src="https://www.emsc-csem.org/Css/img/logo.png" alt="EMSC" width="150" height="82" /><br/>$[description]</td></tr></table></font>]]></text></BalloonStyle></Style><Style id="inactive_jaune"><IconStyle><Icon><href>https://static3.emsc.eu/Images/evt/c8-jaune.png</href></Icon></IconStyle><LabelStyle><scale>0</scale></LabelStyle><BalloonStyle><text><![CDATA[<font face="Verdana"><table width="400"><tr><td><img src="https://www.emsc-csem.org/Css/img/logo.png" alt="EMSC" width="150" height="82" /><br/>$[description]</td></tr></table></font>]]></text></BalloonStyle></Style><Style id="active_jaune"><IconStyle><Icon><href>https://static3.emsc.eu/Images/evt/c8-jaune.png</href></Icon></IconStyle><LabelStyle><scale>1</scale></LabelStyle><BalloonStyle><text><![CDATA[<font face="Verdana"><table width="400"><tr><td><img src="https://www.emsc-csem.org/Css/img/logo.png" alt="EMSC" width="150" height="82" /><br/>$[description]</td></tr></table></font>]]></text></BalloonStyle></Style><Style id="inactive_vert"><IconStyle><Icon><href>https://static1.emsc.eu/Images/evt/c8-vert.png</href></Icon></IconStyle><LabelStyle><scale>0</scale></LabelStyle><BalloonStyle><text><![CDATA[<font face="Verdana"><table width="400"><tr><td><img src="https://www.emsc-csem.org/Css/img/logo.png" alt="EMSC" width="150" height="82" /><br/>$[description]</td></tr></table></font>]]></text></BalloonStyle></Style><Style id="active_vert"><IconStyle><Icon><href>https://static1.emsc.eu/Images/evt/c8-vert.png</href></Icon></IconStyle><LabelStyle><scale>1</scale></LabelStyle><BalloonStyle><text><![CDATA[<font face="Verdana"><table width="400"><tr><td><img src="https://www.emsc-csem.org/Css/img/logo.png" alt="EMSC" width="150" height="82" /><br/>$[description]</td></tr></table></font>]]></text></BalloonStyle></Style><Style id="inactive_bleu"><IconStyle><Icon><href>https://static2.emsc.eu/Images/evt/c8-bleu.png</href></Icon></IconStyle><LabelStyle><scale>0</scale></LabelStyle><BalloonStyle><text><![CDATA[<font face="Verdana"><table width="400"><tr><td><img src="https://www.emsc-csem.org/Css/img/logo.png" alt="EMSC" width="150" height="82" /><br/>$[description]</td></tr></table></font>]]></text></BalloonStyle></Style><Style id="active_bleu"><IconStyle><Icon><href>https://static2.emsc.eu/Images/evt/c8-bleu.png</href></Icon></IconStyle><LabelStyle><scale>1</scale></LabelStyle><BalloonStyle><text><![CDATA[<font face="Verdana"><table width="400"><tr><td><img src="https://www.emsc-csem.org/Css/img/logo.png" alt="EMSC" width="150" height="82" /><br/>$[description]</td></tr></table></font>]]></text></BalloonStyle></Style><Style id="inactive_violet"><IconStyle><Icon><href>https://static3.emsc.eu/Images/evt/c8-violet.png</href></Icon></IconStyle><LabelStyle><scale>0</scale></LabelStyle><BalloonStyle><text><![CDATA[<font face="Verdana"><table width="400"><tr><td><img src="https://www.emsc-csem.org/Css/img/logo.png" alt="EMSC" width="150" height="82" /><br/>$[description]</td></tr></table></font>]]></text></BalloonStyle></Style><Style id="active_violet"><IconStyle><Icon><href>https://static3.emsc.eu/Images/evt/c8-violet.png</href></Icon></IconStyle><LabelStyle><scale>1</scale></LabelStyle><BalloonStyle><text><![CDATA[<font face="Verdana"><table width="400"><tr><td><img src="https://www.emsc-csem.org/Css/img/logo.png" alt="EMSC" width="150" height="82" /><br/>$[description]</td></tr></table></font>]]></text></BalloonStyle></Style><StyleMap id="rouge"><Pair><key>normal</key><styleUrl>#inactive_rouge</styleUrl></Pair><Pair><key>highlight</key><styleUrl>#active_rouge</styleUrl></Pair></StyleMap><StyleMap id="jaune"><Pair><key>normal</key><styleUrl>#inactive_jaune</styleUrl></Pair><Pair><key>highlight</key><styleUrl>#active_jaune</styleUrl></Pair></StyleMap><StyleMap id="vert"><Pair><key>normal</key><styleUrl>#inactive_vert</styleUrl></Pair><Pair><key>highlight</key><styleUrl>#active_vert</styleUrl></Pair></StyleMap><StyleMap id="bleu"><Pair><key>normal</key><styleUrl>#inactive_bleu</styleUrl></Pair><Pair><key>highlight</key><styleUrl>#active_bleu</styleUrl></Pair></StyleMap><StyleMap id="violet"><Pair><key>normal</key><styleUrl>#inactive_violet</styleUrl></Pair><Pair><key>highlight</key><styleUrl>#active_violet</styleUrl></Pair></StyleMap><ScreenOverlay><name>EMSC Legend</name><color>ccffffff</color><Icon><href>https://static1.emsc.eu/Images/legendge.png</href></Icon><overlayXY x="0" y="0" xunits="fraction" yunits="fraction"/><screenXY x="0.01" y="0.02" xunits="fraction" yunits="fraction"/></ScreenOverlay><ScreenOverlay><name>EMSC Logo</name><color>ccffffff</color><Icon><href>https://static2.emsc.eu/Images/logoge.png</href></Icon><overlayXY x="0" y="1" xunits="fraction" yunits="fraction"/><screenXY x="0.5" y="0.99" xunits="fraction" yunits="fraction"/></ScreenOverlay></Document></kml>';
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://cdn.jsdelivr.net/gh/geocodezip/geoxml3@master/polys/geoxml3.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map_canvas"></div>
© www.soinside.com 2019 - 2024. All rights reserved.