Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSMDroid simple example required

Tags:

android

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!

like image 920
Bex Avatar asked Mar 16 '11 15:03

Bex


2 Answers

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>
like image 164
NickT Avatar answered Oct 14 '22 03:10

NickT


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

like image 36
oren zvi Avatar answered Oct 14 '22 03:10

oren zvi