OSMDroid需要简单的例子例子、简单、OSMDroid

2023-09-05 06:34:12 作者:用沉默替代分手`

我想创建一个使用离线地图和定制瓷砖的应用程序。 为此,我决定用OSMDroid并已列入我的计划之内的罐子。 我会用MOBAC创建我的自定义瓷砖。

I am trying to create an app that uses offline maps and custom tiles. For this I have decided to use OSMDroid and have included the jar within my project. I will create my custom tiles using MOBAC.

我已指示这些例子:http://$c$c.google.com/p/osmdroid/source/browse/#svn%2Ftrunk%2FOpenStreetMapViewer%2Fsrc%2Forg%2Fosmdroid%2Fsamples

但我在努力跟随他们,因为我是新来的Java和Android的。

but I am struggling to follow them as I am new to both java and android.

我已经创建了名为test的类文件(我已经创建了下面的一个例子!):

I have created a class file called test (which I have created following an example!):

public class test extends Activity {
/** Called when the activity is first created. */

 protected static final String PROVIDER_NAME = LocationManager.GPS_PROVIDER;

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    MapView map = (MapView) findViewById(R.id.map);
    map.setTileSource(TileSourceFactory.MAPQUESTOSM);

    map.setBuiltInZoomControls(true);
    map.setMultiTouchControls(true);
    map.getController().setZoom(16);
    map.getController().setCenter(new GeoPoint(30266000, -97739000));

}

}

与布局文件:

<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"
        tilesource="MapquestOSM" android:layout_weight="1" />
</LinearLayout>

当我运行这个,我看不出有任何的地图,只是一个空网格。 我想这是因为我的tilesource,但我不知道我需要将其更改为。

When I run this I see no map, just an empty grid. I think this is due to my tilesource but I'm not sure what I need to change it to.

更新: 我也有我的清单文件如下:

UPDATE: I also have the following in my manifest file:

<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" />

谁能帮助?

贝克斯

解决方案 确保权限的位置在清单中的正确的地方!

Solution Make sure the position of the permissions is in the correct place in the manifest!

推荐答案

这一次为我工作:

setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);   

一样:

setTileSource(TileSourceFactory.MAPNIK);

我并不需要有在该XML什么

I didn't need to have anything in the the XML

这是回来是现在,我不得不添加的其中之一:

It's coming back to be now, I had to add one of these:

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

到的manifest.xml。

to the manifest.xml.

我不记得哪一个是必要的,但如果你把所有3,它应该工作。

I can't remember which one was necessary but if you put all 3 in, it should work.

那么这里是我的全部源代码,这是我在模拟器刚刚运行:

Well here's my entire source, which I've just run on the emulator:

package com.nbt.osmdroidtest;

import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import android.app.Activity;
import android.os.Bundle;

public class OsmDroidTest extends Activity {
    /** Called when the activity is first created. */
    private MapController mapController;
    private MapView mapView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        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);
    }
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}   

给它一分钟左右加载,因为最初它可能是在建立一个缓存相当缓慢。 这些坐标应该把你在伦敦市中心。如果你还有问题看看是否有什么启发在logcat中。

Give it a minute or so to load, as initially it might be quite slow in building up a cache. Those coordinates should put you over central London. If you still have problems see if there is anything illuminating in the logcat.

和中的main.xml

And the main.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
    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"

/>
</LinearLayout>