2017-03-10 128 views
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中的空白頁面。

+0

我還加 NSAppTransportSecurity \t \t \t NSAllowsArbitraryLoads \t \t \t在Info.plist文件 –

+0

使用這種方法 - (空)加載HTMLString:(NSString *)string baseURL:(NSURL *)baseURL – iOS

+0

@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]]; 但這也不行/ –

回答

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 
+0

@Sudipta Chatterjee你檢查我的答案? – iOS

+0

此代碼正常工作。謝謝Jigar –

+0

@蘇迪帕查特吉歡迎 – iOS

0

在Info.plist中添加鍵和值

,而不是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屬性名稱