Android Open Source - surveygcp Local Map Tile Provider






From Project

Back to project page surveygcp.

License

The source code is released under:

GNU General Public License

If you think the Android project surveygcp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.droidplanner.gcp.helpers;
/* w w w.ja  v  a 2  s .  c  o m*/
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Locale;

import org.droidplanner.gcp.file.DirectoryPath;

import com.google.android.gms.maps.model.Tile;
import com.google.android.gms.maps.model.TileProvider;

/**
 * Title provider for a MapView from the local storage. Based on:
 * http://stackoverflow.com/questions/14784841/tileprovider-using-local-tiles
 * 
 */
public class LocalMapTileProvider implements TileProvider {
  private static final int TILE_WIDTH = 256;
  private static final int TILE_HEIGHT = 256;
  private static final int BUFFER_SIZE = 16 * 1024;

  @Override
  public Tile getTile(int x, int y, int zoom) {
    byte[] image = readTileImage(x, y, zoom);
    if (image == null) {
      return NO_TILE;
    } else {
      return new Tile(TILE_WIDTH, TILE_HEIGHT, image);
    }

  }

  private byte[] readTileImage(int x, int y, int zoom) {
    FileInputStream in = null;
    ByteArrayOutputStream buffer = null;

    try {
      String patch = DirectoryPath.getMapsPath()
          + getTileFilename(x, y, zoom);
      in = new FileInputStream(patch);
      buffer = new ByteArrayOutputStream();

      int nRead;
      byte[] data = new byte[BUFFER_SIZE];

      while ((nRead = in.read(data, 0, BUFFER_SIZE)) != -1) {
        buffer.write(data, 0, nRead);
      }
      buffer.flush();

      return buffer.toByteArray();
    } catch (FileNotFoundException e) {
      return null;
    } catch (IOException e) {
      return null;
    } catch (OutOfMemoryError e) {
      e.printStackTrace();
      return null;
    } finally {
      if (in != null)
        try {
          in.close();
        } catch (Exception ignored) {
        }
      if (buffer != null)
        try {
          buffer.close();
        } catch (Exception ignored) {
        }
    }
  }

  private String getTileFilename(int x, int y, int zoom) {
    return String.format(Locale.US, "%d/%d/%d.jpg", zoom, y, x);
  }

}




Java Source Code List

android.UnusedStub.java
org.droidplanner.gcp.GCPApp.java
org.droidplanner.gcp.activitys.GCPActivity.java
org.droidplanner.gcp.activitys.SettingsActivity.java
org.droidplanner.gcp.dialogs.openfile.OpenFileDialog.java
org.droidplanner.gcp.dialogs.openfile.OpenGcpFileDialog.java
org.droidplanner.gcp.file.DirectoryPath.java
org.droidplanner.gcp.file.FileList.java
org.droidplanner.gcp.file.IO.GcpReader.java
org.droidplanner.gcp.fragments.GcpMapFragment.java
org.droidplanner.gcp.fragments.helpers.OfflineMapFragment.java
org.droidplanner.gcp.fragments.markers.GcpMarker.java
org.droidplanner.gcp.fragments.markers.MarkerManager.java
org.droidplanner.gcp.gcp.Gcp.java
org.droidplanner.gcp.helpers.LocalMapTileProvider.java
org.droidplanner.gcp.helpers.TTS.java