Downloading specific maptiles to cache in OSMDroid - android

I am currently working on a school project and I am trying to learn the techniques on downloading OSM map tiles into the phone's local memory/cache, so that users are able to view the maps when offline.
This is to be done by giving a specific location, and the map tiles that are within specific radius/distance from the location will be downloaded into the phone memory (when there is online connection) for offline display.
However, I am having some slight trouble in understanding OSMDroid's APIs. From my understanding, it seems that the classes involved are:
MapTileBase
MapDownloader
IFilesystemCache
Please do correct me if I'm mistaken.
Also, in MapTileBase (which I believe is the provider), the getMapTile returns android.graphics.drawable.Drawable.
Is this Drawable class even related to the concept of offline map display?
Hope someone can enlighten me on where exactly maps are being called to be downloaded into the phone's memory, and how we can edit the maps to display interface items such as routes, pins, etc.

I don't have an exactly answer as you're looking for a method to cache offline outside of an area the MapView hasn't already navigated to. The built in caching support is there, but not exactly how you want to do it.
You're going to need online download capabilities if you plan to cache and not just supply your own offline map tiles. I would take a look at the MapTileDownloader and maybe try to implement your own version of it. The MapTileDownloader.TileLoader.loadTile() method takes a MapTileRequestState which I believe the MapTileProviderArray calls when a map tile is needed.
If you can replicate the existing MapTileDownloader's TileLoader functionality, you can probably inject your own relative MapTileRequestState or straight up MapTile's to the one requested. e.g. Take the TileLoader called MapTileRequestState, inspect it, figure out how to construct new MapTileRequestState/MapTile representing tiles outside the current view. It won't be easy though as you'll have to deal with the different zoom levels, and depending on the zoom level, the radius should be smaller, etc... A lower zoom level of 18 would have a larger tile radius than a higher 10.
Personally I was recently working on trying to work 100% offline so I don't have any example code to help. Good luck even though its been a couple of months.

First, go right the source
https://github.com/osmdroid/osmdroid
There's a few wiki articles that will help, especially the offline map tiles article. In short, use MOBAC or the tool osmdroid provides. OSMBonuspack also has a tool to perform the same task on device.
Map tiles are actually downloaded using this class
https://github.com/osmdroid/osmdroid/blob/master/osmdroid-android/src/main/java/org/osmdroid/tileprovider/modules/MapTileDownloader.java

Related

navigation app with a different route finding algorithm

I wish to make an android app that leverages OSM services and runs an algorithm of my choice to find the route, say A* or some heuristic on Dijkstra.
The Routing wiki page of OSM has some good pointers but it does not give any insight about how to go about implementing your own algorithm in the app.
This response is overview which might help you to orient better in the landscape and find a way to approach the problem. Your question is too broad to give an exact simple answer but I do understand why are you asking such question.
You can use Osmdroid library to display the route you have on Android.
Keep in mind that the core feature of Osmdroid is to display the map tiles. Map tiles are just bitmaps so you need either an online source of tiles or an offline one. How to do that is covered in the documentation and partly in other questions on SO. It's a too big topic to explain it here in detail. Just to make it clear: .osm file with raw OpenstreetMap data cannot be directly rendered by Osmdroid. Map tiles are usually rendered from that data in quite complex way and not on the fly.
Another very important feature in Osmdroid library is the ability to display so called Overlays. E.g. Markers, polygons etc. You may use those to draw your route on the map and other features you may wish to display. This is again covered in the documentation and too big to go in details here.
There is a support for routing for Osmdroid (AFAIK in Osmbonuspack) but that's about using some 3rd party service to get your routing data and displaying that. According to your comment you want to implement your own algorithm and therefore this would be no use to you.
So the last part is how to integrate your algorithm into that. That's not a simple question with a simple answer. You'll need to do almost everything on your own, starting with getting the data you need to run the algorithm from the OpenstreetMap database (.osm file). I don't think there's an API which would provide you reasonable way to access the data in a form suitable for route computation. The approach I would choose is to develop some kind of preprocessing which would take either .osm file or a database with the data an produce my own structure representing the graph for navigation. I would than include this graph in my application and run my algorithm on that data. The output of my algorithm would be the route: a list of coordinates which I would use to draw the polyline.
If the area you need to cover is small enough, you may start with including the .osm file in your project and running the algorithm in the same was as you do now. Maybe it's going to be good enough for your situation.
One interesting project to look at to find inspiration is Graphopper - they have build exactly what I said above and they use it to provide a library for Android/iOs and a web API. But the core is the same - pre-process OSM data into graph data and run the algorithm on that. Since you wish to implement the algorithm on your own this library will not help you (unless you fork the project and modify it) but it might be a valuable learning resource.

OSMdroid and MBTiles: get area information

