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

时间:2010-03-15 21:28:18

标签: html iphone xcode uiwebview google-maps

我有一个相对简单的问题,我似乎找不到答案。在进行Google Maps Java API教程时,我遇到了一个问题。我可以从网上加载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>

我在这里做错了什么?

托马斯

1 个答案:

答案 0 :(得分:0)

行。我清理了所有目标,它现在有效...... SWEEEET!