javascript - 无法在本地 html 文件中的 webview 中加载谷歌地图

标签 javascript html ios objective-c google-maps-api-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 中显示一个空白页面。

最佳答案

试试这段代码

<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

关于javascript - 无法在本地 html 文件中的 webview 中加载谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42716654/

相关文章:

javascript - 使用 redux 表单超出更新深度

javascript - 悬停时链接 alt 出现在单独的 div 中

php - 带有 formpree 的复选框

javascript - 使用 jQuery 从 json 动态呈现无序列表

objective-c - 使用标量参数调用选择器

javascript - CSS 在 translateXY 中用鼠标悬停

javascript - 如何将 POST 表单数据转换为 JSON 对象

javascript - 异步/等待 : can't access array value of function directly

ios - 如何从 UIImage 获取文件路径?

ios - 禁用 iPhone 而非 iPad 的旋转