|
| 1 | +package com.airbnb.android.react.maps; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | + |
| 5 | +import com.google.android.gms.maps.GoogleMap; |
| 6 | +import com.google.android.gms.maps.model.TileOverlay; |
| 7 | +import com.google.android.gms.maps.model.TileOverlayOptions; |
| 8 | +import com.google.android.gms.maps.model.UrlTileProvider; |
| 9 | + |
| 10 | +import java.net.MalformedURLException; |
| 11 | +import java.net.URL; |
| 12 | + |
| 13 | +public class AirMapUrlTile extends AirMapFeature { |
| 14 | + |
| 15 | + class AIRMapUrlTileProvider extends UrlTileProvider |
| 16 | + { |
| 17 | + private String urlTemplate; |
| 18 | + public AIRMapUrlTileProvider(int width, int height, String urlTemplate) { |
| 19 | + super(width, height); |
| 20 | + this.urlTemplate = urlTemplate; |
| 21 | + } |
| 22 | + @Override |
| 23 | + public synchronized URL getTileUrl(int x, int y, int zoom) { |
| 24 | + |
| 25 | + String s = this.urlTemplate |
| 26 | + .replace("{x}", Integer.toString(x)) |
| 27 | + .replace("{y}", Integer.toString(y)) |
| 28 | + .replace("{z}", Integer.toString(zoom)); |
| 29 | + URL url = null; |
| 30 | + try { |
| 31 | + url = new URL(s); |
| 32 | + } catch (MalformedURLException e) { |
| 33 | + throw new AssertionError(e); |
| 34 | + } |
| 35 | + return url; |
| 36 | + } |
| 37 | + |
| 38 | + public void setUrlTemplate(String urlTemplate) { |
| 39 | + this.urlTemplate = urlTemplate; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private TileOverlayOptions tileOverlayOptions; |
| 44 | + private TileOverlay tileOverlay; |
| 45 | + private AIRMapUrlTileProvider tileProvider; |
| 46 | + |
| 47 | + private String urlTemplate; |
| 48 | + private float zIndex; |
| 49 | + |
| 50 | + public AirMapUrlTile(Context context) { |
| 51 | + super(context); |
| 52 | + } |
| 53 | + |
| 54 | + public void setUrlTemplate(String urlTemplate) { |
| 55 | + this.urlTemplate = urlTemplate; |
| 56 | + if (tileProvider != null) { |
| 57 | + tileProvider.setUrlTemplate(urlTemplate); |
| 58 | + } |
| 59 | + if (tileOverlay != null) { |
| 60 | + tileOverlay.clearTileCache(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public void setZIndex(float zIndex) { |
| 65 | + this.zIndex = zIndex; |
| 66 | + if (tileOverlay != null) { |
| 67 | + tileOverlay.setZIndex(zIndex); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public TileOverlayOptions getTileOverlayOptions() { |
| 72 | + if (tileOverlayOptions == null) { |
| 73 | + tileOverlayOptions = createTileOverlayOptions(); |
| 74 | + } |
| 75 | + return tileOverlayOptions; |
| 76 | + } |
| 77 | + |
| 78 | + private TileOverlayOptions createTileOverlayOptions() { |
| 79 | + TileOverlayOptions options = new TileOverlayOptions(); |
| 80 | + options.zIndex(zIndex); |
| 81 | + this.tileProvider = new AIRMapUrlTileProvider(256, 256, this.urlTemplate); |
| 82 | + options.tileProvider(this.tileProvider); |
| 83 | + return options; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Object getFeature() { |
| 88 | + return tileOverlay; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void addToMap(GoogleMap map) { |
| 93 | + this.tileOverlay = map.addTileOverlay(getTileOverlayOptions()); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void removeFromMap(GoogleMap map) { |
| 98 | + tileOverlay.remove(); |
| 99 | + } |
| 100 | +} |
0 commit comments