首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >谷歌地图Tile Layer in Kotlin

谷歌地图Tile Layer in Kotlin
EN

Stack Overflow用户
提问于 2019-03-05 17:43:25
回答 2查看 885关注 0票数 0

我正在尝试使用Google实现瓷砖层,但是官方文档中只有Java中的代码示例,我的项目是用Kotlin编写的。我在kotlin找不到关于如何做同样的事情的例子。有人知道怎么做吗?

文档java示例代码

代码语言:javascript
复制
private GoogleMap mMap;

TileProvider tileProvider = new UrlTileProvider(256, 256) {
  @Override
  public URL getTileUrl(int x, int y, int zoom) {

    /* Define the URL pattern for the tile images */
    String s = String.format("http://my.image.server/images/%d/%d/%d.png",
        zoom, x, y);

    if (!checkTileExists(x, y, zoom)) {
      return null;
    }

    try {
      return new URL(s);
    } catch (MalformedURLException e) {
        throw new AssertionError(e);
    }
  }

  /*
   * Check that the tile server supports the requested x, y and zoom.
   * Complete this stub according to the tile range you support.
   * If you support a limited range of tiles at different zoom levels, then you
   * need to define the supported x, y range at each zoom level.
   */
  private boolean checkTileExists(int x, int y, int zoom) {
    int minZoom = 12;
    int maxZoom = 16;

    if ((zoom < minZoom || zoom > maxZoom)) {
      return false;
    }

    return true;
  }
};

TileOverlay tileOverlay = mMap.addTileOverlay(new TileOverlayOptions()
    .tileProvider(tileProvider));

如有任何帮助,我们将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-05 19:28:07

最后,我自己完成了基于这一回应,我只需要实例化UrlTileProvider抽象类如下:

代码语言:javascript
复制
val tileProvider: TileProvider = object: UrlTileProvider(256, 256){ 
...
}

最终结果:

代码语言:javascript
复制
val tileProvider: TileProvider = object: UrlTileProvider(256, 256) {
  override fun getTileUrl(x: Int, y: Int, zoom: Int): URL? {

    /* Define the URL pattern for the tile images */
    val s: String = String.format("http://my.image.server/images/%d/%d/%d.png",
      zoom, x, y)

    if (!checkTileExists(x, y, zoom)) {
      return null;
    }

    try {
      return URL(s)
    } catch (e: MalformedURLException) {
      throw AssertionError(e)
    }
  }

  /*
 * Check that the tile server supports the requested x, y and zoom.
 * Complete this stub according to the tile range you support.
 * If you support a limited range of tiles at different zoom levels, then you
 * need to define the supported x, y range at each zoom level.
 */
  private fun checkTileExists(x: Int, y: Int, zoom: Int): Boolean {
    val minZoom: Int = 12
    val maxZoom: Int = 16

    if ((zoom < minZoom || zoom > maxZoom)) {
      return false
    }
    return true
  }
}

val tileOverlay: TileOverlay = mMap.addTileOverlay(TileOverlayOptions()
.tileProvider(tileProvider))

希望它能帮到别人。

票数 1
EN

Stack Overflow用户

发布于 2019-03-05 21:08:54

加油加油!!

代码语言:javascript
复制
private var mMap:GoogleMap?=null

    val tileProvider = object: UrlTileProvider(256, 256) {
      override fun getTileUrl(x:Int,y:Int,zoom:Int):URL {

        /* Define the URL pattern for the tile images */
        val s = String.format("http://my.image.server/images/%d/%d/%d.png",
            zoom, x, y)

        if (!checkTileExists(x, y, zoom)) {
          return null
        }

        try {
          return URL(s)
        } catch (e:MalformedURLException) {
            throw AssertionError(e)
        }
      }

      /*
       * Check that the tile server supports the requested x, y and zoom.
       * Complete this stub according to the tile range you support.
       * If you support a limited range of tiles at different zoom levels, then you
       * need to define the supported x, y range at each zoom level.
       */
      private fun checkTileExists(x:Int,y:Int,zoom:Int):Boolean {
        val minZoom = 12
        val maxZoom = 16

        if ((zoom < minZoom || zoom > maxZoom)) {
          return false
        }

        return true
      }
    }

    val tileOverlay = mMap!!.addTileOverlay(TileOverlayOptions()
        .tileProvider(tileProvider))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55008667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档