【问题标题】:Display the KML file from Android device memory on the map using Osmdriod使用 Osmdriod 在地图上显示 Android 设备内存中的 KML 文件
【发布时间】:2019-06-22 10:00:02
【问题描述】:

我看到了 Osmdroid 教程,我可以使用它在地图上显示来自 Web 地址的 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) {

        }
    }

【问题讨论】:

    标签: android kml osmdroid


    【解决方案1】:

    假设您提供与APK 捆绑在一起的KML 文件,您可以尝试将文件存储在src/main/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);
    }
    

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

    【讨论】:

    • 你好。感谢您的留言。但是这段代码不起作用。也许给我更多的指导?
    • 抱歉耽搁了,这段代码实际上是个人项目的一部分,它可以工作,唯一不同的是文件名,请您提供更多关于您遇到的错误或部分的信息不工作?
    猜你喜欢
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    • 1970-01-01
    • 2012-08-07
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多