Android APP開發(fā)KML軌跡導(dǎo)出教程示例
引言
前兩天在知乎上面找海外騎行、跑步軟件Strava的時候,看到一個將運動軌跡從A App 導(dǎo)出,導(dǎo)入到B APP的工具 APP RunGap,恰巧之前給臺灣、印度那邊的測試同事處理他們的問題時,寫過這樣的一個工具,KML文件導(dǎo)出,然后在Mac下的 Google 地球上看軌跡是否偏差,是否存在坐標(biāo)類型的轉(zhuǎn)化錯誤等問題,能夠比較快地定位問題。
KML文件,讀者有不知道的可以Google一下,它是一種專門存GPS 點數(shù)據(jù)的xml文件格式。
將以下這個數(shù)據(jù)結(jié)構(gòu) PathRecord類的GPS點 List 轉(zhuǎn)換成xml:
圖 1.0 PathRecord
借用PathRecord的時間戳生成文件,存入SD卡,然后完文件里寫 List 點, 文件帶.kml 后綴。
圖1.1 生成KML文件
寫入kml gps點
public static String pullXMLCreate(String outDir, PathRecord pathRecord) { final String fileName = createKmlFile(outDir, pathRecord); StringWriter xmlWriter = new StringWriter(); try { // // 方式一:使用Android提供的實用工具類android.util.Xml // XmlSerializer xmlSerializer = Xml.newSerializer(); // 方式二:使用工廠類XmlPullParserFactory的方式 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlSerializer xmlSerializer = factory.newSerializer(); xmlSerializer.setOutput(xmlWriter); // 保存創(chuàng)建的xml xmlSerializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); xmlSerializer.startDocument("utf-8", null); //給根節(jié)點kml添加屬性 xmlSerializer.setPrefix("xmlns", "http://www.opengis.net/kml/2.2"); xmlSerializer.setPrefix("gx", "http://www.google.com/kml/ext/2.2");// <?xml version='1.0' encoding='UTF-8' standalone='yes' ?> xmlSerializer.startTag("", "kml"); xmlSerializer.startTag("", "Document"); xmlSerializer.startTag("", "Placemark"); xmlSerializer.startTag("", "Style"); xmlSerializer.startTag("", "LineStyle"); xmlSerializer.startTag(null, "color"); xmlSerializer.text("ff60ff00");//這里的顏色必須小寫 xmlSerializer.endTag(null, "color"); xmlSerializer.startTag(null, "width"); xmlSerializer.text("6"); xmlSerializer.endTag(null, "width"); xmlSerializer.endTag("", "LineStyle"); xmlSerializer.endTag("", "Style"); List<Gps> points = pathRecord.locationList; xmlSerializer.startTag(null, "LineString"); xmlSerializer.startTag(null, "coordinates"); xmlSerializer.text(createKMLPointStr(points)); xmlSerializer.endTag(null, "coordinates"); xmlSerializer.endTag(null, "LineString"); xmlSerializer.endTag("", "Placemark"); xmlSerializer.endTag("", "Document"); xmlSerializer.endTag("", "kml"); xmlSerializer.endDocument(); } catch (XmlPullParserException e) {// XmlPullParserFactory.newInstance e.printStackTrace(); } catch (IllegalArgumentException e) { // xmlSerializer.setOutput e.printStackTrace(); } catch (IllegalStateException e) { // xmlSerializer.setOutput e.printStackTrace(); } catch (IOException e) {// xmlSerializer.setOutput e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } String xmlStr = xmlWriter.toString(); BaseFileUtil.writeStringToFile(fileName, xmlStr); return xmlStr; }public static String pullXMLCreate(String outDir, PathRecord pathRecord) { final String fileName = createKmlFile(outDir, pathRecord); StringWriter xmlWriter = new StringWriter(); try { // // 方式一:使用Android提供的實用工具類android.util.Xml // XmlSerializer xmlSerializer = Xml.newSerializer(); // 方式二:使用工廠類XmlPullParserFactory的方式 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlSerializer xmlSerializer = factory.newSerializer(); xmlSerializer.setOutput(xmlWriter); // 保存創(chuàng)建的xml xmlSerializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); xmlSerializer.startDocument("utf-8", null); //給根節(jié)點kml添加屬性 xmlSerializer.setPrefix("xmlns", "http://www.opengis.net/kml/2.2"); xmlSerializer.setPrefix("gx", "http://www.google.com/kml/ext/2.2");// <?xml version='1.0' encoding='UTF-8' standalone='yes' ?> xmlSerializer.startTag("", "kml"); xmlSerializer.startTag("", "Document"); xmlSerializer.startTag("", "Placemark"); xmlSerializer.startTag("", "Style"); xmlSerializer.startTag("", "LineStyle"); xmlSerializer.startTag(null, "color"); xmlSerializer.text("ff60ff00");//這里的顏色必須小寫 xmlSerializer.endTag(null, "color"); xmlSerializer.startTag(null, "width"); xmlSerializer.text("6"); xmlSerializer.endTag(null, "width"); xmlSerializer.endTag("", "LineStyle"); xmlSerializer.endTag("", "Style"); List<Gps> points = pathRecord.locationList; xmlSerializer.startTag(null, "LineString"); xmlSerializer.startTag(null, "coordinates"); xmlSerializer.text(createKMLPointStr(points)); xmlSerializer.endTag(null, "coordinates"); xmlSerializer.endTag(null, "LineString"); xmlSerializer.endTag("", "Placemark"); xmlSerializer.endTag("", "Document"); xmlSerializer.endTag("", "kml"); xmlSerializer.endDocument(); } catch (XmlPullParserException e) {// XmlPullParserFactory.newInstance e.printStackTrace(); } catch (IllegalArgumentException e) { // xmlSerializer.setOutput e.printStackTrace(); } catch (IllegalStateException e) { // xmlSerializer.setOutput e.printStackTrace(); } catch (IOException e) {// xmlSerializer.setOutput e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } String xmlStr = xmlWriter.toString(); BaseFileUtil.writeStringToFile(fileName, xmlStr); return xmlStr; }
下面是生成的KML文件內(nèi)容,其實還可以添加更多輔助信息到KML文件的,這里就略去了,直接上軌跡點,超級簡單是不是?
奧森10km 軌跡圖
<?xml version='1.0' encoding='utf-8' ?> <kml xmlns:xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2"> <Document> <Placemark> <Style> <LineStyle> <color>ff60ff00</color> <width>6</width> </LineStyle> </Style> <LineString> <coordinates> 116.39348170420259,40.01190507680628 116.39450478868154,40.012104086029254 116.39481726475313,40.01208005324708 116.39511694229225,40.012009991578616 116.39580711364755,40.012061307700286 116.39665653452886,40.01188178384924 116.39678459102021,40.01180961243906 116.39697500459975,40.011471523134674 116.39698255521978,40.01139207409671 116.39687616848552,40.01126881996522 116.39665872919042,40.01115435968396 116.39654356043086,40.010968925696965 116.39653036511537,40.01087789762913 116.39672981870783,40.01052734936534 116.39679748281328,40.010473202401144 116.39692033865322,40.010438560155386 116.3970594302593,40.010440049486185 116.39828339172043,40.010934908063696 116.39863967684049,40.01092802652802 116.39877681138297,40.010968163634566 116.39884061062688,40.01101526404847 116.39949397735265,40.0116962557429 116.39977521437439,40.01226687974138 116.39979220644383,40.01244346676338 116.39962584946927,40.0140293256145 116.39972371868366,40.014678886558045 116.39971123165043,40.01485719187152 116.39974182919752,40.01538365587216 116.39986029319218,40.01568891586989 116.39984761902082,40.015855796079926 116.39970110475406,40.01616080496559 116.39949771774168,40.016405945115984 116.39898637007946,40.01675670390317 116.39884901289405,40.01702368683029 116.39881364730593,40.01733363430368 116.39885737654727,40.01767704020805 116.39896483813365,40.017832335703275 116.39920906138865,40.018005075484304 116.39977262893389,40.01828128985266 116.4001293621636,40.01854417073119 116.40024600409684,40.0187790824643 116.40025302343724,40.01904506378034 116.40017707385432,40.01930156925365 116.39972544746195,40.01994119262449 116.39953253408908,40.02013039614219 116.39945339405541,40.0203165783437 116.39938534895795,40.02059900688997 116.39932082475124,40.02067721242125 116.39712181065607,40.021466155528515 116.39632046855411,40.021754982909044 116.39588490369638,40.02183523343371 116.39575806415249,40.021825101555315 116.39536108668436,40.02172348883348 116.39460444986831,40.02177640361667 116.39376968562837,40.02165166688106 116.39329756827145,40.021515681327294 116.39273766645447,40.021472114042524 116.39265326821067,40.021494349557514 116.39242167784626,40.02175818736408 116.39207742840834,40.02219158932 116.39192142766977,40.02279171647535 116.39193652967609,40.02286602701059 116.3920743534718,40.023216753780545 116.39205810508224,40.02372507555831 116.39209734787802,40.0238278322195 116.39237426468247,40.024091182716106 116.3924143511756,40.02416918438945 116.3924137492412,40.024426061584705 116.39236005104893,40.02544972918901 116.39240701372273,40.025617222117184 116.39249862697335,40.02571240631482 116.39263106342825,40.02577377724534 116.39446638889551,40.02605859524811 116.39556776298676,40.02613580758347 116.39986655759832,40.02675503943772 116.4003386161378,40.026775097861645 116.40096447049662,40.02690883827291 116.40104995984294,40.026981077766145 116.4010789726778,40.0270805462986 116.40086147101938,40.0279336357706 116.40051182038502,40.028538691496564 116.39987329113715,40.029088053832275 116.39962006427466,40.02915865707772 116.39936196461215,40.029161070837674 116.39808724055626,40.02897535982718 116.3973751891038,40.028671500090525 116.39720199786572,40.028660897288304 116.39592890605488,40.028925593909754 116.3949549714303,40.02934906630121 116.39426274832482,40.029812589403946 116.39359856312939,40.030140254691155 116.39311998007548,40.03048394400153 116.39231593389374,40.030749786322964 116.39135698306774,40.03097689480288 116.39115736525918,40.030977533521146 116.3899641069909,40.030828225135366 116.38724540646109,40.03071910713684 116.38690936921729,40.03074932895987 116.38399932313702,40.03057491549396 116.382732199911,40.03028076317685 116.38232732000661,40.030273442065464 116.38178406586397,40.030347550190456 116.38004988336155,40.03029280366401 116.37868235429923,40.02999310215089 116.37781129913705,40.02941602190564 116.37740685573442,40.02900829910359 116.37716828025704,40.02863924607626 116.37710907935389,40.02841664526807 116.37711648422665,40.02796653249694 116.37761035005897,40.02720564002145 116.3778745522578,40.02661433982844 116.37792292072456,40.02635183106591 116.3779858111328,40.02627825292592 116.37803246374106,40.02625756508067 116.37865896305186,40.026307899488955 116.38380012351521,40.02664506513687 116.38459080353836,40.02654754473037 116.38511995328854,40.02655356352237 116.38539647713071,40.02649301625595 116.3861919918598,40.026104293086775 116.38654234571378,40.02600614611209 116.3866338093969,40.025957590924484 116.38672014682825,40.02583003774208 116.386740697838,40.025679200810224 116.38664415798256,40.025228954645016 116.38672106046344,40.02490401542579 116.3870002268534,40.02457311756562 116.38731333514906,40.02441831382311 116.38788048310829,40.02431032769093 116.3887489909213,40.023693376556196 116.38939818197309,40.02355381496369 116.38991352017877,40.02359576125264 116.39039377072802,40.023822207341794 116.39081846015574,40.0238742986498 116.39145986377808,40.02382136038134 116.39185339522352,40.02384709999533 116.3919177078651,40.02382293264885 116.39197144094236,40.02374542639035 116.39199927528868,40.023488792984324 116.39187416774469,40.02268095802571 116.39197886108005,40.022063071021684 116.39171654631168,40.02172855589577 116.39140278212987,40.02144381146428 116.39126100966841,40.021385122037486 116.39061960251743,40.02136014626356 116.38977723994557,40.02150844188777 116.38957680101326,40.0216013167516 116.38899516369777,40.02199609093532 116.38893513690786,40.022012503206525 116.38886896687934,40.02200998202478 116.38698200432371,40.02114631860147 116.38596860838122,40.020230854001255 116.38439567558511,40.01972331387241 116.38337920153049,40.01958496871191 116.38282185085899,40.0193637650231 116.3824242992586,40.01908289343984 116.38191190105081,40.01867549790632 116.38141602572355,40.018416172212554 116.38115991387069,40.018214148963764 116.38109009763927,40.01811210773365 116.38084115034782,40.01750139655885 116.38082895824479,40.01714392311205 116.38103503217111,40.01660707187263 116.38141557803576,40.01619777547733 116.38169056363917,40.01601114322713 116.3833226075677,40.01520028174043 116.3837791672884,40.01486717300946 116.38395244586614,40.014793776737235 116.38472978628388,40.01465909444237 116.38553764219313,40.014426595403116 116.38578952999165,40.01428578085227 116.38611016849502,40.01401904350985 116.3863572913299,40.013700150804 116.38688680798133,40.012687209893826 116.387643979247,40.012053604798794 116.38836363506562,40.011955982458204 116.38994930228544,40.012122471921515 116.39204826869613,40.01169536122755 116.3926362705122,40.011811400930924 116.39317629112587,40.011809775046174 116.39336336314635,40.0119145377369 116.39341299039299,40.01191266234115 116.39236427879013,40.01129695167011 116.39224654958639,40.011281305673876 116.39247366922483,40.01038227793181 </coordinates> </LineString> </Placemark> </Document> </kml>
直接將kml文件拖到Google 地球上,鐺鐺鐺當(dāng)就展現(xiàn)出來啦。
圖1.2 Google地球展示kml文件
生成的kml標(biāo)準(zhǔn)文件,理論上可以導(dǎo)入到其它支持Share的APP,就可以進(jìn)行數(shù)據(jù)遷移了。
以上就是Android APP開發(fā)KML軌跡導(dǎo)出教程示例的詳細(xì)內(nèi)容,更多關(guān)于Android APP導(dǎo)出KML軌跡的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Kotlin中的handler如何避免內(nèi)存泄漏詳解
Handler,我們已經(jīng)相當(dāng)熟悉了,而且經(jīng)常用得不亦樂乎,但就是因為太熟悉了,才會偶爾被它反捅一刀,血流不止,下面這篇文章主要給大家介紹了關(guān)于Kotlin中handler如何避免內(nèi)存泄漏的相關(guān)資料,需要的朋友可以參考下。2017-12-12Android應(yīng)用中Back鍵的監(jiān)聽及處理實例
在Android應(yīng)用中處理Back鍵按下事件,多種實現(xiàn)方法如下,感興趣的朋友可以了解下哈2013-06-06Android圖片的Base64編碼與解碼及解碼Base64圖片方法
Base64是網(wǎng)絡(luò)上最常見的用于傳輸8Bit字節(jié)碼的編碼方式之一,Base64就是一種基于64個可打印字符來表示二進(jìn)制數(shù)據(jù)的方法。接下來通過本文給大家分享Android圖片的Base64編碼與解碼及解碼Base64圖片,需要的朋友參考下吧2017-12-12Android設(shè)置PreferenceCategory背景顏色的方法
這篇文章主要介紹了Android設(shè)置PreferenceCategory背景顏色的方法,涉及Android設(shè)置背景色的技巧,需要的朋友可以參考下2015-05-05Android自定義view之圍棋動畫效果的實現(xiàn)
這篇文章主要介紹了Android自定義view之圍棋動畫效果的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式
本篇文章主要介紹了Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式,非常具有實用價值,需要的朋友可以參考下2017-09-09