Google Maps Javascript API V3 is an awesome API provided by Google. Today I will give you a little flavor about this API. Later I will write about some advance features.
Here is a simple tool, created using this API. Click on the marker, the Latitude and Longitude position will be displayed in a window and also in the textbox. If you set a Latitude, Longitude point in the textbox and click on the button, that point will be shown in the map with a marker.
Longitude
Now I'm going to show you, how you can make this tool..
First load the Google Maps Javascript API and the latest jQuery version . Then design the user interface.
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> <script type="text/javascript" src="http://zmodal.googlecode.com/files/jquery-1.6.1.min.js"></script> <div style="float:left; width:200px; border:1px solid #CCCCCC;"> Latitude <input type="text" id="tbLattitude"/><br/> Longitude <input type="text" id="tbLongitude"/><br/> <input type="button" name="btCal" value="Show in Map" onclick="return showInMap();" /> </div> <div id="map_canvas" style="float:left;width: 400px; height: 400px; border: 2px solid #999;margin-left:10px;"> </div>
The DIV element with id 'map_canvas' will be the container of the Map. This id will be required when you create the map object to display in this div.
Now come to the Javascript part..
Set the window onLoad callback method and in that method, call the LoadMap method with initial latitude,longitude.
google.maps.event.addDomListener(window, 'load', initializeMap); function initializeMap() { LoadMap(23.70992, 90.40714); }
In the LoadMap method first create a LatLng object using the lat, long parameter
function LoadMap(lat, longitude) { var latlng = new google.maps.LatLng(lat, longitude);Create a Map object using map options and the container ID
var myOptions = { zoom: 12, center: latlng, // previously created LatLng object mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
For the information window first create the content of the window then create a InfoWindow object using the content. This object will be later used in the onClick event of the Marker.
var contentString = '' + 'Latitude : '+lat+''; var infowindow = new google.maps.InfoWindow({ content: contentString });
Longitude : ' + longitude + '' '
Now create the Marker object using the LatLng and Map object.
var marker = new google.maps.Marker({ position: latlng, map: map, title: 'Location', draggable: true, animation: google.maps.Animation.DROP });
Finally add an event listener for the Marker object. First open the InfoWindow object then call the method which set LatLng value to the textbox.
google.maps.event.addListener(marker, 'click', function (event) { infowindow.open(map, marker); setLatLongToTextBox(event.latLng); }); }Here is the setLatLongToTextBox and showInMap method
function setLatLongToTextBox(latlong) { var latstr = latlong + ''; var value = latstr.slice(1, latstr.length - 1); var latlongs = value.split(","); $("#tbLattitude").attr("value", latlongs[0]); $("#tbLongitude").attr("value", latlongs[1]); } //this method is used as the onclick method of the button function showInMap() { var lat = $("#tbLattitude").attr("value"); var longitude = $("#tbLongitude").attr("value"); LoadMap(lat, longitude); return false; }