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








 

Array List Sorting Issue

Hi,

Recently, i was coding for an application and came across a problem where i need to sort an ArrayList in an alphabetical order. However, that sounded quite easy as because sorting can easy with single arraylist. But, i was in a situation where i had two different ArrayLists with multiple attributes. So, what i did is that i kept a delimeter to distinct all my 3 values and then i added them into a temporary list.


for(int j=0;j
{
myBean=(MyBean)myList.get(j);
tempList.add(myBean.getOne() + "*" + myBean.getTwo() + "*" + myBean.getThree());

then i used collection's sort method to sort them up in an alphabetical form.

Collections.sort(tempList,new MyComparator());  


I used inner class called Mycomparator to compare.


 class MyComparator implements Comparator {   
         
         public int compare(Object o1,   
                            Object o2) {   
            String s1 = o1.toString().toUpperCase();   
            String s2 = o2.toString().toUpperCase();   
    
            int counter = 0;   
    
            for (counter=0;counter
                               !Character.isDigit(s1.charAt(counter));counter++);   
    
            String temp1 = s1.substring(counter);   
    
            s1 = s1.substring(0,counter);   
    
            for (counter=0;counter
                           !Character.isDigit(s2.charAt(counter));counter++);   
    
            String temp2 = s2.substring(counter);   
    
            s2 = s2.substring(0,counter);   
    
            int max = (temp1.length() > temp2.length()) ? temp1.length() : temp2.length();   
    
            char[] pad = new char[max-temp1.length()];   
    
            Arrays.fill(pad,(char)48);   
    
            temp1 = String.valueOf(pad) + temp1;   
    
            pad = new char[max-temp2.length()];   
    
            Arrays.fill(pad,(char)48);   
    
            temp2 = String.valueOf(pad) + temp2;   
    
            s1 = s1 + temp1;   
            s2 = s2 + temp2;   
    
            return(s1.compareTo(s2));   
         }   
    
         public boolean equals(Object o1,   
                               Object o2) {   
            String s1 = o1.toString();   
            String s2 = o2.toString();   
    
            return(s1.equals(s2));   
         }   
    
      }   


And finally,. the problem got resolved, i got my list ordered in alphabetical form.