使用任何库在Google Maps上为Android渲染KML

时间:2018-10-28 09:19:26

标签: android google-maps kml

我们的学生,目前正在使用Firebase数据库,而kml文件已上载到存储中。我们找到了一些有关它的演示项目,它使用asynctask通过url(inputstream)来获取文件,但是url没有任何url参数。在Firebase网址上,有某些参数。在我看来,问题是,我们在演示中找到的代码无法处理firebase url,并且崩溃了,有没有办法获取文件并将其呈现到Google地图?

这是演示代码中的

package com.google.maps.android.utils.demo;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.maps.android.data.Feature; 
import com.google.maps.android.data.kml.KmlContainer;
import com.google.maps.android.data.kml.KmlLayer;
import com.google.maps.android.data.kml.KmlPlacemark;
import com.google.maps.android.data.kml.KmlPolygon;

import org.xmlpull.v1.XmlPullParserException;

import android.nfc.Tag;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class KmlDemoActivity extends BaseDemoActivity {

private GoogleMap mMap;
private String TAG = "Result";

protected int getLayoutId() {
    return R.layout.kml_demo;
}

public void startDemo () {
    try {
        mMap = getMap();
        //retrieveFileFromResource();
        retrieveFileFromUrl();
    } catch (Exception e) {
        Log.e("Exception caught", e.toString());
    }
}

private void retrieveFileFromResource() {
    try {
        KmlLayer kmlLayer = new KmlLayer(mMap, R.raw.campus, getApplicationContext());
        kmlLayer.addLayerToMap();
        moveCameraToKml(kmlLayer);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    }
}

private void retrieveFileFromUrl() {

    new DownloadKmlFile(getString(R.string.kml_url)).execute();
}

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);
    }

    int width = getResources().getDisplayMetrics().widthPixels;
    int height = getResources().getDisplayMetrics().heightPixels;
    getMap().moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(),     width, height, 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);

            }
            Log.d(TAG, "doInBackground: "+buffer);
            buffer.flush();
            return buffer.toByteArray();


        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(byte[] byteArr) {
        try {
            KmlLayer kmlLayer = new KmlLayer(mMap, new ByteArrayInputStream(byteArr),
                    getApplicationContext());
            kmlLayer.addLayerToMap();
            kmlLayer.setOnFeatureClickListener(new KmlLayer.OnFeatureClickListener() {
                @Override
                public void onFeatureClick(Feature feature) {
                    Toast.makeText(KmlDemoActivity.this,
                            "Feature clicked: " + feature.getId(),
                            Toast.LENGTH_SHORT).show();
                }
            });
            moveCameraToKml(kmlLayer);
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}

strings.xml包含网址

 <string name="kml_url">http://googlemaps.github.io/kml-samples/morekml/Polygons/Polygons.Google_Campus.kml</string>

0 个答案:

没有答案