I am planning to build sort of a geography quiz app where you have to touch inside the borders of the searched country. So, I have to display a map (I want it to be offline) and be able to test the user click against the map in some way.
I've been searching a while now and found good OSM material from Natural Earth Data and TileMill for converting it to a MBTiles-file (sqlite-database containing all the png's and some meta-data too (using UTFGrid)).
On the Android-side, I would like to use OSMdroid for displaying the maps, mainly because it's open-source and highly maintained.
I've found some guides on how to use the MBTiles-database inside osmdroid but is there also an easy way to access the metadata or check otherwise if the user touched inside the right country area? I would like to avoid some sort of "boundary boxes"...
Any help is highly appreciated!
I'm pretty sure that bounding box calculation is best option.
Can you access metadata from the offline tile source? Yes, kind of. You can get a list of unique tile sources in the archive but that's it. There's an open issue regarding getting the bounds of a given offline archive. You can follow it here (or contribute)
https://github.com/osmdroid/osmdroid/issues/174
It's slightly complex because offline tiles are going to have a bounds per layer/source and per zoom level. There's also no guarantee that that bounds of that given zoom/layer is going to be contiguous. I.E. one zoom level can have both the US and western Europe in it.

Storing (caching) Google map with given radius and using it while offline

First of all if this one is a duplicate, please refer me to the original thread(s), as I have failed to find the exact thing I was searching for.
Basically for some time now I am developing an application for the client and it was all going well up until this point.
The client requests to be able to
I) While online:
Access Google map
Set the point by clicking on map
Store a portion of the map around the above mentioned point locally as per the radius he/she selected (eg. 3km)
II) While offline:
Access the map he stored in above steps.
The application should draw the path as he moves as per GPS coords received from another device via BT.
The ability to zoom in/out, scroll around map and everything else that standard google map allows.
I am completely lost on this one and would like at least someone to point out the right direction for something like this.
I'm well aware how to handle most of the above while Android device is online, but have no idea how to do this while offline.
I pretty much need help with I-3, II-1, and maybe II-3.
PS. I don't really need BT tutorials and guidance (mentioned in II-2), that part is handled, I just pointed out the source of coords.
Cheers
You can use TileProvider. The TileProvider provides the images that are used in the tile overlay. You should be able to save portion of map and create your own tileprovider and access tiles locally.

Alternative(To Google Maps) Open Source Map API that provides a database of nodes and Arcs on roads

I need a Map API for Android that can provide me with indexed nodes and indices that make up the road network. The main idea is to determine if two GPS devices are on the same road. Thank you in advance
A Map API by itself will not have that information. Anyway, you can get it from OpenStreetMap freely. You can download it from here.
I don't understand from your question if you intend on displaying the results on a map. If so, and you want a nice and free map API, I would suggest Leaflet. It's not as mature as the likes of OpenLayers but, as you've tagged this post with "android", Leaflet just kicks ass in the mobile department.
OpenStreetMap is definitely a good source of data for this kind of project. Unlike google maps, it gives developers access to the underlying vector data of a map (fully open). This allows interesting new use cases which simply are not possible with google maps, and something involving geometric calculations like this would definitely fit into that category. You either need OpenStreetMap or some other source of "vector" map data, and beyond OpenStreetMap this can be expensive.
Unfortunately that's not the full answer to your question. You still have a lot of work to do to use the data in the way you intend. You need to calculate the proximity of two points (GPS readings from two devices?) to nearby roads, and figure out which road the point lies closest to. It's the kind of powerful geo calculation you might do using a GIS package such as QGIS or a functions of a geo-aware database system PostGIS.
But that's not the answer to your question, because you need to do these calculations on device. I'm not aware of an off-the-shelf library to do this on android. I think you would have to roll your own.
But another challenge is to get the vector data onto the device in a suitable format in the first place, and this is the first thing to solve. You'd want the vector data either as a large download for a whole country, or perhaps a smaller area, perhaps with an on-the-fly download feature within this app. Whole countries are not infeasible when working with maps in vector form (ever tried the awesome MayDroyd app?), but require some compact formatting. Happily some of these problems are starting to be solved in open source off-the-shelf libraries. You could try to build on top of MapsForge for example.
So then you're back to the challenge of writing on-device code to poke around in this data and do the calculations you want to do. I suppose it could be rather good if projects like MapsForge included generic PostGIS style geo-functions to make this easier. Something to ask the mapsforge developers about perhaps.

Android Google Maps Maritime charts layers

I'm looking at displaying a map within my Android app that presents maritime charts, and also needs to work offline as there will be no network connectivity of any type available. I suspect that I will get my maritime charts supplied as a series of bitmap files with LAT/LON references of the area clipped.
My question is this - how do I go about implementing this ? I guess I have to add them as a layer somehow, can someone show me how to do this ?
Also, as the device will be offline, will there be a problem trying to use Google maps even if all the content is held locally, will the API just "not work" until it has connectivity ?
Thanks in advance.
As this just earned Tumbleweed, I thought I should update it.
I tried really hard to use the Maps API offline, with my bitmap as a layer - it didn't work out. The API is too reliant on the interweb and just seems to timeout when trying just about anything.
Simple solution, I have just extended the ImageView class to display my bitmap chart & overlays.

Categories

Resources