Map API

Nutiteq Maps Lib SDK enables developing advanced mobile mapping applications.
Platforms: Android, RIM BlackBerry, Java ME (J2ME)
Download maps API SDK »
Untitled Document

Products

» View demos
» Downloads

Latest news

Last at Tue, 6 Dec 2011 18:37:53
Topic: New BlackBerry sample app
See more news »

Mobile map with MapCruncher

13 replies [Last post]
jaak
User offline. Last seen 13 hours 13 min ago. Offline
Joined: 06/19/2008

As asked in a different topic: is it possible to show own map image on mobile. Yes, it is; and probably the easiest way to do it is using MSR Mapcruncher tool.

Basic steps are following:

  1. Download and install MapCruncher from here
  2. Open your map in MapCruncher and georeference it. Best if you have some exact coordinates, like corner coordinates; but you can use Microsoft maps as reference also. Generally more control points is better. Render map (it may take time). This generates 256x256 map tiles, like needed for Nutiteq map.
  3. Upload directory with tile image PNG files (usually \Layer_NewLayer) to a web server URL. You can preview and test map registration using MapCruncher generated web pages.
  4. Add new custom map to your midlet. Create class MyQuadKeyMap with following contents:
    import com.nutiteq.maps.projections.EPSG3785;
    import com.nutiteq.maps.*;
    
    public class MyQuadKeyMap extends EPSG3785 implements GeoMap, OnlineMap  {
    
    	private String baseUrl;
    
    	public MyQuadKeyMap(final String baseUrl) {
    		super("My Map", 256, 1, 17);
    		this.baseUrl = baseUrl;
    	}
    
    	public String buildURL(final int x, final int y, final int zoom) {
    		final int tmpX = x >> 8;
    		final int tmpY = y >> 8;
    		final StringBuffer buf = new StringBuffer();
    		buf.append(baseUrl);
    
    		for (int i = zoom - 1; i >= 0; i--) {
    			buf.append((((tmpY >> i) & 1) << 1) + ((tmpX >> i) & 1));
    		}
    		buf.append(".png");
    		
    		return buf.toString();
    	}
    }
    
    
  5. Activate the map, using setMap method of MapComponent or Mapitem:
    map.setMap(new MyQuadKeyMap("MY-PUBLIC-WEB-URL"));
    
  6. Build and run. Initially you may get red map, this means that there is no map for this area. MapCruncher generates tiles only for the area covered by the map. Now go to a location which is covered by the tile (I would do this in the code using setMiddlePoint method). You should see the zoomable map.

Some notes:

  • It might be difficult to get GPS level accuracy, unless source map is in projection which is easily convertible to the Spherical Mercator. Mercator or LatitudeLongitude should be quite ok. For best accuracy you should use directly original projection for your map, but MapCruncher cannot be used then.
  • Custom map interfaces will change a bit in next Lib 1.0 release, just minor changes. I can post update if you need it.

/JaakL

jaak
User offline. Last seen 13 hours 13 min ago. Offline
Joined: 06/19/2008

Now step 2: maybe you want your map as overlay, not as a base map. Just like MSN maps shows it.

This is also quite simple:

  1. Create custom overlay class MyQuadKeyOverlay:
    import com.nutiteq.components.MapTile;
    import com.nutiteq.maps.MapTileOverlay;
    
    public class MyQuadKeyOverlay implements MapTileOverlay {
      
    	private final String baseUrl;	
    	
    	public MyQuadKeyOverlay(final String baseUrl) {
    
    		this.baseUrl = baseUrl;
    	}
    	
      public String getOverlayTileUrl(final MapTile tile) {
        
        final int tmpX = tile.getX() >> 8;
    	final int tmpY = tile.getY() >> 8;
    	
        final int zoom = tile.getZoom();
    
        final StringBuffer url = new StringBuffer(baseUrl);
    	
    	for (int i = zoom - 1; i >= 0; i--) {
    		url.append((((tmpY >> i) & 1) << 1) + ((tmpX >> i) & 1));
    	}
    	url.append(".png");
    
        return url.toString();
      }
    }
    
  2. Define map with overlay, using e.g. OpenStreetMap as base:
      GeoMap myMap=OpenStreetMap.MAPNIK;
      myMap.addTileOverlay(new MyQuadKeyOverlay("MY-SERVER-URL"));
      map.setMap(myMap);
    

See attached sample result.

It might be especially interesting if you have transparent or semi-transparent images. MapCruncher should support it, if original source has transparency.

MSR overlay sampleMSR overlay sample

/JaakL

IlBellucci
User offline. Last seen 2 years 1 week ago. Offline
Joined: 01/19/2010

I can't load the map. I did everything in post #1, except that OnlineMap couldn't be found and I adopted LocalMap, which anyway is what I needed. I use map.setMap("mymap") but all I get is a white map where ever I go.
What's wrong?

jaak
User offline. Last seen 13 hours 13 min ago. Offline
Joined: 06/19/2008

In recent Lib versions your map service should have "... implements GeoMap, UnstreamedMap ...", instead of OnlineMap. Also if it uses map tiles from JAR or local file system storage. LocalMap is more or less deprecated.

