文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

AndroidAPP开发KML轨迹导出教程示例

2022-12-09 12:00

关注

引言

前两天在知乎上面找海外骑行、跑步软件Strava的时候,看到一个将运动轨迹从A App 导出,导入到B APP的工具 APP RunGap,恰巧之前给台湾、印度那边的测试同事处理他们的问题时,写过这样的一个工具,KML文件导出,然后在Mac下的 Google 地球上看轨迹是否偏差,是否存在坐标类型的转化错误等问题,能够比较快地定位问题。

KML文件,读者有不知道的可以Google一下,它是一种专门存GPS 点数据的xml文件格式。

将以下这个数据结构 PathRecord类的GPS点 List 转换成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);                // 保存创建的xml
            xmlSerializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
            xmlSerializer.startDocument("utf-8", null);
            //给根节点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);                // 保存创建的xml
            xmlSerializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
            xmlSerializer.startDocument("utf-8", null);
            //给根节点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文件内容,其实还可以添加更多辅助信息到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 地球上,铛铛铛当就展现出来啦。

图1.2 Google地球展示kml文件

生成的kml标准文件,理论上可以导入到其它支持Share的APP,就可以进行数据迁移了。

以上就是Android APP开发KML轨迹导出教程示例的详细内容,更多关于Android APP导出KML轨迹的资料请关注编程网其它相关文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