我为一个离线地图工作了将近3天。后来有人向我推荐了Mapsforge。到目前为止,我所做的是下载我的MAP在这里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.access$900(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.access$900(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.access$900(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.access$900(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中确定酒店/餐厅/加油站等地点…如果有,那是怎么回事?提前感谢!
编辑:这是我的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 studio项目中导入的库。
mapsforge-core-0.5.1.jar
mapsforge-map-0.5.1.jar
mapsforge-map-android-0.5.1.jar
mapsforge-map-reader-0.5.1.jar
我看到你的代码有两个问题:
-
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);
...
我不确定它是否可以解决您的问题,但可能您需要kXML2和androidsvg库。
查找docs