我正在构建一个使用自定义地图图块的Android应用。我开始使用一套工作正常,但只有有限的缩放级别和面积。然后我切换到覆盖更多区域的另一个磁贴集,但只是拒绝在Android中显示。我可以在Javascript地图中看到它们,我的LogCat中没有错误,所以我不确定发生了什么。
Android Map叠加层是否只支持某些图像格式?
供参考,以下是两个图块集:
这个适用于网络和Android: http://mooproductions.org/vfrs/local.html
这个可以在网络上运行,但根本无法在Android上呈现: http://mooproductions.org/vfrs/national.html
这是我用来提供磁贴集的代码:
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;
}
}
你可以看到注释掉我使用的瓷砖集的其他网址就可以了。 tileUrl的创建有点不同,因为这个新的tile集将x和y转换为url。
以下是我用来显示自定义图块的代码:
@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));
}
}
答案 0 :(得分:1)
这是因为"User-Agent"
中URL url
对象的VfrTileProvider.getTileUrl()
属性设置错误。您可以按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);
}
或只是将System.setProperty("http.agent", "");
来电添加到public void onMapReady ()
:
@Override
public void onMapReady (GoogleMap googleMap)
{
System.setProperty("http.agent", "");
_map = googleMap;
...
}
我更喜欢第二个(System.setProperty("http.agent", "");
)案例。