Click here to Skip to main content
Click here to Skip to main content

Custom Map Tiles (Android)

, 24 Sep 2012
Rate this:
Please Sign up or sign in to vote.
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:

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

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

<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:

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.

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)

Share

About the Author

valeriyabobko

Belarus Belarus
Developing in C++, C#, .Net, Java, Delphi; working with SQL, HTML, JavaScript, CSS
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 24 Sep 2012
Article Copyright 2012 by valeriyabobko
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid