Bex
Bex

Reputation: 4918

OSMDroid simple example required

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.

I have been directed to these examples: http://code.google.com/p/osmdroid/source/browse/#svn%2Ftrunk%2FOpenStreetMapViewer%2Fsrc%2Forg%2Fosmdroid%2Fsamples

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

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));

}

}

with a layout file:

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

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

Can anyone help?

Bex

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

Upvotes: 15

Views: 33033

Answers (6)

rawhost
rawhost

Reputation: 154

I have integrated the OSM map in my application. Following class is used 1.OSMMap activity 2.OSMMap ItemizedOverlay Three jar has to copy into lib folder of Project->App->lib OSMBonusPack-v5.8.1-sources(1).jar osmdroid-android-4.3.jar slf4j-android-1.5.8.jar 1.OSMmap Activity

 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;

    }
}

Upvotes: 0

Chelo
Chelo

Reputation: 443

**Note that if you are targeting Android devices >= 6.0 (SDK >= 23), you have to check for "dangerous" permissions at runtime. Dangerous permissions needed by osmdroid are: WRITE_EXTERNAL_STORAGE and ACCESS_COARSE_LOCATION/ACCESS_FINE_LOCATION. Refer to this implementation

take from: "How to use the osmdroid library" - link

Upvotes: 0

zhengfazhen
zhengfazhen

Reputation: 31

    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();

Upvotes: 1

oren zvi
oren zvi

Reputation: 114

To avoid the crash bubbly had, you need to add osmdroid-android.jar and slf4j-android.jar to libs in your project. See Simple osmdroid application doesn't work for more details

Upvotes: 2

tamshi
tamshi

Reputation: 61

I think from looking at other samples, that you miss permissions. unless the map is already cached on your device, you definitely need to access INTERNET. try this set:

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

good luck

Upvotes: 0

NickT
NickT

Reputation: 23873

This one worked for me:

setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);   

as did:

setTileSource(TileSourceFactory.MAPNIK);

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

to the manifest.xml.

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;
    }
}   

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.

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>

Upvotes: 10

Related Questions