/JaakL

IlBellucci
User offline. Last seen 2 years 1 week ago. Offline
Joined: 01/19/2010

thanks, that didn't solve the problem but I guess it's one step closer to the solution :)
So my version of MyQuadKeyMap has the exact same code except for the OnlineMap that is now UnstreamedMap

my code to display the map is the following:

[...]
private MapItem mapItem;
[...]
//Inside the class builder
mapItem = new MapItem("Map", "test", "test2", "test1", 300, 150, new WgsPoint(47.64028017, -122.12903380), 12);
mapItem.setMap(new MyQuadKeyMap("maptest/"));
[...]

"maptest/" is the path in wich the .png files made by the cruncher are stored.
Just to let you know: the code in which I'm trying to use the custom map is the HelloMap code in the PDF tutorial.
I just added the setMap() line.
The problem is that I get a white screen insted of getting the map that I got with the cruncher. It's strange because normally tiles that can't be found should be red, am I wrong?

jaak
User offline. Last seen 13 hours 13 min ago. Offline
Joined: 06/19/2008

If you use exactly the same MyQuadKeyMap as given above, then library tries to make URL "maptest/.png" and this does not have any reference what should be image source: should it be on-line or offline.

URL should start with
# http:// or https:// - on-line source
# file:// - if it is filesytem
# / - file is in application package (as resources)

If it does not start with any of these then in principle you should have in the log a message (and exception) like "Don't know what to do with...". There is exception and this is why you do not get even the red image.

Usually filesystems have their root directory, and this is different on different devices. With WTK emulator your code should be something like mapItem.setMap(new MyQuadKeyMap("file://root1/maptest/")).

If you do not know name of root directory then I suggest to use e.g. StoredMaps sample (included with j2me package), it has also browser to find correct directory.

/JaakL

kibnelson
User offline. Last seen 1 year 17 weeks ago. Offline
Joined: 01/08/2010

Have followed the example above but am getting a red map i know its got do with not getting the tiles but i dont know how fix that please help me
may be on how to point tile locations correctly-with an example i will appreciate.

kibnelson
User offline. Last seen 1 year 17 weeks ago. Offline
Joined: 01/08/2010

Here is my code please check out then please tell me where the error is
this is for the MyQuadKeyMap as you showed above
public class MyQuadKeyMap extends EPSG3785 implements GeoMap, UnstreamedMap {
private String baseUrl;
public MyQuadKeyMap(final String baseUrl) {
super("My Map", 256, 12, 13);
this.baseUrl = baseUrl;}
public String buildPath(final int x, final int y, final int zoom) {
final int tmpX = x >> 8;
final int tmpY = y >> 8;
final StringBuffer buf = new StringBuffer();
buf.append(baseUrl);
for (int i = zoom - 1; i >= 0; i--) {
buf.append((((tmpY >> i) & 1) << 1) + ((tmpX >> i) & 1));}
buf.append(".png");
return buf.toString();
}
}

and the midlet code that implements the map is in
mapItem = new MapItem("Map", "test",this, 300, 150, new WgsPoint(.25783452, 35.08707047), 14);
mapItem.setMap(new JaredOpenStreetMap(256, 15, 16));
mapItem.setMap(new MyQuadKeyMap("/Layer_NewLayer/"));
mapItem.setMiddlePoint(.25783452, 35.08707047, 13);

so the problem is that the png tile are in a format like 1223322.png but i know you have to change to a format like zoom_x_y.png but how do you do that anybody can help me

jaak
User offline. Last seen 13 hours 13 min ago. Offline
Joined: 06/19/2008

JaredOpenStreetMap requires zoom_x_y.png format, this MyQuadKeyMap (actually latest library already includes similar com.nutiteq.maps.QKMap) uses quadkey, same format like MapCruncher uses.

What is in your log? This should indicate what tiles exactly are searched for and from where. So you can check whether you have particular tile, or just directory is wrong.

Some devices/SDK-s do not handle well subdirectories under /res/ directory. I would suggest to copy all map PNG files to /res directly, without subdirectory, and remove directory name from the MyQuadKeyMap constuctor call also. Maybe this is your issue

/JaakL

kibnelson
User offline. Last seen 1 year 17 weeks ago. Offline
Joined: 01/08/2010

Thanks jaakl it worked,
Is it possible to implement direction using cloudmade on you own map tiles from mapcrancher?
If not what are you advising me to do?

jaak
User offline. Last seen 13 hours 13 min ago. Offline
Joined: 06/19/2008

Yes. Note that this works on-line only, not using the offline maps.

/JaakL

kibnelson
User offline. Last seen 1 year 17 weeks ago. Offline
Joined: 01/08/2010

Hi
Thanks for responding
After storing your tiles from mapcrancher in an online server and you can access, it how do you implement cloudmade on it,
is there an example that i can follow or give me example that implements it?
I will appreciate.

jaak
User offline. Last seen 13 hours 13 min ago. Offline
Joined: 06/19/2008

Map tiles and directions service is independent from each other. J2ME samples directory has demo application "directions" source, which shows how to use different routing services, and how route line is shown on the map.

/JaakL