如何在 google map fragment android studio 中导入 KML 多边形文件

How to import a KML polygon file in google map fragment android studio

这是我的第一个问题,请耐心等待。

我正在研究 java 以进行 Android 开发,现在我被一个问题困住了几个星期:

我无法在我的应用程序中导入简单的 KML 文件!

我尝试了 kmlLayer,我将文件作为输入流放入我的设备中,但没有任何效果。 (使用将字符串(我的文件在我的设备中的位置)转换为编码为 "utf-8" 的 InputStream 的代码)

伙计们,我该如何导入这个多边形 .kml?

<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="root_doc">
<Schema name="Extra_31_168_366" id="Extra_31_168_366">
    <SimpleField name="Name" type="string"></SimpleField>
</Schema>
<Folder><name>Extra_31_168_366</name>
  <Placemark>
    <name>Zona Extra a levantar</name>
    <Style><LineStyle><color>FF232323</color><width>0.737006</width></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
      <MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>1.04761446057425,43.1864681306065 1.04740371376916,43.186733854839 1.04859489136319,43.1875310275366 1.04966695119782,43.1877875888645 1.05063821908219,43.1884015034707 1.0513254369249,43.1888596486991 1.05327713559821,43.1889879293631 1.05492645842072,43.1892170019774 1.05634670862899,43.1890520696951 1.05765700398243,43.1895835181601 1.05871073800792,43.1907655328496 1.05776695883726,43.1951179125201 1.06452918240956,43.1965839772512 1.06447420498214,43.195631035176 1.06418099203592,43.1933403090336 1.06093732381831,43.1920391765848 1.05740044265448,43.1885939244666 1.05425298493486,43.18868097206 1.05134834418633,43.1879479396945 1.04761446057425,43.1864681306065</coordinates></LinearRing></outerBoundaryIs></Polygon></MultiGeometry>
  </Placemark>
</Folder>
</Document></kml>

现在我几乎是在做这样的手动操作: Excel,world 并将此代码放入我的 OnMapReady(GoogleMap googleMap)中:

PolygonOptions rectOptions = new PolygonOptions()
                .add(new LatLng(43.1864681306065,1.04761446057425), new LatLng(43.186733854839,1.04740371376916), new LatLng(43.1875310275366,1.04859489136319), new LatLng(43.1877875888645,1.04966695119782), new LatLng(43.1884015034707,1.05063821908219), new LatLng(43.1888596486991,1.0513254369249), new LatLng(43.1889879293631,1.05327713559821), new LatLng(43.1892170019774,1.05492645842072), new LatLng(43.1890520696951,1.05634670862899), new LatLng(43.1895835181601,1.05765700398243), new LatLng(43.1907655328496,1.05871073800792), new LatLng(43.1951179125201,1.05776695883726), new LatLng(43.1965839772512,1.06452918240956), new LatLng(43.195631035176,1.06447420498214), new LatLng(43.1933403090336,1.06418099203592), new LatLng(43.1920391765848,1.06093732381831), new LatLng(43.1885939244666,1.05740044265448), new LatLng(43.18868097206,1.05425298493486), new LatLng(43.1879479396945,1.05134834418633), new LatLng(43.1864681306065,1.04761446057425));
googleMap.addPolygon(rectOptions);

您可以通过先创建 KmlLayer class 的实例来将 KML 图层添加到地图。有两 (2) 种方法可以实例化 KmlLayer:

  • 从本地资源渲染 KML 数据集:
    • KmlLayer layer = new KmlLayer(getMap(), R.raw.kmlFile, getApplicationContext());
  • 从本地流渲染 KML 数据集
    • KmlLayer layer = new KmlLayer(getMap(), kmlInputStream, getApplicationContext());

创建 KmlLayer 后,调用 addLayerToMap() 将导入的数据添加到地图上。

  • layer.addLayerToMap();

更多信息,您可以参考AndroidKML Importing Utility

的Maps SDK