从kml文件中的嵌套容器中检索数据

时间:2016-04-24 13:21:29

标签: android google-maps kml

我正在阅读google android map api -Access KML容器

https://developers.google.com/maps/documentation/android-api/utility/kml#clear

访问嵌套在KmlLayer或KmlContainer中的容器:

 Iterable containers = layer.getContainers();


public void accessContainers(containers) {
for (KmlContainer container : containers ) {
// Do something to container
if (container.hasContainers()) {
  accessContainers(container.getContainers());
}

} }

我应该把这个方法放在哪个程序中?

在demo progrma中,我发现了这些,但我仍然不知道如何使用它

//Retrieve the first container in the KML layer
    KmlContainer container = kmlLayer.getContainers().iterator().next();
    //Retrieve a nested container within the first container
    container = container.getContainers().iterator().next();

这是我的程序

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{

private GoogleMap mMap;
private KmlLayer layer;
private KmlContainer container;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

    @Override
public void onMapReady(GoogleMap googleMap)
{
    mMap = googleMap;
    try {
        layer = new KmlLayer(mMap, R.raw.canberra, getApplicationContext());
        layer.addLayerToMap();
         containers = layer.getContainers();

    } catch (XmlPullParserException e)
    {e.printStackTrace();}
    catch (IOException e)
    { e.printStackTrace();}

    LatLng sydney = new LatLng(-34, 151);
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

}

1 个答案:

答案 0 :(得分:0)

  

我应该把这个方法放在哪个程序中?

您可以将代码放在异步任务中,并在PostExecute步骤中调用它,如:

 //function to be called inside Async Task
 private void moveCameraToKml(KmlLayer kmlLayer) {
        //Retrieve the first container in the KML layer
        KmlContainer container = kmlLayer.getContainers().iterator().next();
        //Retrieve a nested container within the first container
        container = container.getContainers().iterator().next();
        //Retrieve the first placemark in the nested container
        KmlPlacemark placemark = container.getPlacemarks().iterator().next();
        //Retrieve a polygon object in a placemark
        KmlPolygon polygon = (KmlPolygon) placemark.getGeometry();
        //Create LatLngBounds of the outer coordinates of the polygon
        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        for (LatLng latLng : polygon.getOuterBoundaryCoordinates()) {
            builder.include(latLng);
        }
        getMap().moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 1));
    } 

    private class DownloadKmlFile extends AsyncTask<String, Void, byte[]> {
        private final String mUrl;

        public DownloadKmlFile(String url) {
            mUrl = url;
        }

        protected byte[] doInBackground(String... params) {
            try {
                InputStream is =  new URL(mUrl).openStream();
                ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                int nRead;
                byte[] data = new byte[16384];
                while ((nRead = is.read(data, 0, data.length)) != -1) {
                    buffer.write(data, 0, nRead);
                }
                buffer.flush();
                return buffer.toByteArray();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        protected void onPostExecute(byte[] byteArr) {
            try {
                kmlLayer = new KmlLayer(mMap, new ByteArrayInputStream(byteArr),
                        getApplicationContext());
                kmlLayer.addLayerToMap();
                moveCameraToKml(kmlLayer);
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

完整样本为here