无法在本地HTML文件中的WebView中加载Google地图

3
我正在使用名为basicmap.html的本地HTML文件,其中包含以下内容:
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <div id="map"></div>
        <script>
            function initMap() {
                // Create a map object and specify the DOM element for display.
                var map = new google.maps.Map(document.getElementById('map'), {
                                              center: {lat: -34.397, lng: 150.644},
                                              scrollwheel: false,
                                              zoom: 8
                                              });
                                              console.log(map);
            }

            </script>
        <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCvjeJZDdkpxCLasVMvTX2raxKkVGUULP8&callback=initMap"
            async defer></script>
    </body>
</html>

在viewcontroller.m中,将本地的basicmap.html文件加载到具有以下代码的webview中:

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURL *url = [[NSBundle mainBundle] URLForResource:@"basicmap" withExtension:@"html"];
    [_webView loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:_webView];

}
@end

在webview中显示的输出为空白页面。


我还在info.plist文件中添加了以下内容:<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> - Sudipta Chatterjee
请使用以下方法 - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL - Jigar
@Jigar 我使用以下代码: NSString htmlFile = [[NSBundle mainBundle] pathForResource:@"basic" ofType:@"html" inDirectory:nil]; NSString htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil]; [_webView loadHTMLString:htmlString baseURL: [[NSBundle mainBundle] bundleURL]];但是它仍然不起作用。 - Sudipta Chatterjee
你检查一下你的HTML文件是否正常工作了吗? - Jigar
Html页面在Safari/Chrome中也显示为空白。 - Sudipta Chatterjee
2个回答

0
在 Info.plist 中添加键和值
将您的链接 URL 添加到其中,而不是 google.com。
<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>New item</key>
            <string>http://www.google.com</string>
        </dict>
    </dict>

然后添加以下代码来加载链接

NSURLRequest* urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
[self.loadwebs loadRequest:urlRequest];

这里loadwebs是webview的属性名称


0

试试这段代码

<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
  function init() {
    var latlong = new google.maps.LatLng(23.0225, 72.5714);
    var myOptions = {
      zoom: 15,
      center: latlong,
      mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  }
</script>
</head>
<body onload="init()">
 <div id="map_canvas" style="width:100%; height:100%">
</body>
</html>

并使用这个方法

- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL

@Sudipta Chatterjee,您能帮我检查一下我的答案吗? - Jigar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接