【问题标题】:Google Maps Tile Layers in KotlinKotlin 中的谷歌地图平铺层
【发布时间】:2019-07-27 06:23:30
【问题描述】:

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

The documentation java example code:

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));

对此的任何帮助将不胜感激

【问题讨论】:

    标签: android google-maps kotlin google-maps-android-api-2


    【解决方案1】:

    给你干杯!

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

    【讨论】:

      【解决方案2】:

      最后我自己完成了based in this response,我只需要像这样实例化UrlTileProvider抽象类:

      val tileProvider: TileProvider = object: UrlTileProvider(256, 256){ 
      ...
      }
      

      最终结果:

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

      希望它能帮助别人。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-03-20
        • 2012-10-26
        • 2019-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多