Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for OSM Android Offline Map Library
EGEMEN
EGEMEN

Posted on

     

OSM Android Offline Map Library

Offline OpenStreet Map Library (No Internet Required) You dont have to even one-time connect!

Offline OpenStreetMap Library

Performance friendly and scalable

Why this project exists

Offline map usage is kind a problem for developers and there are rare documentations. I believe this library will helpful for developers.

Features and Usage

Implementation

Add it in your root build.gradle at the end of repositories
repositories{maven{url'https://jitpack.io'}}
Enter fullscreen modeExit fullscreen mode
Add the dependency
implementation'com.github.AsynctaskCoffee:AndroidOfflineMapLibrary:v1'implementation'com.github.MKergall:osmbonuspack:6.6.0'implementation'org.osmdroid:osmdroid-android:6.1.0'
Enter fullscreen modeExit fullscreen mode

You need to download offline map-tiles as SQLite format. And you should put it under assets folder. Let me explain how to download offline map-tiles step-by-step;

1. First you need to download Mobile Atlas Creator fromHERE
2. Open MOBAC and select Osmdroid SQLite

3. Select map source (selecting osm is better) and zoom-levels (15-14-13-12 are ideal) as you desire from left panel.

4. Draw a area (small areas consume less storage). And press add selection button from left panel.

5. As you can see layers are selected. After selecting layers press Create Atlas button.

6. Select 'Ignore download errors and continue automatically' and continue.

7. After finishing download process rename the SQLite file as 'map.sqlite' and copy it into assets folder.

8. And yes! You completed the hard part. Rest of the steps just coding few lines.

Java

publicclassMainActivityextendsAppCompatActivityimplementsMapListener,GeoPointListener{OfflineMapViewofflineMapView;MapUtilsmapUtils;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);offlineMapView=findViewById(R.id.map);offlineMapView.init(this,this);}@OverridepublicvoidmapLoadSuccess(MapViewmapView,MapUtilsmapUtils){// GeoPoint belongs to ISTANBUL heart of the world :)this.mapUtils=mapUtils;offlineMapView.setInitialPositionAndZoom(newGeoPoint(41.025135,28.974101),14.5);// 41.025135, 28.974101 Galata TowerMarkermarker=newMarker(mapView);marker.setPosition(newGeoPoint(41.025135,28.974101));marker.setIcon(getResources().getDrawable(R.drawable.galata_tower));// marker.setImage(drawable);marker.setTitle("Hello Istanbul");marker.showInfoWindow();mapView.getOverlays().add(marker);mapView.invalidate();}@OverridepublicvoidmapLoadFailed(Stringex){Log.e("ex:",ex);}@OverridepublicvoidonGeoPointRecieved(GeoPointgeoPoint){//Selected GeoPoint Returns HereToast.makeText(this,geoPoint.toDoubleString(),Toast.LENGTH_SHORT).show();}publicvoidactivateAnimatePicker(Viewview){if(mapUtils!=null)offlineMapView.setAnimatedLocationPicker(true,this,mapUtils);}}
Enter fullscreen modeExit fullscreen mode

Kotlin

classMainActivityKotlin:AppCompatActivity(),MapListener,GeoPointListener{lateinitvarofflineMapView:OfflineMapViewoverridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)setContentView(R.layout.activity_main_kotlin)offlineMapView=findViewById(R.id.map)offlineMapView.init(this,this)}@SuppressLint("ShowToast")overridefunonGeoPointRecieved(geoPoint:GeoPoint?){//Selected GeoPoint Returns HereToast.makeText(this,geoPoint?.toDoubleString(),Toast.LENGTH_SHORT).show()}overridefunmapLoadSuccess(mapView:MapView?,mapUtils:MapUtils?){// GeoPoint belongs to ISTANBUL heart of the world :)offlineMapView.setInitialPositionAndZoom(GeoPoint(41.011099,28.996885),15.5)offlineMapView.setAnimatedLocationPicker(true,this,mapUtils)}overridefunmapLoadFailed(ex:String?){Log.e("ex",ex.orEmpty())}}
Enter fullscreen modeExit fullscreen mode

Or XML

<egolabsapps.basicodemine.offlinemap.Views.OfflineMapViewandroid:id="@+id/map"android:layout_width="match_parent"android:layout_height="match_parent"app:initialFocusLatitude="41.025818"app:initialFocusLongitude="28.973436"app:zoomLevel="15"/>
Enter fullscreen modeExit fullscreen mode
Result: Say Hi to ISTANBUL :)

Top comments(6)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
asafeca profile image
A. Safeca
I am just a guy who wants to improve day after day
  • Location
    Luanda
  • Work
    Software engineer at ItGest
  • Joined
• Edited on• Edited

Hi?
Thanks for sharing, but the map does not load. And the Logcat says permission is denied. I am sending the image with the error reproduced.
And I believe that the problem lives here:
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/osmdroid/");
"osmdroid/" directory does not even exist!

CollapseExpand
 
asynctaskcoffee profile image
EGEMEN
endangered software developer #kotlin #java
  • Location
    Stockholm
  • Education
    Computer Engineering
  • Work
    Software Engineer at DonanımHaber
  • Joined

I think you have to give hardware read/write runtime permission. Because library creates this folder.

CollapseExpand
 
mr_bayat_95f221f4d3489ff profile image
Mr. Bayat
  • Joined

this project have this error "Map load failed Asset files cannot copied" for samsung A34 android 13, . but for android 9 it work well.... what's wrong?????? i add all runtime permission but it dont work well.

CollapseExpand
 
adna973 profile image
adna973
  • Joined

hello friend can make more deletes for that project thanks

CollapseExpand
 
mr_bayat_95f221f4d3489ff profile image
Mr. Bayat
  • Joined

use this ** maven { url = uri("jitpack.io" ) }**

CollapseExpand
 
mr_bayat_95f221f4d3489ff profile image
Mr. Bayat
  • Joined

maven { url = uri("jitpack.io" ) }

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

endangered software developer #kotlin #java
  • Location
    Stockholm
  • Education
    Computer Engineering
  • Work
    Software Engineer at DonanımHaber
  • Joined

More fromEGEMEN

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp