本文介绍了osmdroid中没有显示地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Android Studio创建osmdroid项目,但显示了任何地图.我只有一个空白的格子.

I tried to make an osmdroid project using Android Studio but any map is shown. I have just a blanck grid.

在我的xml文件中,输入以下代码:

In my xml file I put the folowing code :

    <?xml
version="1.0" encoding="utf-8"
?>
<LinearLayout
xmlns:android="..."
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<org.osmdroid.views.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
tilesource="Mapnik"
/>
</LinearLayout>

在我的.java文件中,我将此设置为onCreate方法

in my .java file I made this onCreate method

private MapView mMap;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);

mMap = (MapView) findViewById(R.id.map);
mMap.setMultiTouchControls(true);
mMap.setBuiltInZoomControls(true);
}

IMapController mapController = mMap.getController();

mapController.setZoom(14);
mapController.setCenter(new GeoPoint(48.745, -3.455));
ScaleBarOverlay scala = new ScaleBarOverlay(mMap);
mMap.getOverlays().add(scala);
mMap.invalidate();

我在清单中添加了以下权限:

And I added the following permissions in the manifest:

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

你知道我该如何解决这个问题?

Do you know how can I solve this problem ?

我使用了osmdroid-android:5.6.4'(我也尝试了其他版本,但所有版本都存在相同的问题)

I used osmdroid-android:5.6.4' (and I tried also other versions but I have the same problem with all)

推荐答案

使用 6.1 版本及更高版本的osmdroid lib.请按照以下步骤操作:

use 6.1 version and up for osmdroid lib. follow these steps:

1..从您的xml文件中删除tilesource属性

1. remove tilesource attribute from your xml file

tilesource="mapnik"  

2..在调用地图的findviewbyId()之前,请放置此代码段代码.

2. put this snippet code before you call findviewbyId() your map.

final Context ctx = getApplicationContext();
      Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
      Configuration.getInstance().setUserAgentValue(BuildConfig.APPLICATION_ID);
      mMap = (MapView) findViewById(R.id.map);

3..在findViewbyId()之后在snnipet下方添加

3. add below snnipet after findViewbyId()

mMap.setTileSource(TileSourceFactory.MAPNIK); 

您的最终代码应如下所示:

your final code should be like this :

activity.java

activity.java

    private MapView mMap;
        @Override
        protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    final Context ctx = getApplicationContext();
          Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
          Configuration.getInstance().setUserAgentValue(BuildConfig.APPLICATION_ID);

    mMap = (MapView) findViewById(R.id.map);
    mMap.setTileSource(TileSourceFactory.MAPNIK); 
    mMap.setMultiTouchControls(true);
    mMap.setBuiltInZoomControls(true);
    }

.xml文件

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <org.osmdroid.views.MapView android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

这篇关于osmdroid中没有显示地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 20:07