OpenStreetMap MapView for Android

Tutorials that use the MapActivity. Many using GPS functionality.

Re: OpenStreetMap MapView for Android

Postby sorer » Mon Jan 17, 2011 8:12 pm

download osmdroid-android-3.0.0.jar, include it in your project, and use in your layout like:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.   <org.andnav.osm.views.OpenStreetMapView
  2.    xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:id="@+id/mapview"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6. />
  7.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
sorer
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sun Sep 12, 2010 6:14 pm

Top

Re: OpenStreetMap MapView for Android

Postby Ulathar » Mon Jan 17, 2011 8:19 pm

the layout isnt the problem.
is the java part the same as with the normal MapView (just add OpenStreetMapView)?
Ulathar
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sat Jun 05, 2010 9:55 pm

Re: OpenStreetMap MapView for Android

Postby sorer » Tue Jan 18, 2011 9:58 am

Yes, most main methods are same. In my opinion OpenStreetMapView is better, you can use offline maps, many other map sources (mapnik, osm, cycle maps etc..).
I used it this way:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. this.mapView = (OpenStreetMapView) this.findViewById(R.id.mapview);        
  2. this.mapController = this.mapView.getController();
  3. this.mapController.setZoom(mapUtils.zoomlevel);
  4. this.mapView.setBuiltInZoomControls(true);
  5. this.mapView.setMultiTouchControls(true);
  6. this.mLocationOverlay = new MyLocationOverlay(this.getBaseContext(), this.mapView);
  7. this.mapView.getOverlays().add(this.mLocationOverlay);
  8.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
sorer
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sun Sep 12, 2010 6:14 pm

Re: OpenStreetMap MapView for Android

Postby Fritte » Tue Jan 18, 2011 11:56 am

hello,
I too am not against a tutorial, I follow other applications, and I always errors at runtime ...
If, i shall view the map, I shall continue, it really nice.
Help please ;-).
Very nice work and thank you all.
Fritte
Freshman
Freshman
 
Posts: 2
Joined: Tue Jan 18, 2011 11:50 am

Re: OpenStreetMap MapView for Android

Postby sorer » Tue Jan 18, 2011 8:20 pm

I create basic demo for using osmdroid MapView using mylocationoverlay and scalebaroverlay

Download link

map.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package osmdemo.demo;
  2.  
  3. import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
  4. import org.osmdroid.views.MapController;
  5. import org.osmdroid.views.MapView;
  6. import org.osmdroid.views.overlay.ScaleBarOverlay;
  7. import org.osmdroid.views.overlay.SimpleLocationOverlay;
  8. import org.osmdroid.views.util.constants.MapViewConstants;
  9.  
  10. import android.app.Activity;
  11. import android.os.Bundle;
  12.  
  13. public class map extends Activity implements MapViewConstants {
  14.        
  15.         private MapView mapView;
  16.         private MapController mapController;
  17.         private SimpleLocationOverlay mMyLocationOverlay;
  18.         private ScaleBarOverlay mScaleBarOverlay;  
  19.                
  20.     /** Called when the activity is first created. */
  21.     @Override
  22.     public void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.main);
  25.      
  26.         mapView = (MapView) this.findViewById(R.id.mapview);  
  27.         mapView.setTileSource(TileSourceFactory.MAPNIK);
  28.             mapView.setBuiltInZoomControls(true);
  29.             mapView.setMultiTouchControls(true);
  30.            
  31.             mapController = this.mapView.getController();
  32.         mapController.setZoom(2);
  33.            
  34.             this.mMyLocationOverlay = new SimpleLocationOverlay(this);                          
  35.             this.mapView.getOverlays().add(mMyLocationOverlay);
  36.            
  37.             this.mScaleBarOverlay = new ScaleBarOverlay(this);                          
  38.             this.mapView.getOverlays().add(mScaleBarOverlay);
  39.     }
  40. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


main.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    >
  7.  
  8.  
  9.         <org.osmdroid.views.MapView
  10.                 xmlns:android="http://schemas.android.com/apk/res/android"
  11.                 android:layout_width="fill_parent"
  12.         android:layout_height="fill_parent"
  13.         android:id="@+id/mapview"
  14.         ></org.osmdroid.views.MapView>
  15.  
  16. </LinearLayout>
  17.  
  18.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
