【问题标题】:How to import KML with osmdroid?如何使用 osmdroid 导入 KML?
【发布时间】:2018-02-11 12:24:29
【问题描述】:

我有需要在地图中显示 KML 的项目,我已经在谷歌地图中完成了,但它似乎太慢了,有时它会强制关闭。 我查过了,他们需要把KML转成GPX,是不是和KML一样还是不一样?

现在我尝试使用 osmdroid 寻找其他解决方案,但很少有关于使用 osmdroid 导入 KML 的参考资料。 你知道我如何使用 osmdroid 导入 KML 吗?

【问题讨论】:

    标签: android openstreetmap osmdroid


    【解决方案1】:

    完整示例:

    app/build.gradle:

    android{
    ...
        repositories{
            ...
            maven { url "https://jitpack.io" }
        }
    ...
    }
    dependencies {
        ...
        compile 'org.osmdroid:osmdroid-android:5.6.5'
        compile 'com.github.MKergall:osmbonuspack:6.4'
    }
    

    MainActivity.java:

    public class MainActivity extends AppCompatActivity {
    
        private MapView mapView;
        private IMapController mapController;
        private Context context;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            context = this;
            mapView = (MapView) findViewById(R.id.mapView);
            mapView.setTileSource(TileSourceFactory.MAPNIK);
            mapView.setBuiltInZoomControls(true);
            mapController = mapView.getController();
            mapController.setZoom(15);
            GeoPoint point2 = new GeoPoint(51496994, -134733);
            mapController.setCenter(point2);
    
            loadKml();
        }
    
        public void loadKml() {
            new KmlLoader().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        }
    
        class KmlLoader extends AsyncTask<Void, Void, Void> {
            ProgressDialog progressDialog = new ProgressDialog(context);
            KmlDocument kmlDocument;
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                progressDialog.setMessage("Loading Project...");
                progressDialog.show();
            }
    
            @Override
            protected Void doInBackground(Void... voids) {
                kmlDocument = new KmlDocument();
                kmlDocument.parseKMLStream(getResources().openRawResource(R.raw.kmlfile), null);
                FolderOverlay kmlOverlay = (FolderOverlay)kmlDocument.mKmlRoot.buildOverlay(mapView, null, null,kmlDocument);
                mapView.getOverlays().add(kmlOverlay);
                return null;
            }
    
            @Override
            protected void onPostExecute(Void aVoid) {
                progressDialog.dismiss();
                mapView.invalidate();
                BoundingBox bb = kmlDocument.mKmlRoot.getBoundingBox();
                mapView.zoomToBoundingBox(bb, true);
    //            mapView.getController().setCenter(bb.getCenter());
                super.onPostExecute(aVoid);
            }
        }
    }
    

    activity_main.xml

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.sample.osmdroid.osmdroidsample.MainActivity">
    
        <org.osmdroid.views.MapView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/mapView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"/>
    
    </android.support.constraint.ConstraintLayout>
    

    在 Manifest 中添加以下权限:

        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    

    【讨论】:

    • 工作正常,但我收到一条消息“这个 AsyncTask 类应该是静态的,否则可能会发生泄漏 (com.example.map3.MainActivity.KmlLoader)” - 当然,如果它是静态的,我可以'不访问非静态 mapView。
    【解决方案2】:

    osmbonuspack 可以处理 KML 文件。有关如何在地图上显示 KML 文件的示例,请参阅 Tutorial 4

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多