15

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!

Bex
  • 4,898
  • 11
  • 50
  • 87
  • can i ask something @Bex,.... is this you get the map offline i mean, trully without the android being connected to the internet? – gumuruh Aug 23 '14 at 09:11
  • @gumuruh This is a very old post, but yes, the map was truly offline (once it had been intially downloaded). I suspect there is a new way of doing things now though. – Bex Aug 26 '14 at 07:59
  • i see then, thanks alot @Bex, ... but unfortunately the newest way is using another SDK which is called nutiteq but i ended up without success for the offline part. – gumuruh Aug 26 '14 at 08:50

6 Answers6

10

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>
NickT
  • 23,844
  • 11
  • 78
  • 121
  • @NickT thanx for the reply! I have tried both these but with the same result! I am definitely missing something.Any ideas? – Bex Mar 16 '11 at 15:55
  • @NickT.. that's one thing the documentation was very clear about adding to the manifest. http://code.google.com/p/osmdroid/wiki/HowToUseJar so again not the problem! :( – Bex Mar 16 '11 at 16:13
  • Do you know does my class have to extend anything other than activity? – Bex Mar 16 '11 at 16:20
  • Mine doesn't, posted my entire code which worked once I added those lines to the manifest. Before I did the logcat was full of security exceptions – NickT Mar 16 '11 at 16:23
  • What's in your layout file just so I can make sure that is the same? – Bex Mar 16 '11 at 16:29
  • Posted my main.xml. I tried your coordinates, they work fine - sent me to Austin, Texas – NickT Mar 16 '11 at 16:34
  • Ahhh works! And the reason was not the code.. but the position of my permissions in the manifest! Told you I was new..! Your suggestion of looking in the log cat told me that! I keep forgetting about that..! Thanks so much! – Bex Mar 16 '11 at 16:37
  • Your welcome. If you have more problems with say, overlays and the like, I got a great deal of help from the answer to my question: http://stackoverflow.com/questions/4910410/porting-a-google-maps-app-to-osmdroid-problem-with-overlay – NickT Mar 16 '11 at 16:43
  • Great answer, in my case the code was similar to yours so I decided to look for other reasons - turns out my VD did not have an SD card configured. Thanks for the answer. – Steven Kramer Apr 03 '12 at 08:57
  • hey, hey,... sorry for bumping in about this osmdroid. But where do you guys got the map come from? @StevenKramer – gumuruh Aug 23 '14 at 09:18
  • Cool! this is taken from this doc https://github.com/osmdroid/osmdroid/wiki/How-to-use-the-osmdroid-library thank – Jorgesys Jun 24 '16 at 20:06
2

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

Community
  • 1
  • 1
oren zvi
  • 114
  • 1
  • 4
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();
hguser
  • 35,079
  • 54
  • 159
  • 293
  • 3
    You should translate your comment in to english language. – Ariel Magbanua Mar 15 '13 at 09:36
  • Also, if you could add a bried statement about how your code addresses the problem, that would be good. The top line of your code is outside the code block also. – Ren Mar 15 '13 at 09:37
0

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

Chelo
  • 443
  • 4
  • 7
0

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;

    }
}
rawhost
  • 144
  • 1
  • 6
0

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

tamshi
  • 61
  • 5