Custom Map Tile Work in Chrome but not Android

Viewed 169

I'm building an Android app that uses custom map tiles. I started out with a set that worked fine but only had limited zoom levels and area. Then I switched to another tile set that covered much more area but simply refuses to display in Android. I can see them in the Javascript Maps and there are no errors in my LogCat so I'm not sure what's going on.

Are there only certain image formats that the Android Map overlays support?

For reference, here are the two tile sets:

This one works both on the web and in Android: http://mooproductions.org/vfrs/local.html

This one works on the web but simply doesn't render on Android: http://mooproductions.org/vfrs/national.html

Here's he code I'm using to provide the tile sets:

public class VfrTileProvider extends UrlTileProvider
{
//  private static final String BASE_URL = "http://www.mooproductions.org/vfrs/%d/%d/%d.png";
//  private static final String BASE_URL = "http://mw1.google.com/mw-planetary/lunar/lunarmaps_v1/clem_bw/%d/%d/%d.jpg";
    private static final String BASE_URL = "http://vfrmap.com/20170914/tiles/vfrc/%d/%d/%d.jpg";

    public VfrTileProvider (int width, int height)
    {
        super(width, height);
    }

    @Override
    public URL getTileUrl (int x, int y, int zoom)
    {
        int reversedY = (1 << zoom) - y - 1;
//      String tileUrl = String.format(Locale.US, BASE_URL, zoom, x, reversedY);
        String tileUrl = String.format(Locale.US, BASE_URL, zoom, reversedY, x);
        Log.d("Tile Provider", tileUrl);
        URL url = null;

        try { url = new URL(tileUrl); }
        catch (MalformedURLException e) { e.printStackTrace(); }

        return url;
    }
}

You can see commented out are the other URLs for tile sets that I use that work just fine. The creation of the tileUrl is a little different because this new tile set transposes the x and y in their url.

Here is the code I use to display the custom tiles:

@Override
public void onMapReady (GoogleMap googleMap)
{
    _map = googleMap;

    _map.setMapType(GoogleMap.MAP_TYPE_NONE);
    VfrTileProvider tileProvider = new VfrTileProvider(256, 256);
    _map.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider));

    Criteria criteria = new Criteria();
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
        ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION }, REQUEST_LOCATION_PERMISSION);
        return;
    }

    Location location = _locationManager.getLastKnownLocation(_locationManager.getBestProvider(criteria, false));
    if (location != null)
    {
        _map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 13));

        CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(new LatLng(location.getLatitude(), location.getLongitude()))
            .zoom(10)
            .build();
        _map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }
}
1 Answers

This is because wrong "User-Agent" property settings of URL url object in VfrTileProvider.getTileUrl(). You can set it to correct by setRequestProperty():

URL url = null;
try {
    url = new URL(tileUrl);
    try {
        url.openConnection().setRequestProperty("User-Agent","<correct user agent name>");
    } catch (IOException e) {
        e.printStackTrace();
} catch (MalformedURLException e) {
    throw new AssertionError(e);
}

or just add System.setProperty("http.agent", ""); call to your public void onMapReady ():

@Override
public void onMapReady (GoogleMap googleMap)
{
    System.setProperty("http.agent", "");    
    _map = googleMap;
    ...
}

I prefer second (System.setProperty("http.agent", "");) case.

Related