我正在尝试解析作为字符串传递的 KML 文件,并将其加载到谷歌地图上。当我从 geoxml站点示例中获取一些 KML 时,它工作正常,请参见“kmlString1”,但是当我尝试使用自己提取的值时失败,尽管我似乎看不出差异并且错误是模糊的。
我的地图代码:
geoMap = new google.maps.Map(document.getElementById('map-canvas'), myOptions);
geoXMLParser = new geoXML3.parser({ map: geoMap });
geoXMLParser.parseKmlString(kmlString1);
google.maps.event.trigger(geoMap, 'resize');
KML 字符串 1(有效):
var kmlString1 = '<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.1"><Document><name>Chicago Transit Map</name><description>Chicago Transit Authority train lines</description><Style id="greenLine"><LineStyle><color>ff009900</color><width>4</width></LineStyle></Style><Placemark><name>Green Line</name><styleUrl>#greenLine</styleUrl><LineString><altitudeMode>relative</altitudeMode><coordinates>-87.8049498796463,41.8868887424469
-87.79437124729156,41.88703894615173 -87.78372824192047,41.88716769218445 -87.77429759502411,41.88731789588928 -87.76573598384857,41.88741445541382 -87.75802195072174,41.887521743774414 -87.75767862796783,41.887532472610474 -87.75741040706635,41.887500286102295 -87.7571851015091,41.887478828430176 -87.75661647319794,41.88741445541382 -87.75600492954254,41.88729643821716 -87.75571525096893,41.887232065200806 -87.75543630123138,41.88719987869263 -87.75520026683807,41.88717842102051 -87.75498569011688,41.88715696334839 -87.75451362133026,41.88712477684021 -87.7461451292038,41.88667416572571 -87.74589836597443,41.88666343688965 -87.74570524692535,41.88665270805359 -87.74551212787628,41.88662052154541 -87.74530827999115,41.88656687736511 -87.7445787191391,41.886523962020874 -87.725657,41.885483 -87.71686613559723,41.884968280792236 -87.706155,41.884332 -87.70189940929413,41.88412070274353 -87.70147025585175,41.88408851623535 -87.70112693309784,41.88409924507141 -87.70066559314728,41.88414216041565 -87.696354,41.884232 -87.666934,41.885396 -87.64175355434418,41.88570857048035 -87.63107299804688,41.885762214660645 -87.628083,41.885843 -87.6265561580658,41.885772943496704 -87.62641668319702,41.885730028152466 -87.62629866600037,41.88565492630005 -87.62625575065613,41.885504722595215 -87.626343,41.884609 -87.626292,41.882163 -87.626157,41.879559 -87.62608408927917,41.8768572807312 -87.62604117393494,41.87461495399475 -87.62613773345947,41.874486207962036 -87.62627720832825,41.87443256378174 -87.62649178504944,41.87443256378174 -87.62665271759033,41.8743896484375 -87.62676000595093,41.874271631240845 -87.6268458366394,41.87411069869995 -87.62665271759033,41.86731934547424 -87.625850,41.831033 -87.62565493583679,41.82229042053223 -87.6256012916565,41.822108030319214 -87.62548327445984,41.82196855545044 -87.62529015541077,41.821861267089844 -87.62503266334534,41.821796894073486 -87.62476444244385,41.82178616523743 -87.621551156044,41.821850538253784 -87.6198399066925,41.821850538253784 -87.61965751647949,41.821829080581665 -87.61948585510254,41.82174324989319 -87.61931419372559,41.821603775024414 -87.61922836303711,41.82144284248352 -87.61921763420105,41.82119607925415 -87.61906743049622,41.81662559509277 -87.61889576911926,41.809319257736206 -87.61878848075867,41.80673360824585 -87.61874556541443,41.80654048919678 -87.61868119239807,41.80626153945923 -87.61865973472595,41.80602550506592 -87.61857390403748,41.80204510688782 -87.61838,41.794272 -87.61819839477539,41.789363622665405 -87.61801600456238,41.7805016040802 -87.6179838180542,41.780362129211426 -87.61791944503784,41.78027629852295 -87.61780142784119,41.78022265434265 -87.61762976646423,41.78019046783447 -87.61593461036682,41.78025484085083 -87.606048,41.780334 -87.61593461036682,41.78025484085083 -87.61762976646423,41.78019046783447 -87.61780142784119,41.78022265434265 -87.61791944503784,41.78027629852295 -87.6179838180542,41.780362129211426 -87.61801600456238,41.7805016040802 -87.61819839477539,41.789363622665405 -87.61838,41.794272 -87.61819839477539,41.789363622665405 -87.61818766593933,41.787400245666504 -87.61824131011963,41.78729295730591 -87.61829495429993,41.78718566894531 -87.61840224266052,41.787099838256836 -87.61852025985718,41.78702473640442 -87.61868119239807,41.7870032787323 -87.6188313961029,41.78699254989624 -87.63068675994873,41.78682088851929 -87.63105154037476,41.78682088851929 -87.63129830360413,41.78679943084717 -87.63153433799744,41.78675651550293 -87.63169527053833,41.78667068481445 -87.63181328773499,41.78656339645386 -87.63190984725952,41.78647756576538 -87.63200640678406,41.786370277404785 -87.63207077980042,41.78619861602783 -87.63208150863647,41.785898208618164 -87.63207077980042,41.78076982498169 -87.63207077980042,41.780608892440796 -87.63207077980042,41.78049087524414 -87.63211369514465,41.780383586883545 -87.63215661048889,41.78029775619507 -87.63222098350525,41.78021192550659 -87.63312220573425,41.77951455116272 -87.63318657875061,41.77945017814636 -87.6333475112915,41.779385805130005 -87.6335620880127,41.77932143211365 -87.63384103775024,41.77931070327759 -87.63548254966736,41.77921414375305 -87.6449453830719,41.77903175354004 -87.64538526535034,41.77905321121216 -87.64556765556335,41.77908539772034 -87.64607191085815,41.77918195724487 -87.64630794525146,41.77923560142517 -87.664416,41.778970 </coordinates></LineString></Placemark></Document></kml>';
KML 2(失败):
<?xml version="1.0" encoding="UTF-8"?><?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" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom"><Document><name>My KML</name><open>1</open><Style id="30"><IconStyle><color>7fff3300</color><Icon></Icon></IconStyle><LineStyle><color>7f0000ff</color></LineStyle><PolyStyle><color>7fff3300</color></PolyStyle></Style><StyleMap id="3"><Pair><key>normal</key><styleUrl>#30</styleUrl></Pair><Pair><key>highlight</key><styleUrl>#31</styleUrl></Pair></StyleMap><Style id="31"><IconStyle><color>7fff3300</color><Icon></Icon></IconStyle><LineStyle><color>7f0000ff</color></LineStyle><PolyStyle><color>7fff3300</color></PolyStyle></Style><Placemark><name>KML placemark</name><styleUrl>#3</styleUrl><MultiGeometry><Point><coordinates>-112.01256377417,41.5072606265432,0</coordinates></Point><Polygon><outerBoundaryIs><LinearRing><tessellate>1</tessellate><coordinates>-112.07027,41.56417,0 -112.06117,41.5644,0 -112.05986,41.54304,0 -112.05498,41.54181,0 -112.05491,41.53933,0 -112.05613,41.53895,0 -112.05936,41.53422,0 -112.05671,41.53371,0 -112.03522,41.52959,0 -112.0426846109771,41.35927998389626,0 -112.0365321250359,41.34290347582947,0 -112.0326426316012,41.35062378545741,0 -112.0352127974503,41.36054453265797,0 -112.0304787575574,41.38568017766292,0 -112.0277746639736,41.41418975342698,0 -112.013922763087,41.48592411971617,0 -111.9703671344106,41.52842914945973,0 -111.9968962302691,41.5585848842676,0 -111.9950938533972,41.56263138377672,0 -111.9946639618171,41.56571005197098,0 -111.9957853281415,41.56955799380076,0 -111.9986040833722,41.57197591841132,0 -111.9990961877412,41.5783882360938,0 -112.0020297179709,41.58510362540922,0 -112.0059311436483,41.59042659385794,0 -112.0074182264176,41.60028570553145,0 -112.0046996397476,41.60773902647082,0 -112.0041733317747,41.61289025309328,0 -112.0062603853204,41.61479752005407,0 -112.0059198089366,41.62005229857395,0 -112.0080174997886,41.62335969934363,0 -112.0071885708408,41.62730955878394,0 -112.0050706283366,41.62919181090332,0 -112.0055717306615,41.63317982067965,0 -112.013389886181,41.63560375118057,0 -112.0160692905661,41.64220218855945,0 -112.0122358129207,41.64855870380178,0 -112.0163801613532,41.65982249901096,0 -112.0269935811944,41.67094870061529,0 -112.0298829330745,41.68197265639441,0 -112.0362598757643,41.6834287939119,0 -112.0455534304689,41.68637402554889,0 -112.0518901532527,41.69854793608697,0 -112.0509199046411,41.70526778825629,0 -112.044377247211,41.70922929304604,0 -112.0442713581888,41.71404286665,0 -112.0443060457553,41.73239994805866,0 -112.0409604574352,41.7399722918305,0 -112.0354912570731,41.74812651559856,0 -112.0311921302564,41.75720886754628,0 -112.0310311209535,41.75804321166538,0 -112.0311766687799,41.75817988198256,0 -112.0321038394212,41.76305882309595,0 -112.0316924628234,41.76813192032779,0 -112.0284756204678,41.7712009470295,0 -112.0266175077556,41.77380930137499,0 -112.026963317656,41.77587724666508,0 -112.0194788819137,41.78012578895596,0 -112.0147999814896,41.78015167444554,0 -112.0108334838747,41.78659711386337,0 -112.0099097758721,41.79144322189291,0 -112.0093879444815,41.79573308968118,0 -112.0180658061009,41.8036734751649,0 -112.0226007780891,41.80525360324329,0 -112.0303095937943,41.81155224279044,0 -112.0295057720067,41.81764770590057,0 -112.0527400223457,41.86508229565804,0 -112.0680276442323,41.88025344265011,0 -112.0824442122287,41.89867550494549,0 -112.1256376185121,41.92600086673807,0 -112.2298237999514,41.91468721708712,0 -112.1996652582874,41.87291264940141,0 -112.1900616792923,41.8342823743127,0 -112.1895092775782,41.76822875788476,0 -112.2035636686155,41.73726768166225,0 -112.2309880901628,41.73564704817329,0 -112.2656792819361,41.78740907882889,0 -112.3120297573393,41.7886217083115,0 -112.2967860244342,41.75534820470151,0 -112.296344296865,41.71103094529849,0 -112.2991703300951,41.67750399816768,0 -112.295390359434,41.65146852743963,0 -112.2915231054029,41.63973443957563,0 -112.2725045143609,41.63069785644005,0 -112.2306640495189,41.62307203027278,0 -112.2160878858202,41.61448540620161,0 -112.2155431506392,41.57415800811948,0 -112.107820158346,41.55038353538438,0 -112.0827589054261,41.54000730467362,0 -112.06633,41.53559,0 -112.06548,41.53536,0 -112.06468,41.53524,0 -112.06398,41.53721,0 -112.07027,41.56417,0 </coordinates></LinearRing></outerBoundaryIs><innerBoundaryIs><LinearRing><tessellate>1</tessellate><coordinates>-112.05537,41.50514,0 -112.05558,41.50434,0 -112.05711,41.50354,0 -112.05765,41.50576,0 -112.05607,41.5075,0 -112.05412,41.50598,0 -112.05537,41.50514,0 </coordinates></LinearRing></innerBoundaryIs></Polygon></MultiGeometry></Placemark></Document></kml>
完全错误:
common.js:151 Uncaught TypeError: Cannot read property 'x' of undefined
at Iv (common.js:151)
at _.Av._.k.Pi (common.js:210)
at Object._.A.trigger (js?v=3.exp&sensor=false&key=AIzaSyCU25UJR3thgc29E7FSiS18oomSrSdfq2c:99)
at ny (map.js:13)
at _.Sv.<anonymous> (map.js:13)
at Object._.A.trigger (js?v=3.exp&sensor=false&key=AIzaSyCU25UJR3thgc29E7FSiS18oomSrSdfq2c:99)
at _.Sv._.k.Pl (common.js:219)
at Object._.A.trigger (js?v=3.exp&sensor=false&key=AIzaSyCU25UJR3thgc29E7FSiS18oomSrSdfq2c:99)
at jq.<anonymous> (common.js:133)
at Object._.A.trigger (js?v=3.exp&sensor=false&key=AIzaSyCU25UJR3thgc29E7FSiS18oomSrSdfq2c:99)
有限制parseKmlString
吗?该文件加载得很好, geoXMLParser.parse("path/to/file")
所以我很困惑为什么它会因我需要使用的字符串值而失败。