Xamarin:尝试使用带有 html 和 javascript 的 OpenLayers 地图显示 WebView 时出错 [英] Xamarin: Error trying to show WebView using OpenLayers maps with html and javascript

查看:73
本文介绍了Xamarin:尝试使用带有 html 和 javascript 的 OpenLayers 地图显示 WebView 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 WebView 组件在 xamarin 中显示地图,尝试将此代码传递给一个 html 文件,它工作正常.WebView 来自 Xamarin.Forms

I'm trying to show a map in xamarin using a WebView component, try passing this code to an html file and it works fine. The WebView is from Xamarin.Forms

alert('test');并且html正文中的App这个词显示没有问题,但地图没有显示.

The line alert('test'); and the word App in the body of the html is shown without problems, but the map doesn't show.

用于测试的设备运行的是 android 9

The devise for test was running android 9

    var browser = new WebView();
    browser.HeightRequest = 600;
    browser.WidthRequest = 600;

    var htmlSource = new HtmlWebViewSource();
    htmlSource.Html = @"<html>
                           <head>
                              <script src='http://www.openlayers.org/api/OpenLayers.js'></script>
                            </head>
                            <body>
                                 App
                                  <div id='mapdiv'></div>
                                  <script>
                                       alert('test');  
                                       map = new OpenLayers.Map('mapdiv');
                                       map.addLayer(new OpenLayers.Layer.OSM());

                                       var lonLat = new OpenLayers.LonLat(-0.1279688, 51.5077286)
                                      .transform(
                                       new OpenLayers.Projection('EPSG:4326'), 
                                       map.getProjectionObject() 
                                    );

                                      var zoom = 16;

                                      var markers = new 
                                      OpenLayers.Layer.Markers('Markers');
                                      map.addLayer(markers);

                                      markers.addMarker(new OpenLayers.Marker(lonLat));

                                      map.setCenter(lonLat, zoom);
                                </script>
                      </body></html>";

                    browser.Source = htmlSource;
                    Children.Add(browser);

推荐答案

1st: Change your script link use https (vs. http)

1st: Change your script link use https (vs. http)

2nd:OpenLayers.js 脚本本身使用了很多非安全链接,这是 iOS 和 Google 的新 Android API 级别上的问题,因为默认情况下所有链接都应该是安全的(<代码>https).

2nd: The OpenLayers.js script itself uses a lot of non-secure links and that is a problem on iOS and new Android API levels from Google as by default all links should be secure (https).

如果您查看应用程序输出,您将看到如下日志消息:

If you review the application output you will see log messages such as:

应用传输安全已阻止明文 HTTP (http://) 资源加载,因为它不安全.可以通过应用的 Info.plist 文件配置临时例外.

App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

iOS:

因此,在 iOS 上,您必须在应用程序的 Info.plist 中添加一个例外,例如:

iOS:

So on iOS, you will have to add an exception to your app's Info.plist such as:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

回复:https://stackoverflow.com/a/40299837/4984832

从 Android 9(API 级别 28)开始,默认情况下禁用明文支持.

Starting with Android 9 (API level 28), cleartext support is disabled by default.

在 Android 9+ 上,您将为您的应用添加安全例外 (cleartextTrafficPermitted)

On Android 9+ you will have add a security exception to your app (cleartextTrafficPermitted)

回复:https://stackoverflow.com/a/55997456/4984832

注意:我的基于 Xamarin 的答案已链接,或直接通过该问题的其他答案编辑清单.

Note: My Xamarin-based answer is linked, or edit the manifest directly via the other answers to that question.

顺便说一句:最终的答案是向 OpenLayers 提交错误,以便在其所有脚本中使用安全端点,这样就不必申请应用程序安全例外...

这篇关于Xamarin:尝试使用带有 html 和 javascript 的 OpenLayers 地图显示 WebView 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