Last edited by sorer on Thu Jan 20, 2011 11:57 am, edited 1 time in total.
sorer
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sun Sep 12, 2010 6:14 pm

Re: OpenStreetMap MapView for Android

Postby nicolas83 » Tue Jan 18, 2011 9:51 pm

Hi,

just found this (quite old) forum thread while searching for an alternative to GoogleMap MapView which supports offline rendering. The osmdroid project really seems promising. In this context I also stumbled upon a project called mapsforge (http://mapsforge.org). The screenshots (http://code.google.com/p/mapsforge/) of their maps look really great. They claim that they have offline maps (rendering on device) and that their MapView is compliant with the GoogleMaps API (just substitute the imports...). Has anyone tried their API? Can anyone suggest me which library to use (osmdroid, mapsforge, or other I'm not aware of)?

Best regards
nicolas83
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Jan 18, 2011 9:28 pm

Top

Re: OpenStreetMap MapView for Android

Postby Fritte » Thu Jan 20, 2011 11:19 am

Hello, and thank you for the help, I finally found it.
And I even found why it posted twice my map.
Thank you and continued like that
Fritte
Freshman
Freshman
 
Posts: 2
Joined: Tue Jan 18, 2011 11:50 am

Re: OpenStreetMap MapView for Android

Postby sorer » Thu Jan 20, 2011 12:01 pm

I slightly rewrite my code to use osmdroid mapview, not old openstreetmapview, because some overlays like mylocationoverlay not worked. Now is all ok. Codes and download link are updated.
sorer
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sun Sep 12, 2010 6:14 pm

Re: OpenStreetMap MapView for Android

Postby Ulathar » Tue Jan 25, 2011 2:59 pm

sorer wrote:I slightly rewrite my code to use osmdroid mapview, not old openstreetmapview, because some overlays like mylocationoverlay not worked. Now is all ok. Codes and download link are updated.



thanks a lot i finally got the trick and it works now!

but i have one question:
in your example code you use another lib (slf4j-android-1.5.8.jar) instead of the latest osmdroid-android-3.0.0.jar.

is that lib free to use (GNU licence?) as well? and where does it come from?
when i do NOT use that lib (slf4j-android-1.5.8.jar) and keep using the osmdroid libs only, i get this error:

http://img209.imageshack.us/i/errordu.jpg/

so i wonder whats wrong (it works when i use your lib...).
Ulathar
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sat Jun 05, 2010 9:55 pm

Re: OpenStreetMap MapView for Android

Postby sorer » Tue Jan 25, 2011 4:23 pm

It seems thats a logging framework. See http://www.slf4j.org/android/
I probably grab it from their openstreetmapviewer in svn.
sorer
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sun Sep 12, 2010 6:14 pm

Re: OpenStreetMap MapView for Android

Postby Ulathar » Tue Jan 25, 2011 4:33 pm

so you dont know why your programm wont work without that lib? (i compiles just fine but crashes at runtime without that lib)
Ulathar
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sat Jun 05, 2010 9:55 pm

Re: OpenStreetMap MapView for Android

Postby sorer » Tue Jan 25, 2011 4:42 pm

yes, it's wierd, but it works. I dont know why this assembly dont get directly from osmdroid-android-3.0.0.jar.
sorer
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sun Sep 12, 2010 6:14 pm

Re: OpenStreetMap MapView for Android

Postby Ulathar » Tue Jan 25, 2011 5:41 pm

ok but you are sure that its "opensource" aka free to use under GNU licence? because i use it in a project at the university and dont want to get into trouble ;).
Ulathar
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sat Jun 05, 2010 9:55 pm

Re: OpenStreetMap MapView for Android

Postby sorer » Tue Jan 25, 2011 5:52 pm

sorer
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sun Sep 12, 2010 6:14 pm

Re: OpenStreetMap MapView for Android

Postby Ulathar » Tue Jan 25, 2011 5:59 pm

great thanks dude! you saved me hell a lot of time and frustration ;).
also i still cant understand why it crashes without but hey it doesnt matter right now!
Ulathar
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sat Jun 05, 2010 9:55 pm

Top
PreviousNext

Return to Map Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests