【问题标题】:Not able load google map inside webview in local html file无法在本地 html 文件中的 webview 中加载谷歌地图
【发布时间】:2017-07-31 16:42:23
【问题描述】:

我正在使用一个名为 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 文件中添加了 NSAppTransportSecurityNSAllowsArbitraryLoads
  • 使用这个方法 - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
  • @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]];但这也行不通/
  • 您检查您的 html 文件是否正常工作?
  • @Jigar Html 页面在 Safari/Chrome 中也显示为空白

标签: javascript html ios objective-c google-maps-api-3


【解决方案1】:

在 Info.plist 中添加键和值

而不是 google.com 添加您的链接 url

<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 属性名

【讨论】:

    【解决方案2】:

    试试这个代码

    <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 你检查我的答案了吗?
    猜你喜欢
    • 1970-01-01
    • 2019-09-22
    • 2012-06-18
    • 1970-01-01
    • 2014-01-19
    • 2014-02-28
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    相关资源
    最近更新 更多