Android Studio 上的 Mapsforge

Mapsforge on Android Studio

我为离线地图工作了将近 3 天。然后有人向我推荐了 Mapsforge。到目前为止,我所做的是在此处 http://download.mapsforge.org/maps/asia/ 下载我的地图。之后,我将其放入我的设备存储中,并将 运行 这段代码放入我的 android 工作室。

public class MainActivity extends Activity {
private static final String MAPFILE = "philippines.map";
private MapView mapView;
private TileCache tileCache;
private TileRendererLayer tileRendererLayer;
private MyLocationOverlay myLocationOverlay;
private MapViewPosition mapViewPosition;
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RelativeLayout mylayout=(RelativeLayout)findViewById(R.id.mainView);
    AndroidGraphicFactory.createInstance(this.getApplication());

    this.mapView = new MapView(this);
    mylayout.addView(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(13.000,122.000));
    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.destroy();
}


private File getMapFile() {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    File file = new File("/mnt/sdcard/"+MAPFILE);
    return file;
}

当我 运行 我的应用程序出现此错误时。

10-03 00:07:01.680  30050-30050/com.example.galvez.mapsforge E/Zygote﹕ MountEmulatedStorage()
10-03 00:07:01.680  30050-30050/com.example.galvez.mapsforge E/Zygote﹕ v2
10-03 00:07:01.680  30050-30050/com.example.galvez.mapsforge I/libpersona﹕ KNOX_SDCARD checking this for 10268
10-03 00:07:01.680  30050-30050/com.example.galvez.mapsforge I/libpersona﹕ KNOX_SDCARD not a persona
10-03 00:07:01.710  30050-30050/com.example.galvez.mapsforge I/SELinux﹕ Function: selinux_compare_spd_ram, SPD-policy is existed. and_ver=SEPF_SM-G900F_5.0 ver=27
10-03 00:07:01.710  30050-30050/com.example.galvez.mapsforge I/SELinux﹕ Function: selinux_compare_spd_ram , priority [1] , priority version is VE=SEPF_SM-G900F_5.0_0027
10-03 00:07:01.720  30050-30050/com.example.galvez.mapsforge E/SELinux﹕ [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
10-03 00:07:01.720  30050-30050/com.example.galvez.mapsforge I/art﹕ Late-enabling -Xcheck:jni
10-03 00:07:01.770  30050-30050/com.example.galvez.mapsforge D/TimaKeyStoreProvider﹕ TimaSignature is unavailable
10-03 00:07:01.770  30050-30050/com.example.galvez.mapsforge D/ActivityThread﹕ Added TimaKeyStore provider
10-03 00:07:02.060  30050-30050/com.example.galvez.mapsforge D/ResourcesManager﹕ creating new AssetManager and set to /data/app/com.example.galvez.mapsforge-1/base.apk
10-03 00:07:02.110  30050-30050/com.example.galvez.mapsforge V/BitmapFactory﹕ DecodeImagePath(decodeResourceStream3) : res/drawable-xxhdpi-v4/sym_def_app_icon.png
10-03 00:07:02.130  30050-30050/com.example.galvez.mapsforge D/AndroidRuntime﹕ Shutting down VM
10-03 00:07:02.130  30050-30050/com.example.galvez.mapsforge E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.galvez.mapsforge, PID: 30050
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.galvez.mapsforge/com.example.galvez.mapsforge.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class org.mapsforge.map.android.view.MapView
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
            at android.app.ActivityThread.access0(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5832)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
     Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class org.mapsforge.map.android.view.MapView
            at android.view.LayoutInflater.createView(LayoutInflater.java:640)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:428)
            at android.app.Activity.setContentView(Activity.java:2241)
            at com.example.galvez.mapsforge.MainActivity.onCreate(MainActivity.java:36)
            at android.app.Activity.performCreate(Activity.java:6221)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
            at android.app.ActivityThread.access0(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5832)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
            at android.view.LayoutInflater.createView(LayoutInflater.java:614)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:428)
            at android.app.Activity.setContentView(Activity.java:2241)
            at com.example.galvez.mapsforge.MainActivity.onCreate(MainActivity.java:36)
            at android.app.Activity.performCreate(Activity.java:6221)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
            at android.app.ActivityThread.access0(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5832)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
     Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'org.mapsforge.core.graphics.Paint org.mapsforge.core.graphics.GraphicFactory.createPaint()' on a null object reference
            at org.mapsforge.map.view.FpsCounter.createPaintFront(FpsCounter.java:36)
            at org.mapsforge.map.view.FpsCounter.<init>(FpsCounter.java:62)
            at org.mapsforge.map.android.view.MapView.<init>(MapView.java:73)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
            at android.view.LayoutInflater.createView(LayoutInflater.java:614)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:428)
            at android.app.Activity.setContentView(Activity.java:2241)
            at com.example.galvez.mapsforge.MainActivity.onCreate(MainActivity.java:36)
            at android.app.Activity.performCreate(Activity.java:6221)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
            at android.app.ActivityThread.access0(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5832)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)  

以及以后的问题。 是否可以在 mapsforge 中确定 hotels/Restaurant/Gasoline 站等地点... 如果是这样怎么办?提前致谢!

编辑:这是我的 XML

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/mainView"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

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

</RelativeLayout>

这是我的 android 工作室项目中导入的库。

mapsforge-core-0.5.1.jar
mapsforge-map-0.5.1.jar
mapsforge-map-android-0.5.1.jar
mapsforge-地图-reader-0.5.1.jar

我不确定它能否解决您的问题,但您可能需要 kXML2 和 androidsvg 库。

docs

中查找链接

我发现您的代码有两个问题:

  • AndroidGraphicFactory.createInstance(this.getApplication()) 语句应在设置内容视图之前调用,如@Nikolai Doronin
  • 所评论
  • 由于您的 xml-layout 已经包含一个 MapView,因此没有理由创建一个新的 MapView。请改用现有的。

.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    AndroidGraphicFactory.createInstance(this.getApplication());

    setContentView(R.layout.activity_main);
    this.mapView = (MapView) findViewById(R.id.mapView);

    this.mapView.setClickable(true);
    ...