作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用一个名为 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/
我是一名优秀的程序员,十分优秀!