Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Google Maps using a UIWebView with zooming

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 breaks the apple's Human Interface Guidelines(HIG) Rules, tell me if it is.

like image 463
Rahul Vyas Avatar asked Dec 22 '22 08:12

Rahul Vyas


2 Answers

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
like image 129
nevan king Avatar answered Dec 26 '22 11:12

nevan king


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>
like image 42
Joris Mans Avatar answered Dec 26 '22 10:12

Joris Mans