Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

Custom Map Tiles (Android)

4.00/5 (1 vote)
24 Sep 2012CPOL1 min read 22.7K   498  
A simple example of using custom tile server for Android maps using OSM (Open Street Map) library

Introduction

In this article, I would like to show a simple example of using custom tile server for Android maps using OSM (Open Street Map) library.

Using the Code

Implementing custom tile sources is rather simple. First thing you need to do is to add open-street-map jar to your projects. For newbies:

A Best way to add External Jars to your Android Project or any Java project is:
  1. Create a folder called 'libs' into your project root folder
  2. Copy your Jar files to the libs folder
  3. Now right click on the Jar file and then select Build Path > Add to Build Path, this will create a folder called 'Referenced Library' into your project, and you are done

Here, you can find the latest version of osmdroid-android.jar.

Now don't forget to include Internet Permission to your Manifest file:

XML
<uses-permission android:name="android.permission.INTERNET" /> 

and another one permission for tile provider to access network state:

XML
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

After you have successfully added library to your project, you need to implement TileSource. To do that, you simply have to create your class, let's say, called MyTileSource:

Java
public MyTileSource(String aName, string aResourceId, int aZoomMinLevel, 
			int aZoomMaxLevel, int aTileSizePixels, 
			String aImageFilenameEnding, String[] aBaseUrl) { 
		super(aName, aResourceId, aZoomMinLevel, aZoomMaxLevel, aTileSizePixels, 
				aImageFilenameEnding, aBaseUrl); 
	} 
	@Override 
	public String getTileURLString(MapTile aTile) { 
	
	return getBaseUrl() + "?request=getTile&apikey="+ MY_API_KEY +
         "&LayerName=E50931C3B2DD4E0FA2C03366552EEAA1" + "&z=" + aTile.getZoomLevel() + "&x=" + 
		aTile.getX() + "&y=" + aTile.getY(); 
	} 
} 

The implementation is really simple. All you need is to override method getTileURLString.

In this method, you have to form the URL to your custom tile. Mine was something like http://maps.kosmosnimki.ru/TileService.ashx?request=getTile&apikey=P8DQBF1TBW&LayerName=E50931C3B2DD4E0FA2C03366552EEAA1&z=9&x=2&y=2.

After that, you just need to set the TileSource of your map to your custom MyTileSource.

Java
ITileSource _ITileSource = new MyTileSource("E50931C3B2DD4E0FA2C03366552EEAA1". null, 2, 13, 
256, "", "http://maps.kosmosnimki.ru/TileService.ashx");  
mapView.setTileSource(_ITileSource);   

History

I would be happy if I could help any of you in your work! Leave a comment, so I will try to improve my articles!

If you need the source code - it's available here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)