Mapsforge最简单的地图查看器 - 未使用默认主题渲染的地图

时间:2016-01-08 15:39:46

标签: java android android-studio osmdroid mapsforge

我试图运行mapsforge项目中最简单的地图查看器的示例: Android Studio A very basic Android app example - GitHub 在应用程序运行期间,我遇到问题 - 地图未呈现:http://i.stack.imgur.com/bVNUZ.jpg
我的logcat有消息:

01-08 15:46:45.273 32331-32359/com.maptests.mapsforgetest E/DatabaseRenderer: Error to retrieve render theme from future
java.util.concurrent.ExecutionException: java.lang.NoClassDefFoundError: com.caverock.androidsvg.SVG
at java.util.concurrent.FutureTask.report(FutureTask.java:93)
at java.util.concurrent.FutureTask.get(FutureTask.java:163)
at org.mapsforge.map.layer.renderer.DatabaseRenderer.executeJob(DatabaseRenderer.java:124)
at org.mapsforge.map.layer.renderer.MapWorkerPool$MapWorker.run(MapWorkerPool.java:121)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NoClassDefFoundError: com.caverock.androidsvg.SVG
at org.mapsforge.map.android.graphics.AndroidSvgBitmap.getResourceBitmap(AndroidSvgBitmap.java:49)
at org.mapsforge.map.android.graphics.AndroidSvgBitmap.<init>(AndroidSvgBitmap.java:107)
at org.mapsforge.map.android.graphics.AndroidGraphicFactory.renderSvg(AndroidGraphicFactory.java:302)
at org.mapsforge.map.rendertheme.XmlUtils.createBitmap(XmlUtils.java:64)
at org.mapsforge.map.rendertheme.renderinstruction.RenderInstruction.createBitmap(RenderInstruction.java:131)
at org.mapsforge.map.rendertheme.renderinstruction.Symbol.getBitmap(Symbol.java:62)
at org.mapsforge.map.rendertheme.renderinstruction.Caption.<init>(Caption.java:96)
at org.mapsforge.map.rendertheme.rule.RenderThemeHandler.startElement(RenderThemeHandler.java:186)
at org.mapsforge.map.rendertheme.rule.RenderThemeHandler.processRenderTheme(RenderThemeHandler.java:107)
at org.mapsforge.map.rendertheme.rule.RenderThemeHandler.getRenderTheme(RenderThemeHandler.java:67)
at org.mapsforge.map.rendertheme.rule.RenderThemeFuture$RenderThemeCallable.call(RenderThemeFuture.java:60)
at org.mapsforge.map.rendertheme.rule.RenderThemeFuture$RenderThemeCallable.call(RenderThemeFuture.java:43)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.lang.Thread.run(Thread.java:841) 

主要活动代码(几乎没有改变,只有与原始代码相比的重要变化):

package com.maptests.mapsforgetest;
import java.io.File;
import org.mapsforge.core.model.LatLong;
import org.mapsforge.map.android.graphics.AndroidGraphicFactory;
import org.mapsforge.map.android.util.AndroidUtil;
import org.mapsforge.map.android.view.MapView;
import org.mapsforge.map.layer.cache.TileCache;
import org.mapsforge.map.layer.renderer.TileRendererLayer;
import org.mapsforge.map.datastore.MapDataStore;
import org.mapsforge.map.reader.MapFile;
import org.mapsforge.map.rendertheme.InternalRenderTheme;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Environment;

public class MainActivity extends AppCompatActivity {
    // name of the map file in the external storage
    private static final String MAPFILE = "germany.map";
    private MapView mapView;
    private TileCache tileCache;
    private TileRendererLayer tileRendererLayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidGraphicFactory.createInstance(this.getApplication());
        this.mapView = new MapView(this);
        setContentView(this.mapView);
        this.mapView.setClickable(true);
        this.mapView.getMapScaleBar().setVisible(true);
        this.mapView.setBuiltInZoomControls(true);
        this.mapView.getMapZoomControls().setZoomLevelMin((byte) 10);
        this.mapView.getMapZoomControls().setZoomLevelMax((byte) 20);
        // create a tile cache of suitable size
        this.tileCache = AndroidUtil.createTileCache(this, "mapcache",
                mapView.getModel().displayModel.getTileSize(), 1f,
                this.mapView.getModel().frameBufferModel.getOverdrawFactor());
    }
    @Override
    protected void onStart() {
        super.onStart();
        this.mapView.getModel().mapViewPosition.setCenter(new LatLong(52.229081, 21.068434));
        this.mapView.getModel().mapViewPosition.setZoomLevel((byte) 12);
        // tile renderer layer using internal render theme
        MapDataStore mapDataStore = new MapFile(getMapFile());
        this.tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore,
                this.mapView.getModel().mapViewPosition, false, true, AndroidGraphicFactory.INSTANCE);
        tileRendererLayer.setXmlRenderTheme(InternalRenderTheme.OSMARENDER);
        // only once a layer is associated with a mapView the rendering starts
        this.mapView.getLayerManager().getLayers().add(tileRendererLayer);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.mapView.destroyAll();
    }

    private File getMapFile() {
        File file = new File(Environment.getExternalStorageDirectory(), MAPFILE);
        return file;
    }
}

我的XML设计文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.maptests.mapsforgetest.MainActivity">

    <org.mapsforge.map.android.view.MapView
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

我想我已经添加了所有必需的.jar-s:
mapsforge核-0.6.0.jar,
mapsforge-MAP-0.6.0.jar,
mapsforge-地图的Android,
mapsforge-MAP-AWT-0.6.0.jar,
mapsforge地图阅读器 - 0.6.0.jar。

我还应该添加其他库吗?

根据logcat中的消息和我读到的消息,默认地图主题存在问题,我该如何解决问题?

3 个答案:

答案 0 :(得分:1)

Cichocki,它也发生在我身上,让我们开始是的,它从库mapsforge 0.6中缺少那个类,那个和其他的,你需要下载这个源代码它是一个库svg: https://github.com/senkir/androidsvg    下载该repo,导入到你的工作区并添加到你的项目我在eclipse中做了,添加为库,并运行该代码不需要添加任何额外的东西,你应该看到地图,祝你好运

答案 1 :(得分:1)

您应该通过将主题库添加到build.gradle或使用等效的jar来添加主题库:

implementation 'org.mapsforge:mapsforge-themes:[CURRENT-VERSION]'

有关更多信息,请参见integration guide

答案 2 :(得分:0)

根据integration guide,您需要另外导入:

implementation 'com.caverock:androidsvg:1.4'