Jun 27, 2014

google maps javascript api v3 using j-query



This post similar my post GEOLOCATION for Google Maps JavaScript V3

But this map using j-query.

First, prepare.

You will download library.

And unzip and copy  ui folder to you project.


Second, coding.

This is javascript for library.

  1. <!--jquery-ui-map-->
  2. <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
  3. <script type="text/javascript" src="ui/min/jquery.ui.map.full.min.js"></script>
  4. <script type="text/javascript" src="ui/jquery.ui.map.extensions.js"></script>

This is body

  1. <body>
  2.     <div data-role="page" id="page" data-fullscreen="true" class="map_style">
  3.     <div data-role="header" data-position="fixed">
  4.         <h1>map page</h1>
  5.     </div>
  6.     <div data-role="content" class="map_style">
  7.         <div id="map_canvas" class="map_style" >
  8.             </div> 
  9.     </div>
  10.     <div data-role="footer" data-position="fixed">
  11.         <div data-role="navbar">
  12.            <ul>
  13.            <li><a href="#" data-icon="search" id="currentpos">current location</a></li>
  14.            </ul>
  15.         </div>
  16.     </div>
  17.     </div>
  18. </body>

Add map_style class to page(line 2), content(line 6), map(line 7)

'Line 7' is map and 'line 13' is search button.

This is very important code.

This code Implement google map.

  1. <script type="text/javascript">
  2.     $(document).ready(function(e) {
  3.         var startpos=new google.maps.LatLng(37.309,126.875);
  4.         $("#map_canvas").gmap({
  5.             "center":startpos,
  6.         "zoom":16
  7.         });
  8.         $("#currentpos").click(function() {
  9.             $("#map_canvas").gmap("getCurrentPosition",function(position,status){
  10.             if(status=="OK"){
  11.                 var latlng=new google.maps.LatLng(
  12.                 position.coords.latitude,position.coords.longitude);
  13.                 var marker="http://www.google.com/intl/en_us/mapfiles/ms/icons/blue-dot.png";
  14.                 $("#map_canvas").gmap("get","map").panTo(latlng);
  15.                     $("#map_canvas").gmap("addMarker",{
  16.                     "position":latlng,
  17.                     "icon":marker
  18.                     }).click(function(){
  19.                         $("#map_canvas").gmap("openInfoWindow",{
  20.                         "content":"<b>current location</b> <img src='http://images.forbes.com/media/lists/companies/google_200x200.jpg' alt=''/>"},this);
  21.                         });
  22.             }else{
  23.                 alert("can't found location");
  24.             }
  25.         });
  26.     });
  27.     });//ready   
  28.    
  29. </script>
Look so complex but simple.

'Line 3' is init position for display map.

'Line 4~7' is display(position and zoom level)

'Line 8~26' is searching code.

'Line 8' is button click listener.

'Line 9' is check current position using sensor(gps or network)

I recommend using mobile.

'Line 10~21' is success searching your position and 'line 22~24' is can't searching.

'Line 11~12' is getting current position.

'Line 13' is marker option.

'Line 14~17' display current position and marker.

'Line 18' is click listener for marker.

'Line 19~21' is info window.

I ran my mobile device.

This is result.

This is init status.

I click to search button.

I click to marker.


No comments:

Post a Comment