Gluon Maps 不加载地图并抛出异常

Gluon Maps doesn't load the map and throws an exception

在 android 上使用 Gluon Maps 时,实际地图不会加载。而是显示空的白色 space,我可以在日志中看到:

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'

in build.gradle(与 1.0.3 相同)

请注意,如果我在浏览器中输入 URL(来自上述异常),我将被重定向到 https:

http://tile.openstreetmap.org/16/33497/22228.pnghttps://tile.openstreetmap.org/16/33497/22228.png

关于 android 异常的任何想法?

OpenStreetMaps 服务器最近似乎发生了一些变化。 Gluon Maps 在桌面上也失败了。

我已经用 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 在浏览器上工作正常,这可能表明需要将 "User-agent" 添加到 URLConnection request.

最初这似乎解决了问题,因为不再有 http 错误。然而,瓷砖是空白的。

最后,对我来说完全有效的解决方案,至少在桌面上,是这个:

static {
    System.setProperty("http.agent", "Gluon Mobile/1.0.3");
}

由于这是一个系统属性,所以可以添加到使用Gluon Maps的项目中,所以可以在新版本完成之前进行测试来解决这个问题issue

Android

在 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 服务器将 agent/device/app bundle/IP 包含在白名单中,但这只是一个纯粹的猜测。

iOS

如前所述,在 iOS 上它工作得很好,即使 user.agent returns 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);
}