Add Google Maps With Latitude and Logitude and Auto Complete Search

Now I want to share how to add Google Maps to your HTML or Website.. I get this tutorial from here. So this is example to show google maps with latitude and logitude.

</pre>
<html>
<head>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<style type="text/css">
body {
	font:10pt arial;
}

.main { 
	text-align:center; 
	font:12pt Arial; 
	width:80%; 
	height:auto;
}

.eventtext {
	width:100%; 
	margin-top:20px; 
	font:10pt Arial; 
	text-align:left; 
	line-height:25px; 
	background-color:#EDF4F8;
	padding:5px; 
	border:1px dashed #C2DAE7;
}

#mapa {
	width:100%; 
	height:340px; 
	border:5px solid #DEEBF2;
}

ul {
	font:10pt arial; 
	margin-left:0px; 
	padding:5px;
}

li {
	margin-left:0px; 
	padding:5px; 
	list-style-type:decimal;
}

.code {
	border:1px dashed #cecece;
	background-color:#F7F7F7; 
	padding:5px;
}

.small {
	font:9pt arial;
	color:gray;
	padding:2px;
}

.text { 
	margin:0px;
}
</style>
</head>
<body onload="mapa()">
	<div class="main">
		<h2 class="text">Get lattitude and longitude onmouseover and onclick event in google map v3 </h2>
	<div style="width:70%;  margin:0px auto;">
	<div id="mapdiv" style="width:100%; height:250px;"></div>
	<div class="eventtext">
		<div>Lattitude: <span id="latspan"></span></div>
		<div>Longitude: <span id="lngspan"></span></div>
		<div>Lat Lng: <span id="latlong"></span></div>
		<div>Lat Lng on click: <input type="text" id="latlongclicked" style="width:300px; border:1px inset gray;"></span></div>
	</div>
	</div>

</div>
<script type="text/javascript">
var map;
function mapa()
{
	var opts = {'center': new google.maps.LatLng(26.12295, -80.17122), 'zoom':11, 'mapTypeId': google.maps.MapTypeId.ROADMAP }
	map = new google.maps.Map(document.getElementById('mapdiv'),opts);
	
	google.maps.event.addListener(map,'click',function(event) {
		document.getElementById('latlongclicked').value = event.latLng.lat() + ', ' + event.latLng.lng()
	})

	google.maps.event.addListener(map,'mousemove',function(event) {
		document.getElementById('latspan').innerHTML = event.latLng.lat()
		document.getElementById('lngspan').innerHTML = event.latLng.lng()
		document.getElementById('latlong').innerHTML = event.latLng.lat() + ', ' + event.latLng.lng()
	});
	
}
</script>
</body>
</html>

And this is example for get auto complete search:

<!DOCTYPE PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script> 
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
  <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>  
 </head>
 <body>
  <h1>Google Maps Autocomplete Search Sample</h1>
  <div align="left">
   <input type="text" value="" id="searchbox" style=" width:800px;height:30px; font-size:15px;">
  </div>
  <div align="left" id="map" style="width:800px; height: 600px; margin-top: 10px;">
   
  </div>
  
 </body>
</html>
<script type="text/javascript">
 $(document).ready(function(){

  var mapOptions = {
       zoom: 10,
       mapTypeId: google.maps.MapTypeId.ROADMAP,
       center: new google.maps.LatLng(41.06000,28.98700)
     };

  var map = new google.maps.Map(document.getElementById("map"),mapOptions);

  var geocoder = new google.maps.Geocoder();  

     $(function() {
         $("#searchbox").autocomplete({
         
           source: function(request, response) {

          if (geocoder == null){
           geocoder = new google.maps.Geocoder();
          }
             geocoder.geocode( {'address': request.term }, function(results, status) {
               if (status == google.maps.GeocoderStatus.OK) {

                  var searchLoc = results[0].geometry.location;
               var lat = results[0].geometry.location.lat();
                  var lng = results[0].geometry.location.lng();
                  var latlng = new google.maps.LatLng(lat, lng);
                  var bounds = results[0].geometry.bounds;

                  geocoder.geocode({'latLng': latlng}, function(results1, status1) {
                      if (status1 == google.maps.GeocoderStatus.OK) {
                        if (results1[1]) {
                         response($.map(results1, function(loc) {
                        return {
                            label  : loc.formatted_address,
                            value  : loc.formatted_address,
                            bounds   : loc.geometry.bounds
                          }
                        }));
                        }
                      }
                    });
            }
              });
           },
           select: function(event,ui){
      var pos = ui.item.position;
      var lct = ui.item.locType;
      var bounds = ui.item.bounds;

      if (bounds){
       map.fitBounds(bounds);
      }
           }
         });
     });   
 });
