在安卓上使用Gluon Maps
时,实际地图不会加载。相反,显示了空白的空白区域,我可以在日志中看到这一点:
05-31 14:20:34.041 E/CachedOsmTileRetriever(18834): null
05-31 14:20:34.041 E/CachedOsmTileRetriever(18834): java.io.FileNotFoundException: http://tile.openstreetmap.org/16/33497/22228.png
05-31 14:20:34.041 E/CachedOsmTileRetriever(18834): at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251)
05-31 14:20:34.041 E/CachedOsmTileRetriever(18834): at com.gluonhq.impl.maps.tile.osm.CachedOsmTileRetriever$CacheThread.doCache(CachedOsmTileRetriever.java:189)
05-31 14:20:34.041 E/CachedOsmTileRetriever(18834): at com.gluonhq.impl.maps.tile.osm.CachedOsmTileRetriever$CacheThread.run(CachedOsmTileRetriever.java:157)
代码本身是微不足道的:
private void showMap(Double lat, Double lon) {
mapView = new MapView();
PoiLayer poiLayer = new PoiLayer();
MapPoint mapPoint = new MapPoint(lat, lon);
poiLayer.addPoint(mapPoint, new Circle(7, Color.RED));
mapView.setZoom(16);
mapView.addLayer(poiLayer);
mapView.flyTo(0.1, mapPoint, 0.1);
tabMap.setContent(mapView);
}
相同的代码在iOS上工作得很好:地图按预期加载。
用
compile 'com.gluonhq:maps:1.0.2'
在build.gradle中(与1.0.3
相同)
请注意,如果我在浏览器中输入URL(来自上述异常),我会被重定向到https:
http://tile.openstreetmap.org/16/33497/22228.png 自 https://tile.openstreetmap.org/16/33497/22228.png
任何想法为什么在安卓上例外?
看起来OpenStreetMaps服务器最近发生了一些变化。胶子映射在桌面上也失败了。
我已经用http
进行了测试,并收到报告的403错误:
java.io.IOException: Server returned HTTP response code: 403 for URL: http://tile.openstreetmap.org/5/19/10.png
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1913)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1509)
at com.gluonhq.maps/com.gluonhq.impl.maps.tile.osm.CachedOsmTileRetriever$CacheThread.doCache(CachedOsmTileRetriever.java:190)
at com.gluonhq.maps/com.gluonhq.impl.maps.tile.osm.CachedOsmTileRetriever$CacheThread.run(CachedOsmTileRetriever.java:157)
还有https
,现在我得到一个不同的错误:
java.io.IOException: Server returned HTTP response code: 429 for URL: https://tile.openstreetmap.org/6/37/22.png
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1913)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1509)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:245)
at com.gluonhq.maps/com.gluonhq.impl.maps.tile.osm.CachedOsmTileRetriever$CacheThread.doCache(CachedOsmTileRetriever.java:190)
at com.gluonhq.maps/com.gluonhq.impl.maps.tile.osm.CachedOsmTileRetriever$CacheThread.run(CachedOsmTileRetriever.java:157)
在这两种情况下(http 或 https),URL 在浏览器上运行良好,这可能表明需要向 URLConnection 请求添加"User-agent"
。
最初,这似乎解决了这个问题,因为不再有http错误。然而,瓷砖是空白的。
最后,至少在桌面上完全适合我的解决方案是这个:
static {
System.setProperty("http.agent", "Gluon Mobile/1.0.3");
}
由于这是一个系统属性,因此可以将其添加到使用 Gluon Map 的项目中,因此可以在完成新版本以解决此问题之前对其进行测试。
人造人
在Android上,看起来您需要设置一个有效的代理。
同样,这远非解决方案,但这是一个快速解决方案。
添加到您的项目,并部署到您的 Android 设备:
static {
String userAgent = System.getProperty("http.agent");
System.out.println("HTTP.AGENT: " + userAgent);
System.setProperty("http.agent", userAgent);
}
插入设备后,打开终端,转到 Android SDK 文件夹,然后键入:
cd platform-tools
adb logcat -v threadtime
现在启动应用程序并查看控制台以获取输出。
就我而言,我看到:
06-10 09:57:40.784 32630 32656 I System.out: HTTP.AGENT: Dalvik/2.1.0 (Linux; U; Android 9; Pixel XL Build/PQ3A.190505.001)
这意味着Android提供了一个有效的代理,但不知何故必须明确设置它。
现在,磁贴已下载。
但是,一旦这起作用,我就可以从我的应用程序中删除此静态块,然后重新安装。并且瓷砖也被下载。看起来只需要做一次。也许OSM服务器将该代理/设备/应用程序包/IP包含在白名单中,但这只是一个纯粹的猜测。
苹果
如前所述,在iOS上,即使user.agent
返回null,它也可以正常工作。
编辑
最后,一个适当的解决方案可以是这样的:
static {
String httpAgent = System.getProperty("http.agent");
if (httpAgent == null) {
httpAgent = "(" + System.getProperty("os.name") + " / " + System.getProperty("os.version") + " / " + System.getProperty("os.arch") + ")";
}
System.setProperty("http.agent", "Gluon Mobile/1.0.3 " + httpAgent);
}