Слои листов карт Google в Котлине - PullRequest
0 голосов
/ 05 марта 2019

Я пытаюсь реализовать Tile Layers с помощью Google Maps SDK Android, но официальная документация содержит только примеры кода на Java, и мой проект находится в Kotlin.Я не могу найти примеры того, как сделать то же самое в kotlin.Кто-нибудь знает, как это сделать?

Документация java пример кода :

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

Любая помощь с этим будет оценена

Ответы [ 2 ]

0 голосов
/ 06 марта 2019

Вот и ура!

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 голосов
/ 05 марта 2019

В итоге я сам все сделал , основываясь на этом ответе , мне нужно просто создать экземпляр абстрактного класса 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))

Надеюсь, это поможет кому-то еще.

...