Friday, March 26, 2010

Google Maps API used to map multiple locations.

Hi,

I was recently working on a project where i need to fetch multiple locations used google maps API. I searched a lot and tried many examples but was'nt able to find any appropriate, finally i gone through the documentation of google maps API. I found the solution

Below is the list where i stored all my addresses


var addresses =new Array();

addresses[] = '';



Here's my onload function


function load() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(10, -180), 13);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());

        geocoder = new GClientGeocoder();
      
        GEvent.addListener(map, 'click', function(overlay, point) {
     if (point) {
       //document.getElementById("show_x").innerHTML = point.x;
       // document.getElementById("show_y").innerHTML = point.y;
     }
   });
  
   GEvent.addListener(map, 'zoomend',
                       function(oldZoomLevel, newZoomLevel) {
     //document.getElementById("zoom_old").innerHTML = oldZoomLevel;
     //document.getElementById("zoom_new").innerHTML = newZoomLevel;
   });
  
     }

showAddress();
    }

Here's my show address function


 function showAddress() {
       //alert("address : " + addresses);
     convertToLatLng();
    }

Here's my convertToLatLng, this function is the core function i used for


 var points = [];
    function convertToLatLng() {
      if (points.length >= addresses.length) {
        showPoints();
      } else {
        geocode(addresses[points.length]);
      }
    }

My geocode function


 function geocode(address) {
//alert(address);
   geocoder.getLatLng(
          address,
          function(point) {
          
           if(!point)
           {
          
           }
           else
           {
              points.push(point);
              window.setTimeout(convertToLatLng, 300);
              map.setCenter(point, 13);
             //var marker = new GMarker(point);
             var letters=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P", "Q","R","S","T","U","V","W","X","Y","Z"];
            
             var marker=createMarker(point, letters[points.length-1]);

              map.addOverlay(marker);
              //marker.openInfoWindowHtml(address + "
Long: " + point.x + "
Lati:  " + point.y);
              //document.getElementById("show_x").innerHTML = point.x;
        //document.getElementById("show_y").innerHTML = point.y;
        //document.getElementById("zoom_new").innerHTML = 14;
            }
          }
        );
    }

My show points function, this function displays points for each location fetched from the database


function showPoints() {
      for (i in points) {
        var lat_lng = points[i];
        if (lat_lng) {
          var marker=createMarker(points[i], letters[i]);
 map.addOverlay(marker);
          //map.addOverlay(new GMarker(lat_lng));
        }
      }
    }

I tweaked a bit and created alphabetical markers for the map as below


     function createMarker(point, letter) {
    // use a custom icon with letter A - Z


var letter = letter;
var myIcon = new GIcon(G_DEFAULT_ICON, "http://www.google.com/mapfiles/marker" + letter + ".png");
myIcon.printImage = "http://maps.google.com/mapfiles/marker"+letter+"ie.gif";
myIcon.mozPrintImage = "http://maps.google.com/mapfiles/marker"+letter+"ff.gif";
var CallSign = CallSign;
var marker = new GMarker(point, {icon:myIcon});
//GEvent.addListener(marker, "click", function()
//{
//
//// save the info we need to use later for the side_bar
//gmarkers.push(marker)
//// add a line to the side_bar html
//
//
//
//});
return marker;
}

This is how i did to display multiple locations on the map.

Cheers!!!

Ujjwal








 

8 comments:

pass said...

朝著既定的目標走,就不會迷失。........................................

憲妤憲妤 said...

與其爭取不可能得到的東西,不如善自珍惜運用自己所擁有的........................................

念強 said...

God helps those who help themselves...................................................

Porsha蔡Coghlan明宏 said...

不勞而獲的事情,並沒有價值......................................................

皓珊 said...

I love readding, and thanks for your artical. ........................................

雅威 said...

先為別人的快樂著想,是超人;先為自己的快樂著想,是凡人;使別人不快樂,自己也不快樂的,是笨人。..................................................

OliveWol8888 said...

您的blog蠻不錯的耶,祝你快樂哦!期待您的更新!........................................

劉KarolR_Sundquis said...

大家隨便說,說什麽都好,就是請不要惦惦.............................................