Using Google Maps JavaScript API v3 with jQuery Mobile
by Dragan Gaić Jul 18, 2013
First let us talk about how Gmap v3 API can be used inside a classic web application, it will give us a good perspective when we switch back to jQuery Mobile. In the beginning of a map craze iframe was a standard delivery solution but as a time went by it become an outdated technology, more like an obstacle them a successful delivery technology.
Classic solution
<!DOCTYPE html> <html> <head> <title>Google Maps JavaScript API v3 Example: Map Simple</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/> <meta charset="utf-8"/> <style> html, body, #map_canvas { margin: 0; padding: 0; height: 100%; } </style> <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> <script> var map; function initialize() { var mapOptions = { zoom: 8, center: new google.maps.LatLng(-34.397, 150.644), mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions); } google.maps.event.addDomListener(window, 'load', initialize); </script> </head> <body> <div id="map_canvas"></div> </body> </html>
jQuery Mobile solution
#content { padding: 0; position : absolute !important; top : 40px !important; right : 0; bottom : 40px !important; left : 0 !important; }
<!DOCTYPE html> <html> <head> <title>jQM Complex Demo</title> <meta name="viewport" content="initial-scale=1, maximum-scale=1"/> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /> <style> #content { padding: 0; position : absolute !important; top : 40px !important; right : 0; bottom : 40px !important; left : 0 !important; } </style> <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script> <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script> <script> $(document).on('pageinit', '#index',function(e,data){ var minZoomLevel = 12; var map = new google.maps.Map(document.getElementById('map_canvas'), { zoom: minZoomLevel, center: new google.maps.LatLng(38.50, -90.50), mapTypeId: google.maps.MapTypeId.ROADMAP }); }); </script> </head> <body> <div data-role="page" id="index"> <div data-theme="a" data-role="header"> <h3> First Page </h3> </div> <div data-role="content" id="content"> <div id="map_canvas" style="height:100%"></div> </div> <div data-theme="a" data-role="footer" data-position="fixed"> <h3> First Page </h3> </div> </div> </body> </html>
Second jQuery Mobile solution
function getRealContentHeight() { var header = $.mobile.activePage.find("div[data-role='header']:visible"); var footer = $.mobile.activePage.find("div[data-role='footer']:visible"); var content = $.mobile.activePage.find("div[data-role='content']:visible:visible"); var viewport_height = $(window).height(); var content_height = viewport_height - header.outerHeight() - footer.outerHeight(); if((content.outerHeight() - header.outerHeight() - footer.outerHeight()) <= viewport_height) { content_height -= (content.outerHeight() - content.height()); } return content_height; }
0 comments:
Post a Comment
Silahkan isikan comment box untuk komentar Anda..