amay077 / Xamarin.Forms.GoogleMaps

Map library for Xamarin.Forms using Google maps API
https://www.nuget.org/packages/Xamarin.Forms.GoogleMaps/
MIT License
546 stars 346 forks source link

[question] MbTiles support #623

Closed petr-pokorny-1 closed 5 years ago

petr-pokorny-1 commented 5 years ago

This is rather question that bug report. Is there any code sample showing how to display raster layer using tiles from local MbTiles file?

tekinc commented 5 years ago

I was able to accomplish this by using the "FromSyncImage" method. You can store your images in your XamarinForm Project and rather than the native projects. Make sure you sent their build types to "Embedded Resource". I believe in "Droid" the folder path name to the resource files requires an "_". Check the code below. Hope this helps.

objTile = TileLayer.FromSyncImage((int x, int y, int zoom) =>
            {
                var assembly = this.GetType().GetTypeInfo().Assembly;                 byte[] buffer;
                                Stream stream = null;
                try
                {

                    stream = assembly.GetManifestResourceStream($"XamarimFormProjectNamespace.Res._{zoom}._{x}.{y}.png");

                    long length = stream.Length;
                    buffer = new byte[length];
                    stream.Read(buffer, 0, (int)length);

                    return buffer;
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }
                finally
                {
                    if (stream != null)
                        stream.Close();
                }
                return appImage;
            });
amay077 commented 5 years ago

Google Maps can not read "MbTiles" .

MbTiles is "MapBox Binary VECTOR Tile", but Google Maps can not render vector tile.

petr-pokorny-1 commented 5 years ago

Google Maps can not read "MbTiles" .

MbTiles is "MapBox Binary VECTOR Tile", but Google Maps can not render vector tile.

Thanks, but your comment is not accurate, MBTiles format is designed to support both raster and vector data.

Raster version is used quite often

amay077 commented 5 years ago

Hi @spatialguy, sorry you're correct.

If you want to use format: png/jpeg/etc, you can use @tekinc 's suggested code.

  1. Read MBTile file using stream = assembly.GetManifestResourceStream(...
  2. Get image from MBTile file.
  3. Return image's byte array.
petr-pokorny-1 commented 5 years ago

Hi @spatialguy, sorry you're correct.

If you want to use format: png/jpeg/etc, you can use @tekinc 's suggested code.

  1. Read MBTile file using stream = assembly.GetManifestResourceStream(...
  2. Get image from MBTile file.
  3. Return image's byte array.

Thanks @tekinc , @amay077 . I was able to get it working using BruTile library. The trick is to correctly convert tile coordinates between Google Maps and TMS:

                var mbTiles = _mbTiles.Value; // open mbTiles file using brutile
                var layer = TileLayer.FromSyncImage((int x, int y, int zoom) =>
                {
                    var yMax = 1 << zoom;
                    var y1 = yMax - y - 1; // from Google Maps tiling to TMS
                    var tileIndex = new TileIndex(x, y1, zoom.ToString());
                    var tileInfo = new TileInfo {Index = tileIndex};
                    var data = mbTiles.GetTile(tileInfo);
                    return data;
                });

and code to open MbTiles file:

        private readonly Lazy<MbTilesTileSource> _mbTiles = new Lazy<MbTilesTileSource>(
            () =>
            {
                return new MbTilesTileSource(
                    new SQLiteConnectionString(ResourcesDeployService.MbTilesFilePath, false));
            }) ;
ssombat commented 4 years ago

@spatialguy Thanks for those pieces of code, it helped me out with loading an mbtiles file. I'm trying to load multiple mbtiles together, to use as layering (for example I have a custom map base layer, I want to ability to toggle on/off another layer for parking meters). I have issue where the parking meter layout transparent segments are blacked out like this image This causes my base layout to not show. Have you had any experience with this issue? I appreciate any help I can get, thanks.