Skip to content

Instantly share code, notes, and snippets.

@piruin
Last active June 8, 2016 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piruin/f65e51b13b627b4a34f79b68d0f70172 to your computer and use it in GitHub Desktop.
Save piruin/f65e51b13b627b4a34f79b68d0f70172 to your computer and use it in GitHub Desktop.
Caching version of ThaichoteMapTile
/*
* Copyright (c) 2016 NECTEC
* National Electronics and Computer Technology Center, Thailand
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.tanrabad.survey.presenter.maps;
import android.content.Context;
import android.graphics.Bitmap;
import com.google.android.gms.maps.model.Tile;
import com.google.android.gms.maps.model.TileOverlayOptions;
import com.google.android.gms.maps.model.TileProvider;
import com.google.android.gms.maps.model.UrlTileProvider;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import java.io.ByteArrayOutputStream;
import java.net.MalformedURLException;
import java.net.URL;
public class ThaichoteMapTile implements TileProvider {
private static final String TILE_URL =
"http://giportal.gistda.or.th/arcgis/rest/services/raster/thaichote_update_2014_2016/MapServer/tile/%d/%d/%d";
private final DisplayImageOptions mOptions;
public static final int WIDTH = 256;
public static final int HEIGHT = 256;
public ThaichoteMapTile(Context context) {
if (!ImageLoader.getInstance().isInited()) {
// Create global configuration and initialize ImageLoader with this config
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).build();
ImageLoader.getInstance().init(config);
}
// init ImageLoader display options
DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder();
builder.cacheInMemory(true).cacheOnDisk(true);
mOptions = builder.build();
}
@Override
public Tile getTile(int x, int y, int z) {
byte[] tileImage = getTileImage(x, y, z);
if (tileImage != null) {
return new Tile(WIDTH / 2, HEIGHT / 2, tileImage);
}
return NO_TILE;
}
private byte[] getTileImage(int x, int y, int z) {
Bitmap bitmap = ImageLoader.getInstance().loadImageSync(getTileUrl(x, y, z), mOptions);
if (bitmap == null) {
return null;
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
return stream.toByteArray();
}
public String getTileUrl(int x, int y, int zoom) {
return String.format(TILE_URL, zoom, y, x);
}
public static TileOverlayOptions createTileOverlayOption(Context context) {
return new TileOverlayOptions()
.tileProvider(new ThaichoteMapTile(context))
.fadeIn(true);
}
}
@piruin
Copy link
Author

piruin commented Jun 8, 2016

Requires Android Universal Image Loader:

dependencies {
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment