Working with Google Maps Javascript API V3

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.

Latitude

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+'
Longitude : ' + longitude + '' '
'; var infowindow = new google.maps.InfoWindow({ content: contentString });

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;
}

6 comments:

Anonymous November 29, 2011 at 1:55 AM  

thanx a lot... used it in IB
-Rashed

Anonymous November 29, 2011 at 1:56 AM  

thnx a lot... used it in IB
-Rashed

Rashed November 29, 2011 at 1:56 AM  
This comment has been removed by the author.
Anonymous June 5, 2012 at 6:27 PM  

keep it up, man!

Anonymous January 22, 2021 at 2:33 AM  

Сосредоточьтесь не только на напольной плитке заграничного выпуска, но и на наши виды. Кафельная очень хорошо подойдет к каждому интерьеру. Отбирая хороший продукт как, например, energieker официальный сайт все равно придется разобрать множественное число версий. Лучший выбор плитки может приукрасить какую угодно кухню или ванную комнату.

Anonymous February 9, 2021 at 11:49 AM  

Купить икру http://linienheld.de/index.php?title=%D0%97%D0%B0%D0%BA%D1%83%D0%BF%D0%B0%D0%B9%D1%82%D0%B5%20%D0%BA%D1%80%D0%B0%D0%B1%D0%BE%D0%B2%20%D1%83%20%D0%B1%D0%B5%D0%B7%D0%BE%D0%BF%D0%B0%D1%81%D0%BD%D1%8B%D1%85%20%D0%B4%D0%B8%D0%BB%D0%B5%D1%80%D0%BE%D0%B2%20%D0%B2%20%D0%BD%D0%BE%D0%B2%D0%BE%D0%BC%20%D1%88%D0%BE%D1%83-%D1%80%D1%83%D0%BC%D0%B5%20%D0%B4%D0%B0%D1%80%D0%BE%D0%B2%20%D0%BC%D0%BE%D1%80%D1%8F%20%C2%AB%D0%9A%D1%80%D0%B0%D1%81%D0%BD%D1%8B%D0%B9%20%D0%B6%D0%B5%D0%BC%D1%87%D1%83%D0%B3%C2%BB имеется возможность в поштучно или небольшими партиями. Доставка выполняется прямо в день заказа. С целью оформления заказа на рыбу в нужном объеме достаточно позвонить менеджеру или сформировать удаленный заказ именно на сайте redgmshop.ru.

Total Pageviews

Tags

Twitter Updates
    follow me on Twitter

    Followers