</script>

And now how to combine 2 source code from above to 1 sourcecode? This is very simple and not hard. Look at this:

<!DOCTYPE html>
<html>
  <head>
    <title>Places Autocomplete</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <link href="http://code.google.com//apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css">
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>

    <style>
      html, body {
  		height: 80%;
  		margin: 0;
  		padding: 0;
	}

	#map-canvas, #map_canvas {
  		height: 100%;
	}

	@media print {
  		html, body {
    		height: auto;
  	}

  		#map-canvas, #map_canvas {
   		height: 350px;
  		}
	}

	#panel {
		position: absolute;
  		top: 5px;
  		left: 50%;
  		margin-left: -180px;
  		z-index: 5;
  		background-color: #fff;
  		padding: 5px;
  		border: 1px solid #999;
	}
   </style>

<script>
function initialize() {

  var mapOptions = {
    center: new google.maps.LatLng(-33.8688, 151.2195),
    zoom: 13,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById('map-canvas'),
    mapOptions);

  var input = /** @type {HTMLInputElement} */(document.getElementById('searchTextField'));
  var autocomplete = new google.maps.places.Autocomplete(input);

  autocomplete.bindTo('bounds', map);

  var infowindow = new google.maps.InfoWindow();
  var marker = new google.maps.Marker({
    map: map
  });

  google.maps.event.addListener(map,'click',function(event) {
            document.getElementById('latlongclicked').value = event.latLng.lat() + ', ' + event.latLng.lng()
        })

        google.maps.event.addListener(map,'mousemove',function(event) {
            document.getElementById('latspan').innerHTML = event.latLng.lat()
            document.getElementById('lngspan').innerHTML = event.latLng.lng()
            document.getElementById('latlong').innerHTML = event.latLng.lat() + ', ' + event.latLng.lng()
  });

  google.maps.event.addListener(autocomplete, 'place_changed', function() {
    infowindow.close();
    marker.setVisible(false);
    input.className = '';
    var place = autocomplete.getPlace();
    if (!place.geometry) {
      // Inform the user that the place was not found and return.
      input.className = 'notfound';
      return;
    }

    // If the place has a geometry, then present it on a map.
    if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
    } else {
      map.setCenter(place.geometry.location);
      map.setZoom(17);  // Why 17? Because it looks good.
    }
    marker.setIcon(/** @type {google.maps.Icon} */({
      url: place.icon,
      size: new google.maps.Size(71, 71),
      origin: new google.maps.Point(0, 0),
      anchor: new google.maps.Point(17, 34),
      scaledSize: new google.maps.Size(35, 35)
    }));
    marker.setPosition(place.geometry.location);
    marker.setVisible(true);

    var address = '';
    if (place.address_components) {
      address = [
        (place.address_components[0] && place.address_components[0].short_name || ''),
        (place.address_components[1] && place.address_components[1].short_name || ''),
        (place.address_components[2] && place.address_components[2].short_name || '')
      ].join(' ');
    }

    infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
    infowindow.open(map, marker);
  });

  // Sets a listener on a radio button to change the filter type on Places
  // Autocomplete.
  function setupClickListener(id, types) {
    var radioButton = document.getElementById(id);
    google.maps.event.addDomListener(radioButton, 'click', function() {
      autocomplete.setTypes(types);
    });
  }

  setupClickListener('changetype-all', []);
  setupClickListener('changetype-establishment', ['establishment']);
  setupClickListener('changetype-geocode', ['geocode']);
}

google.maps.event.addDomListener(window, 'load', initialize);

</script>
  </head>
  <body>
    <div id="panel" style="margin-left: -260px">
      <input id="searchTextField" type="text" size="50">
      <input type="radio" name="type" id="changetype-all" checked="checked">
      <label for="changetype-all">All</label>

      <input type="radio" name="type" id="changetype-establishment">
      <label for="changetype-establishment">Establishments</label>

      <input type="radio" name="type" id="changetype-geocode">
      <label for="changetype-geocode">Geocodes</lable>
    </div>
    <div id="map-canvas"></div>
    <div class="eventtext">
<div>Lattitude: <span id="latspan"></span></div>

<div>Longitude: <span id="lngspan"></span></div>
<div>Lat Lng: <span id="latlong"></span></div>
<div>Lat Lng on click: 
  <input type="text" id="latlongclicked" style="width:300px; border:1px inset gray;"></span></div>
</div>
  </body>
</html>

Source: http://vikku.info and http://gerger.co

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s