OSMDroid需要简单的例子

时间:2011-03-16 15:15:11

标签: android

我正在尝试创建一个使用离线地图和自定义图块的应用。 为此我决定使用OSMDroid并将jar包含在我的项目中。 我将使用MOBAC创建我的自定义图块。

我被引导到以下示例:http://code.google.com/p/osmdroid/source/browse/#svn%2Ftrunk%2FOpenStreetMapViewer%2Fsrc%2Forg%2Fosmdroid%2Fsamples

但我很难跟随他们,因为我是java和android的新手。

我创建了一个名为test的类文件(我根据一个例子创建了它!):

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,但我不确定我需要将它更改为。

更新: 我的清单文件中还有以下内容:

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

有人可以帮忙吗?

贝克斯

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

6 个答案:

答案 0 :(得分:11)

这个对我有用:

setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);   

同样如下:

setTileSource(TileSourceFactory.MAPNIK);

我不需要在XML中有任何内容

它现在又回来了,我不得不添加其中一个:

<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。

我不记得哪一个是必要的,但是如果你把所有3个都放进去,它应该有效。

这是我的完整资源,我刚刚在模拟器上运行:

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中是否有任何异常。

和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>

答案 1 :(得分:2)

为了避免bubbly崩溃,您需要将osmdroid-android.jar和slf4j-android.jar添加到项目中的libs。有关详细信息,请参阅Simple osmdroid application doesn't work

答案 2 :(得分:1)

    final MapView mapView = new MapView(this, 256);

    mapView.setBuiltInZoomControls(true);//显示缩放按钮  //display the zoom button
    mapView.setMultiTouchControls(true);//开启多点触控  //turn on the multiple touch feature
    mapView.getController().setZoom(13);


    mapView.setTileSource(TileSourceFactory.MAPQUESTOSM);
    mapView.getController().setCenter(new GeoPoint(39.9,116.3));//设置中心点 //set the center 
    //定位当前位置 // locate to current position
    myLocationOverlay = new MyLocationOverlay(getApplicationContext(), mapView);
    mapView.getOverlays().add(myLocationOverlay);
    myLocationOverlay.enableCompass();
    myLocationOverlay.enableMyLocation();
    myLocationOverlay.enableFollowLocation();

答案 3 :(得分:0)

我认为从查看其他示例时,您会错过权限。 除非地图已经缓存在您的设备上,否则您肯定需要访问INTERNET。 试试这套:

<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" />
祝你好运

答案 4 :(得分:0)

**请注意,如果您要定位Android设备&gt; = 6.0(SDK&gt; = 23),则必须在运行时检查“危险”权限。 osmdroid所需的危险权限是:WRITE_EXTERNAL_STORAGE和ACCESS_COARSE_LOCATION / ACCESS_FINE_LOCATION。请参阅此implementation

取自:“如何使用osmdroid库” - link

答案 5 :(得分:0)

我已将OSM地图集成到我的应用程序中。 使用以下课程 1.OSMMap活动 2.OSMMap ItemizedOverlay 三个jar必须复制到Project-&gt; App-&gt; lib的lib文件夹中 OSMBonusPack-v5.8.1-源(1)的.jar osmdroid-Android的4.3.jar SLF4J-Android的1.5.8.jar 1.OSMmap活动

 public class OSMMap extends Activity
    {
    MapView mapView;
    MapController mapController;
    private double latitude;
    private double longitude;
    String date,time,vname;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_osmmap2);

        //Coming Parameter is from vehicleinfo activity
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        latitude = Double.parseDouble(bundle.getString("lat"));
        Log.i("CPA","In the OSM map method"+latitude);
        longitude = Double.parseDouble(bundle.getString("lng"));
        Log.i("CPA","In the OSM map method"+longitude);
        date = bundle.getString("date");
        Log.i("CPA","In the OSM map method"+date);
        time = bundle.getString("time");
        Log.i("CPA","In the OSM map method"+time);
        vname = bundle.getString("vname");
        Log.i("CPA","In the OSM map method"+vname);
        mapView= (MapView) findViewById(R.id.mapview);
        mapView.setTileSource(TileSourceFactory.MAPNIK);
//        mapView.setTileSource(TileSourceFactory.BASE_OVERLAY_NL);

//        mapView.setTileSource(TileSourceFactory.CYCLEMAP);
//        mapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
//      mapView.setTileSource(TileSourceFactory.BASE);
//        mapView.setTileSource(TileSourceFactory.MAPQUESTOSM);
//      mapView.setTileSource(TileSourceFactory.MAPQUESTAERIAL);
        mapView.setBuiltInZoomControls(true);
        mapView.setMultiTouchControls(true);
        mapController= (MapController) mapView.getController();
        Log.i("CPA","In the map view"+mapView);

//      GeoPoint geoPoint=new GeoPoint(40.712784,-74.005941);
        GeoPoint geoPoint=new GeoPoint(latitude,longitude);
        Log.i("CPA","In the OSM map method"+latitude);
        Log.i("CPA","In the OSM map method"+longitude);
        mapController.setZoom(8);
        mapController.animateTo(geoPoint);
        Log.i("CPA","In the OSM map method"+geoPoint);
        Log.i("CPA","In the OSsM map method");

        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.blue_marker);

        OSMDemo itemizedoverlay = new OSMDemo(drawable,this);
        itemizedoverlay.setEnabled(true);

        OverlayItem overlayItem=new OverlayItem("Vehicle"+time,"VEHICLE NO"+date,geoPoint);
        mapView.getController().setZoom(18);
        mapView.getController().setCenter(geoPoint);

        itemizedoverlay.addOverlay(overlayItem);
        mapOverlays.add(itemizedoverlay);
        mapView.getOverlays().add(itemizedoverlay);
        mapView.setMultiTouchControls(true);

    }

}

2.OSM ItemizedOverlay

public class OSMDemo extends ItemizedOverlay<OverlayItem>
{

    static ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    private Context mContext;

    public OSMDemo(Drawable pDefaultMarker, Context context)
    {

        super(pDefaultMarker,new DefaultResourceProxyImpl(context));
        mContext=context;

    }

    public void addOverlay(OverlayItem overlay)
    {
        mOverlays.add(overlay);
        populate();

    }

    @Override
    protected OverlayItem createItem(int i)
    {
        return mOverlays.get(0);

    }
    @Override
    public int size()
    {
        return mOverlays.size();
    }
    @Override
    public boolean onSnapToItem(int i, int i1, Point point, IMapView iMapView)
    {


        return false;
    }

    public boolean ontap(int index)
    {

        Log.i("CPA","In the OSMDEMO ONTAP");
        OverlayItem item = mOverlays.get(index);
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();
        return true;

    }
}