I'm trying to load a large number of tile overlays from a folder in Assets in my Android project. I can get tiles to load from a remote url, but not from a local folder. Any ideas what I'm doing wrong here?
In my Activity
public void OnMapReady(GoogleMap map) { map.MapType = GoogleMap.MapTypeNormal; var options = new TileOverlayOptions(); options.InvokeTileProvider(new LocalTileProvider()); map.AddTileOverlay(options); }
and the UrlTileProvider
public class LocalTileProvider : UrlTileProvider { public LocalTileProvider() : base(256, 256) { } public override URL GetTileUrl(int x, int y, int z) { string s = "file:///android_asset/tiles/" + z.ToString() + "/" + y.ToString() + "/" + x.ToString() + ".jpg"; //string s = "http://b.tile.openstreetmap.org/" + z + "/" + x + "/" + y + ".png"; // <= this works return new URL(s); } }
If you want to use a local resource we need to use ITileProvider
instead of UrlTileProvider
. Make your activity implement the UrlTileProvider
. Then apply it like:
public Tile GetTile(int x, int y, int zoom) { var bytes = default(byte[]); using (StreamReader reader = new StreamReader(Assets.Open("image.jpg"))) { using (var memstream = new MemoryStream()) { reader.BaseStream.CopyTo(memstream); bytes = memstream.ToArray(); } } return new Tile(256, 256, bytes); } public void OnMapReady(GoogleMap map) { map.MapType = GoogleMap.MapTypeNormal; var tileOverlayOptions = new TileOverlayOptions() .InvokeTileProvider(this); map.AddTileOverlay(tileOverlayOptions); }
Answers
If you want to use a local resource we need to use
ITileProvider
instead ofUrlTileProvider
. Make your activity implement theUrlTileProvider
. Then apply it like:@LandLu Many thanks. I was stuck on this for hours. Works fine now.
@PhilipJohn Would you mind to share your solution code with multiple tile images?
Thanks.
@tnx I'm really sorry, but I no longer have access to that code.