I would like to access some Custom Map Tiles when creating a TileOverlay for Google Maps API.
So this is my current code:
TileProvider tileProvider = new UrlTileProvider(256, 256) {
@Override
public URL getTileUrl(int x, int y, int z) {
String url = String.format("https://api.mycustommaps.com/v1/%d/%d/%d.jpg", z, x, y);
if (!checkTileExists(x, y, z)) {
return null;
}
try {
URL tileUrl = new URL(url);
tileUrl.openConnection().addRequestProperty("Authorization", LOGIN_TOKEN);
return tileUrl;
} catch (MalformedURLException e) {
e.printStackTrance();
} catch (IOException e) {
e.printStackTrance();
}
return null;
}
};
Since the connection returns 401 Anauthorized, I can't access the tiles. How could I pass Authorization header to let the url know I am authorized to access those tiles?
you have to implement the "TileProvider" interface, not URLTileProvider (because you have to retrieve the tile on your own, an URL is not enough. https://developers.google.com/android/reference/com/google/android/gms/maps/model/TileProvider as you can see, there is a note to keep attention:
and you have to implement a single method:
It is now your work download the tile, I've done it for local files, so I'm just writing here some code that might need some more refinement and testing:
to download: