使用Osmdriod在地图上显示Android设备内存中的KML文件

时间:2019-06-22 10:00:02

标签: android kml osmdroid

我看了Osmdroid教程,可以用它在地图上的网址上显示KML文件。但现在我想在地图上的Android设备内显示KML文件。我尝试了几种方法。我使用了诸如parseKMLFile之类的不同方法。但是我没有回答。也许指导我编写正确的代码。我使用的示例代码如下所示。 谢谢

----------- sample code 1 for show file from device
    import android.app.ProgressDialog;
    import android.content.Context;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;

    import org.osmdroid.api.IMapController;
    import org.osmdroid.bonuspack.kml.KmlDocument;
    import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
    import org.osmdroid.util.BoundingBox;
    import org.osmdroid.util.GeoPoint;
    import org.osmdroid.views.MapView;
    import org.osmdroid.views.overlay.FolderOverlay;

    import java.io.File;

    public class KMLfile extends AppCompatActivity {

        private MapView mapView;
        private IMapController mapController;
        private Context context;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.kml_file);

            context = this;
            mapView = (MapView) findViewById(R.id.map_kml_file);
            mapView.setTileSource(TileSourceFactory.MAPNIK);
            mapView.setBuiltInZoomControls(true);
            mapController = mapView.getController();
            mapController.setZoom(15);
            GeoPoint point2 = new GeoPoint(51496994, -134733);
            mapController.setCenter(point2);

            //loadMarker
            File filefile = new File(this.getActivity().getFilesDir(), "earth.kml");
            KmlDocument kmlFile = new KmlDocument();
            kmlFile.parseKMLFile(filefile);
            FolderOverlay kmlOverlay = (FolderOverlay)kmlFile.mKmlRoot.buildOverlay(mapView, null, null, kmlFile);
            mapView.getOverlays().addAll(kmlOverlay.getItems());
        }
    }

    ------------  sample code 2 for show from web
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.StrictMode;
    import android.view.View;

    import org.osmdroid.api.IMapController;
    import org.osmdroid.bonuspack.kml.KmlDocument;
    import org.osmdroid.config.Configuration;
    import org.osmdroid.events.MapEventsReceiver;
    import org.osmdroid.util.BoundingBox;
    import org.osmdroid.util.GeoPoint;
    import org.osmdroid.views.MapView;
    import org.osmdroid.views.overlay.FolderOverlay;

    /**
     * This is the implementation of OSMBonusPack tutorials.
     * Sections of code can be commented/uncommented depending on the progress in the tutorials.
     *
     * @author M.Kergall
     * @see <a href="https://github.com/MKergall/osmbonuspack">OSMBonusPack on GitHub</a>
     */
    public class Three extends Activity implements MapEventsReceiver, MapView.OnFirstLayoutListener {

        MapView map;
        KmlDocument mKmlDocument;

        @Override protected void onCreate(Bundle savedInstanceState) {

            //Disable StrictMode.ThreadPolicy to perform network calls in the UI thread.
            //Yes, it's not the good practice, but this is just a tutorial!
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

            //Introduction
            super.onCreate(savedInstanceState);
            Configuration.getInstance().setUserAgentValue("OBP_Tuto/1.0");

            setContentView(R.layout.six);
            map = (MapView) findViewById(R.id.map_simple_show_routing);

            map.setMultiTouchControls(true);
            GeoPoint startPoint = new GeoPoint(48.13, -1.63);
            IMapController mapController = map.getController();
            mapController.setZoom(10.0);
            mapController.setCenter(startPoint);


            KmlDocument kmlDocument = new KmlDocument();
            kmlDocument.parseKMLUrl("http://mapsengine.google.com/map/kml?forcekml=1&mid=z6IJfj90QEd4.kUUY9FoHFRdE");
            FolderOverlay kmlOverlay = (FolderOverlay)kmlDocument.mKmlRoot.buildOverlay(map, null, null, kmlDocument);

            map.getOverlays().add(kmlOverlay);
            BoundingBox bb = kmlDocument.mKmlRoot.getBoundingBox();
            map.zoomToBoundingBox(bb,true);




            map.invalidate();
        }


        @Override
        public boolean singleTapConfirmedHelper(GeoPoint p) {
            return false;
        }

        @Override
        public boolean longPressHelper(GeoPoint p) {
            return false;
        }

        @Override
        public void onFirstLayout(View v, int left, int top, int right, int bottom) {

        }
    }

1 个答案:

答案 0 :(得分:0)

假设您提供与KML捆绑在一起的APK文件,则可以尝试将文件存储在src/main/assets目录中

Storing file in assets

然后尝试使用此代码读取KMLgetAssets()方法将在运行时解析到assets目录的正确路径。

try (InputStream fileStream = getAssets().open("earth.kml")) {
    KmlDocument kmlDocument = new KmlDocument();
    boolean isOk = kmlDocument.parseKMLStream(fileStream, null);

    if (!isOk) {
        Log.e(TAG, "onCreate: parsing Failed, is this a valid KML?");
    } else {
        FolderOverlay kmlOverlay = (FolderOverlay) kmlDocument.mKmlRoot.buildOverlay(mapView, null, null, kmlDocument);
        overlays.add(kmlOverlay);
        BoundingBox bb = kmlDocument.mKmlRoot.getBoundingBox();
        mapView.zoomToBoundingBox(bb, true);
        mapView.invalidate();
    }

} catch (IOException e) {
    Log.e(TAG, "onCreate: Error reading KMZ file", e);
}

希望这会有所帮助,但如果不是这种情况,也许您的日志以及有关如何提供(和/或存储)文件的更多说明可以使我们更好地了解可能是问题所在。