|
| 1 | +package plugin.google.maps; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.net.HttpURLConnection; |
| 6 | +import java.net.URL; |
| 7 | + |
| 8 | +import android.graphics.Bitmap; |
| 9 | +import android.graphics.Bitmap.Config; |
| 10 | +import android.graphics.BitmapFactory; |
| 11 | +import android.graphics.Canvas; |
| 12 | +import android.graphics.Matrix; |
| 13 | +import android.graphics.Paint; |
| 14 | + |
| 15 | +import com.google.android.gms.maps.model.Tile; |
| 16 | +import com.google.android.gms.maps.model.TileProvider; |
| 17 | + |
| 18 | +public class PluginTileProvider implements TileProvider { |
| 19 | + private String tileUrlFormat = null; |
| 20 | + private int tileSize = 256; |
| 21 | + private Paint tilePaint = new Paint(Paint.FILTER_BITMAP_FLAG); |
| 22 | + |
| 23 | + public PluginTileProvider(String tileUrlFormat, double opacity, int tileSize) { |
| 24 | + this.tileUrlFormat = tileUrlFormat; |
| 25 | + this.tileSize = tileSize; |
| 26 | + this.tilePaint.setAlpha((int) (opacity * 255)); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public Tile getTile(int x, int y, int zoom) { |
| 31 | + |
| 32 | + String urlStr = tileUrlFormat.replaceAll("<x>", x + "") |
| 33 | + .replaceAll("<y>", y + "") |
| 34 | + .replaceAll("<zoom>", zoom + ""); |
| 35 | + |
| 36 | + try { |
| 37 | + InputStream inputStream = null; |
| 38 | + if (urlStr.startsWith("http://") || urlStr.startsWith("https://")) { |
| 39 | + URL url = new URL(urlStr); |
| 40 | + HttpURLConnection http = (HttpURLConnection)url.openConnection(); |
| 41 | + http.setRequestMethod("GET"); |
| 42 | + http.addRequestProperty("Accept-Language", "en-US,en;q=0.8"); |
| 43 | + http.addRequestProperty("User-Agent", "Mozilla"); |
| 44 | + http.setInstanceFollowRedirects(true); |
| 45 | + HttpURLConnection.setFollowRedirects(true); |
| 46 | + |
| 47 | + boolean redirect = false; |
| 48 | + // normally, 3xx is redirect |
| 49 | + int status = http.getResponseCode(); |
| 50 | + if (status != HttpURLConnection.HTTP_OK) { |
| 51 | + if (status == HttpURLConnection.HTTP_MOVED_TEMP |
| 52 | + || status == HttpURLConnection.HTTP_MOVED_PERM |
| 53 | + || status == HttpURLConnection.HTTP_SEE_OTHER) |
| 54 | + redirect = true; |
| 55 | + } |
| 56 | + if (redirect) { |
| 57 | + |
| 58 | + // get redirect url from "location" header field |
| 59 | + String newUrl = http.getHeaderField("Location"); |
| 60 | + |
| 61 | + // get the cookie if need, for login |
| 62 | + String cookies = http.getHeaderField("Set-Cookie"); |
| 63 | + |
| 64 | + // open the new connection again |
| 65 | + http = (HttpURLConnection) new URL(newUrl).openConnection(); |
| 66 | + http.setRequestProperty("Cookie", cookies); |
| 67 | + http.addRequestProperty("Accept-Language", "en-US,en;q=0.8"); |
| 68 | + http.addRequestProperty("User-Agent", "Mozilla"); |
| 69 | + } |
| 70 | + |
| 71 | + inputStream = http.getInputStream(); |
| 72 | + |
| 73 | + Bitmap image = BitmapFactory.decodeStream(inputStream); |
| 74 | + Bitmap tileImage = this.resizeForTile(image); |
| 75 | + |
| 76 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 77 | + tileImage.compress(Bitmap.CompressFormat.PNG, 100, outputStream); |
| 78 | + byte[] byteArray = outputStream.toByteArray(); |
| 79 | + Tile tile = new Tile(tileSize, tileSize, byteArray); |
| 80 | + |
| 81 | + outputStream.close(); |
| 82 | + //tileImage.recycle(); |
| 83 | + image.recycle(); |
| 84 | + inputStream.close(); |
| 85 | + return tile; |
| 86 | + } |
| 87 | + |
| 88 | + } catch (Exception e) { |
| 89 | + e.printStackTrace(); |
| 90 | + } |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + public void setOpacity(double opacity) { |
| 95 | + this.tilePaint.setAlpha((int) (opacity * 255)); |
| 96 | + } |
| 97 | + |
| 98 | + private Bitmap resizeForTile(Bitmap bitmap) { |
| 99 | + |
| 100 | + if (bitmap == null) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + /** |
| 104 | + * http://stackoverflow.com/questions/4821488/bad-image-quality-after-resizing-scaling-bitmap#7468636 |
| 105 | + */ |
| 106 | + Bitmap scaledBitmap = Bitmap.createBitmap(tileSize, tileSize, Config.ARGB_8888); |
| 107 | + |
| 108 | + float ratioX = tileSize / (float) bitmap.getWidth(); |
| 109 | + float ratioY = tileSize / (float) bitmap.getHeight(); |
| 110 | + float middleX = tileSize / 2.0f; |
| 111 | + float middleY = tileSize / 2.0f; |
| 112 | + |
| 113 | + Matrix scaleMatrix = new Matrix(); |
| 114 | + scaleMatrix.setScale(ratioX, ratioY, middleX, middleY); |
| 115 | + |
| 116 | + Canvas canvas = new Canvas(scaledBitmap); |
| 117 | + canvas.setMatrix(scaleMatrix); |
| 118 | + canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2, tilePaint); |
| 119 | + bitmap.recycle(); |
| 120 | + |
| 121 | + return scaledBitmap; |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments