簡體   English   中英

如何在UIWebView中顯示本地html文件?

[英]How do I display a local html file in a UIWebView?

我有一個相對簡單的問題,我似乎找不到答案。 在進行Google Maps Java API教程時,我遇到了一個問題。 我可以從Web加載HTML文件,但是當我在本地試用它時,它只顯示文件的內容而不是運行腳本。

這是有效的:

NSString *url = @"http://code.google.com/apis/maps/documentation/v3/examples/geocoding-simple.html";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[webView loadRequest:request];

我想在本地存儲HTML文件並從設備本身運行它,所以我試過:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"geocoding-simple" ofType:@"html"]isDirectory:NO]]]; 

它只顯示文件的內容。

這是html文件:

<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps JavaScript API v3 Example: Geocoding Simple</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  }

  function codeAddress() {
    var address = document.getElementById("address").value;
    if (geocoder) {
      geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          map.setCenter(results[0].geometry.location);
          var marker = new google.maps.Marker({
              map: map, 
              position: results[0].geometry.location
          });
        } else {
          alert("Geocode was not successful for the following reason: " + status);
        }
      });
    }
  }
</script>
</head>
<body style="margin:0px; padding:0px;" onload="initialize()">
  <div>
    <input id="address" type="textbox" value="Sydney, NSW">
    <input type="button" value="Geocode" onclick="codeAddress()">

  </div>
<div id="map_canvas" style="width:100%; height:90%"></div>
</body>
</html>

我在這做錯了什么?

托馬斯

好。 我清理了所有目標,它現在有效...... SWEEEET!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM