开发者

Show Google Maps using a UIWebView with zooming

开发者 https://www.devze.com 2022-12-08 21:02 出处:网络
Driving directions are not supported in MapKit. so I think I can show driving direction in a webview. I am showing google maps in uiwebview, but it shows the whole site I just want to show only map pa

Driving directions are not supported in MapKit. so I think I can show driving direction in a webview. I am showing google maps in uiwebview, but it shows the whole site I just want to show only map part with some zoom so that it looks like original maps application of iphone. Also I don't know if this break开发者_StackOverflows the apple's Human Interface Guidelines(HIG) Rules, tell me if it is.


Load a string like this as an NSString (maybe strip the newlines). You can change the latitude and longitude, zoom level etc with stringWithFormat

<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 initialize() {
    var latlng = new google.maps.LatLng(35.000, 139.000);
    var myOptions = {
      zoom: 15,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  }
</script>
</head>
<body onload="initialize()">
 <div id="map_canvas" style="width:100%; height:100%">
</body>
</html>

Then set your UIWebViews html to that. It will give you a page with just a map on it, allow you to scroll with your finger or zoom in, pinch zoom, and place a marker.

Use this to load the HTML:

- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL


Here you go. Pass it through a stringWithFormat with twice the origin lat long and once the destination lat long, all of them as float:

[NSString stringWithformat:... , oriLat,oriLon,oriLat,oriLon,destLat,destLon];

and then pass it into a UIWebView

<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">

  var directionsDisplay = new google.maps.DirectionsRenderer();

  var directionsService = new google.maps.DirectionsService();

  function initialize() {
        var latlng = new google.maps.LatLng(%f, %f);
        var myOptions = {
          zoom: 15,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  directionsDisplay.setMap(map);
  calcRoute();
  }

function calcRoute() {
  var start = new google.maps.LatLng(%f, %f);
  var end = new google.maps.LatLng(%f, %f);
  var request = {
    origin:start, 
    destination:end,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
  };
  directionsService.route(request, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(result);
    }
  });
}


</script>
</head>
<body onload="initialize()">
 <div id="map_canvas" style="width:300px; height:300px">
</body>
</html>
0

精彩评论

暂无评论...
验证码 换一张
取 消