Wednesday, October 6, 2010

Google Map with multiple markers

Google Map with multiple Markers used to locate different locations .
We can show a map with image and contact details like following


Example for Placing multiple markers with image on a Google Map

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
 <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" type="text/javascript"></script>
 <style type="text/css">
#map{
  height: 150%;
  width: 100%;
    }
 </style>

<script type="text/javascript">

    var map;
    var geocoder;
       var xml;
       var markers;
       var mapopt;
       var address;
    var    markeraddress;
    var k=0;
   
     // On page load, call this function
   function load()
   {
     // Create new map object
      map = new GMap2(document.getElementById("map")); 
      map.addControl(new GMapTypeControl());
      map.setMapType(G_SATELLITE_MAP);
      map.addControl(new GLargeMapControl3D());
      // Create new geocoding object
      geocoder = new GClientGeocoder();
      var latlonarr=new Array();
      var arselected = new Array(); 
      var length1=0;
      var loopcount=0;
      var sw=0;
     
        arselected[0]="Bhoopalpally Phase -II,Hanamkonda,Warangal";
        arselected[1]="Gopalan arcade.4th flore,1st Main,Kasturi Nagar,Bangalore,KA";
        arselected[2]="Sri Mobiles,No 2,24th Main,JP Nagar 1st Phase,Bangalore,KA";
        arselected[3]="#33,RK  Mutt Road,Ulsoor,Bangalore 560008,KA";
        arselected[4]="Minnu Tour & Travels No 74,Nagavarapalya,CV Raman Nagar,Bangalore,KA";
        arselected[5]="Fazal Technologies,No 89,Fazal Manor,Richmond Road,Bangalore,KA";
        arselected[6]="Neon Electronics,No 335/,Gokul Complex,Sampige Road,Malleshwaram,Bangalore,KA";
        arselected[7]="Canadian International School Survey No.420,Yelahanka,Bangalore,KA";
        arselected[8]="485,6th 'D' Cross,Koramangala 6th Block,Bangalore,KA";
        arselected[9]="Salarpuria Infinity, 3rd Floor #5,Bannerghatta Road ,Bangalore-560029,KA";
        arselected[10]="BMTC,central offices,K.H.Road,Shanthi nagar,Bangalore-560027";
        arselected[11]="57-A, 21st K M, Hosur Road, Bommasandra Industrial Area,Bangalore,KA";
        arselected[12]="63/8.Maruthi Layout,Near Rajaram Factory,2nd Phase,Peenya Industrial Area,Bangalore,KA";
        arselected[13]="Plot 1&2, KIADB Industrial area, Attibele,Bangalore,KA";
        arselected[14]="Apt 203, Scion Enclave, Govindappa Lane, Off 80 ft rd,Indira nagar,Bangalore,KA";
        
          var address=new Array();
        var exact_address=new Array();
          var exact=new Array();
        var j;
        var count=0;
        var marker=new Array();
        var exact_add=new Array();
        var xmlfl="<markers>";
        var s=0;
        var km=0;
        var url="'http://www.google.com','google','width=400,height=400'";
         for (var i = 0; i < arselected.length; i++)
          {
            address[i]= arselected[i];
            var len=address[i].split(',').length;
            exact=address[i].split(',');
            for(var k=0;k<3;k++)
            {
               exact_add[k]=exact[parseInt(len)-(parseInt(k)+1)];
            }
           
            exact_add.reverse();
            exact_address[i]=exact_add;
            s=i+1;
                xmlfl+="<marker address=\""+exact_address[i]+"\">";
                xmlfl+="<AlphaCharacter>"+s+"</AlphaCharacter></marker>";
              }
            xmlfl+="</markers>";
            document.getElementById("sample").innerHTML="";
            document.getElementById("sample").innerHTML=xmlfl;
            markers =document.getElementById("sample").getElementsByTagName("marker");
            for (var i = 0; i < exact_address.length; i++)
            { 
                    markeraddress = markers[i].getAttribute("address");
                  geocoder.getLocations(markeraddress,addToMap);
                 document.getElementById("loop").value=i;           
            }
               
         function addToMap(response)
                       {
                            
                              place = response.Placemark[0];
                             point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
                             latlonarr[km]=point;
                             km++;
                            sendpoint(point);                                     
                   }
                function sendpoint(point)
                {
                             var numberedIcon = new GIcon(G_DEFAULT_ICON)
                            var ar=address[loopcount];
                            map.setCenter(point, 13);
                             numberedIcon.image = "http://www.geocodezip.com/mapIcons/marker.png"
                            var k=document.getElementById("loop").value;
                             markerOptions = { icon:numberedIcon, clickable:true,title:ar,position:loopcount}
                             mapopt= { icon:numberedIcon, clickable:true,title:ar,position:loopcount};
                             marker= new GMarker(point,mapopt);
                            map.addOverlay(marker);
                            loopcount++;   
                }
                GEvent.addListener(map,"click",function(marker,markerOptions,ar)
                                           { 
                                                   var html="";
                                                   var ra="";
                                                   var sowji=0;
                                                for(var siri=0;siri<15;siri++)
                                                {
                                                   if(latlonarr[siri]==ar)
                                                   {
                                                      sowji=siri;
                                                      ra=address[siri-1];
                                                    } 
                                                }
                                               
                                                 html=ra+"<br><img src=\"im/css.jpg\"  height=\"160px\" width=\"240px\"/>For More Information <a href=javascript:popupwin("+url+")><b>click<b></a>";
                                                marker.openInfoWindowHtml(html,{maxWidth:200},markerOptions);
                                            });
              }
             
    function popupwin(url)
    {
        newwindow=window.open(url);
        if (window.focus) {newwindow.focus();}
    }
    </script>

  </head>
 <body onLoad="load()">

<input type="hidden" name="loop" id="loop">
<div id="map"></div>

<div id="sample" style="display=none"></div>

<div id=addr123 style="display:block">
1.Kalyan Meher Kotay, 15/15,25th Cross,JayaNagar 6th Main Rd,BANGALORE,KA;<br>
2.Gopalan arcade.4th flore,1st Main,Kasturi Nagar,Bangalore,KA;<br>

                     
</div>

</body>
</html>





Monday, October 4, 2010

Sending JSON object to Java and processing

In java script :
function adjustments()
{
    //validation
    var st1="";
    var st2="";
    var gridsData;
    var mygridOne=new Array();
    var mygridTwo=new Array();
    var mygrid1Arry=null;
    var mygrid2Arry=null;

       if(document.getElementById("adjNature").value!='FIFO')
        {
     mygrid1Arry=mygrid1.getSelectedRecords();
     mygrid2Arry=mygrid2.getSelectedRecords();
        }
        else
            {
                   
                mygrid1Arry=Sigma.Grid.getAllRows(mygrid1);
                mygrid2Arry=Sigma.Grid.getAllRows(mygrid2);
                }

       if(mygrid1Arry.length>0 && mygrid2Arry.length>0)
    {

         if(beforeAdjustvalidate())
        {
        for(var i=0;i
        {
            var docType1= mygrid1Arry[i].doctype;
            if(docType1==undefined) { docType1=mygrid1.getColumnValue('doctype', i);}
            var docNum1= mygrid1Arry[i].docno;
              if(docNum1==undefined) { docNum1=mygrid1.getColumnValue('docno', i);}
            var docDate1= mygrid1Arry[i].docdate;
             if(docDate1==undefined) { docDate1=mygrid1.getColumnValue('docdate', i);}
            var docAmount1= mygrid1Arry[i].docamt;
             if(docAmount1==undefined) { docAmount1=mygrid1.getColumnValue('docamt', i);}
               var amtAdjusted1= mygrid1Arry[i].amtadjusted;
             if(amtAdjusted1==undefined) { amtAdjusted1=mygrid1.getColumnValue('amtadjusted', i);}
            var balance1= mygrid1Arry[i].balance;
             if(balance1==undefined) { balance1=mygrid1.getColumnValue('balance', i);}
            var currentAdjustment1= mygrid1Arry[i].curradj;
             if(currentAdjustment1==undefined) { currentAdjustment1=mygrid1.getColumnValue('curradj', i);}
            if(i==(mygrid1Arry.length-1))
            {
                st1+="{docType:'"+docType1+"',docNum:'"+docNum1+"',docDate:'"+docDate1+"',docAmount:'"+docAmount1+"',amtAdjusted:'"+amtAdjusted1+"',balance:'"+balance1+"',currentAdjustment:'"+currentAdjustment1+"'}";

            }else
                st1+="{docType:'"+docType1+"',docNum:'"+docNum1+"',docDate:'"+docDate1+"',docAmount:'"+docAmount1+"',amtAdjusted:'"+amtAdjusted1+"',balance:'"+balance1+"',currentAdjustment:'"+currentAdjustment1+"'},";
        }
        for(var j=0;j
        {
            var docType2= mygrid2Arry[j].doctype;
             if(docType2==undefined) { docType2=mygrid2.getColumnValue('doctype', j);}
            var docNum2= mygrid2Arry[j].docno;
             if(docNum2==undefined) { docNum2=mygrid2.getColumnValue('docno', j);}
            var docDate2= mygrid2Arry[j].docdate;
             if(docDate2==undefined) { docDate2=mygrid2.getColumnValue('docdate', j);}
            var docAmount2= mygrid2Arry[j].docamt;
             if(docAmount2==undefined) { docAmount2=mygrid2.getColumnValue('docamt', j);}
            var amtAdjusted2= mygrid2Arry[j].amtadjusted;
             if(amtAdjusted2==undefined) { amtAdjusted2=mygrid2.getColumnValue('amtadjusted', j);}
            var balance2= mygrid2Arry[j].balance;
             if(balance2==undefined) { balance2=mygrid2.getColumnValue('balance', j);}
            var currentAdjustment2= mygrid2Arry[j].curradj;
             if(currentAdjustment2==undefined) { currentAdjustment2=mygrid2.getColumnValue('curradj', j);}
      
            if(j==(mygrid2Arry.length-1))
            { //alert("docType2-----"+docType2+"---");
                st2+="{docType:'"+docType2+"',docNum:'"+docNum2+"',docDate:'"+docDate2+"',docAmount:'"+docAmount2+"',amtAdjusted:'"+amtAdjusted2+"',balance:'"+balance2+"',currentAdjustment:'"+currentAdjustment2+"'}";
            }
            else
                st2+="{docType:'"+docType2+"',docNum:'"+docNum2+"',docDate:'"+docDate2+"',docAmount:'"+docAmount2+"',amtAdjusted:'"+amtAdjusted2+"',balance:'"+balance2+"',currentAdjustment:'"+currentAdjustment2+"'},";
        }

        st1="["+st1+"],";
        st2="["+st2+"]";
        gridsData='{"grid1-dr":'+ st1;
        gridsData+='"grid2-cr":'+ st2+'}';
 
        var toServer=gridsData;
                       
        /* code started sending JSON Object to server */
        xmlhttp = false;
        if (window.XMLHttpRequest) {// code for Firefox, Opera, IE7, etc.
            xmlhttp=new XMLHttpRequest();
        } else if (window.ActiveXObject) {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (xmlhttp!=null) {
            url=appsPath+"/finance/adjustCrDrdocs?adjNature="+encodeURIComponent(document.getElementById("adjNature").value);
            xmlhttp.onreadystatechange= function () {
                getAdjDetailsCallback(xmlhttp);
            };
            xmlhttp.open("POST", url, true);
            xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            xmlhttp.send(toServer);

        } else {
            alert("Your browser does not support XMLHTTP.");
        }

        }
    /* code ended sending JSON Object to server */


    }

    
    else
    {
        alert("Please select corresponding Cr/Dr documents");
        return false;
    }


}




function getAdjDetailsCallback()
{
    if (xmlhttp.readyState == 4)
    { // Complete
        alert(xmlhttp.responseText);
     var message=xmlhttp.responseXML;
      var recordlength=message.getElementsByTagName("grid").length;
      var docsArray=new Array();
      var amtAdjustedArray=new Array();
              var amtadjusted1=0;
      for(var i=0;i
          {
             var amtadjusted="";
              var record=message.getElementsByTagName("grid");
              var docnum=record[i].getElementsByTagName("docnum")[0].childNodes[0].nodeValue;
              amtadjusted=record[i].getElementsByTagName("amtadjusted")[0].childNodes[0].nodeValue;
              var balance=record[i].getElementsByTagName("balance")[0].childNodes[0].nodeValue;

        
                      var index;
                            var totaldrRecords = Sigma.Grid.getAllRows(mygrid1).size();
                           for(var t=0;t
                               {
                                  var docNumDr= mygrid1.getColumnValue("docno",t);
                                  if(docnum==docNumDr)
                                      {
                                        index=t;
                                         mygrid1.setColumnValue("amtadjusted", index,amtadjusted);
                                         mygrid1.setColumnValue("balance", index,balance);
                                        mygrid1.refresh();
                                       
                                      }
                               }
                                  
                 
               
                           var index1;
                          var totalcrRecords = Sigma.Grid.getAllRows(mygrid2).size();
                           for(var t1=0;t1
                               {
                                  var docNumDr= mygrid2.getColumnValue("docno",t1);
                                  if(docnum==docNumDr)
                                      {
                                        index1=t1;
                                       mygrid2.setColumnValue("amtadjusted", index1,amtadjusted);
                               mygrid2.setColumnValue("balance", index1,balance);
                               mygrid2.refresh();
                                      }
                               }
     }

    }
}


In Java :

public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        System.out.println("-----------------in AdjustmentAction--execute");
        String gridsData = readJSON(request);
        String adjNature = request.getParameter("adjNature") != null ? request.getParameter("adjNature") : "";
        System.out.println("adjNature---------"+adjNature);
        //Use the JSON-Java binding library to create a JSON object in Java
        JSONObject jsonObject = null;
        StringBuffer sb = new StringBuffer();
        response.setContentType("text/xml");
        JSONArray gridOneArray;
        JSONArray gridOneTwo;
        try {
            jsonObject = new JSONObject(gridsData);
            System.out.println("jsonObject-----" + jsonObject);

            System.out.println("grid1------" + jsonObject.get("grid1-dr"));
            System.out.println("grid2------" + jsonObject.get("grid2-cr"));
            System.out.println("grid1-getJSONArray-----" + jsonObject.getJSONArray("grid1-dr"));
            System.out.println("grid2--getJSONArray----" + jsonObject.getJSONArray("grid2-cr"));
            gridOneArray = jsonObject.getJSONArray("grid1-dr");
            gridOneTwo = jsonObject.getJSONArray("grid2-cr");



            sb.append("<message>");
            sb.append("<grids>");
            for (int i = 0; i < gridOneArray.length(); i++) {
                System.out.println("outer for loop 1");
                JSONObject record1 = (JSONObject) gridOneArray.get(i);
                String docType1 = record1.getString("docType");
                Double balance1 = record1.getDouble("balance");
                Double currentAdjustment1;
                try{
                     currentAdjustment1= record1.getDouble("currentAdjustment");
                }catch(Exception e){ currentAdjustment1=0.0d;}
                Double docAmount1 = record1.getDouble("docAmount");
                Double amtAdjusted1 = record1.getDouble("amtAdjusted");
                String docDate1 = record1.getString("docDate");
                String docNum1 = record1.getString("docNum");
                System.out.println("outer for loop 2");

                for (int j = 0; j < gridOneTwo.length(); j++) {
                    JSONObject record2 = (JSONObject) gridOneTwo.get(j);
                    String docType2 = record2.getString("docType");
                    Double balance2 = record2.getDouble("balance");
                  Double currentAdjustment2;
                try{
                     currentAdjustment2= record2.getDouble("currentAdjustment");
                }catch(Exception e){ currentAdjustment2=0.0d;}
                    Double docAmount2 = record2.getDouble("docAmount");
                    Double amtAdjusted2 = record2.getDouble("amtAdjusted");
                    String docDate2 = record2.getString("docDate");
                    String docNum2 = record2.getString("docNum");
                    System.out.println("inner for loop");
                    /*
                     *  Respose XML
                    <message><grids>
                    <grid><docnum>CR0000081</docnum><amtadjusted>9000.0</amtadjusted><balance>3000.0</balance><adjustedDoc>CI0000061</adjustedDoc></grid>
                    <grid><docnum>CI0000061</docnum><amtadjusted>19000.0</amtadjusted><balance>0.0</balance></grid>
                    <grid><docnum>CR0000051</docnum><amtadjusted>10000.0</amtadjusted><balance>10000.0</balance><adjustedDoc>CI0000061</adjustedDoc></grid>
                    </grids></message>
                     *
                     */
                    //business logic for manual adjustment starts here
                    if (adjNature != null && adjNature.equals("manual")) {
                        if (currentAdjustment1 <= currentAdjustment2) {    // Step 1 : deduct currentadj1 in balance2,then if balance1=0 then close the status of doctype1
                            //Step 2 : if balance1!=0,then go to next credit document

                            //Step 1 execution starts here
                            System.out.println("if currentAdjustment1<=balance2--strats");
                            balance2 = balance2 - currentAdjustment2;
                            amtAdjusted2 = amtAdjusted2 + currentAdjustment2;
                            amtAdjusted1 += currentAdjustment1;
                            balance1 = balance1 - currentAdjustment1;
                            sb.append("<grid>");
                            sb.append("<docnum>" + docNum1 + "</docnum>");
                            sb.append("<amtadjusted>" + amtAdjusted1 + "</amtadjusted>");
                            sb.append("<balance>" + balance1 + "</balance>");
                            sb.append("</grid>");
                            sb.append("<grid>");
                            sb.append("<docnum>" + docNum2 + "</docnum>");
                            sb.append("<amtadjusted>" + amtAdjusted2 + "</amtadjusted>");
                            sb.append("<balance>" + balance2 + "</balance>");
                            sb.append("<adjustedDoc>" + docNum1 + "</adjustedDoc>");
                            sb.append("</grid>");
                            System.out.println("if currentAdjustment1<=balance2--ends");
                        } else if (currentAdjustment1 > currentAdjustment2) {
                            //Step 1 : deduct balance2 in currentAdjustment1 then if balance2=0, then close doctype2
                            System.out.println("if currentAdjustment1>balance2--strats");
                            amtAdjusted2 = amtAdjusted2 + currentAdjustment2;
                            amtAdjusted1 += currentAdjustment2;
                            currentAdjustment1 = currentAdjustment1 - currentAdjustment2;
                            System.out.println("currentAdjustment1-2--" + currentAdjustment1);
                            balance1 = balance1 - currentAdjustment2;
                            balance2 = balance2 - currentAdjustment2;
                            sb.append("<grid>");
                            sb.append("<docnum>" + docNum2 + "</docnum>");
                            sb.append("<amtadjusted>" + amtAdjusted2 + "</amtadjusted>");
                            sb.append("<balance>" + balance2 + "</balance>");
                            sb.append("<adjustedDoc>" + docNum1 + "</adjustedDoc>");
                            sb.append("</grid>");
                            System.out.println("if currentAdjustment1>balance2--ends");

                        }
                    // manual end


                    }// END MANUAL IF
                    // FIFO STARTS
                    else if (adjNature != null && adjNature.equals("FIFO")) {
                      if(balance1<=balance2)
                        {    // Step 1 : deduct balance1 in balance2,then if balance1=0 then close the status of doctype1
                        //Step 2 : if balance1!=0,then go to next credit document

                        //Step 1 execution starts here
                        System.out.println("if balance1<=balance2--strats");
                        balance2=balance2-balance1;
                        amtAdjusted2=amtAdjusted2+balance1;
                        amtAdjusted1+=balance1;
                        balance1=balance1-balance1;
                        sb.append("<grid>");
                        sb.append("<docnum>"+docNum1+"</docnum>");
                        sb.append("<amtadjusted>"+amtAdjusted1+"</amtadjusted>");
                        sb.append("<balance>"+balance1+"</balance>");
                        sb.append("</grid>");
                       
                        sb.append("<grid>");
                        sb.append("<docnum>"+docNum2+"</docnum>");
                        sb.append("<amtadjusted>"+amtAdjusted2+"</amtadjusted>");
                        sb.append("<balance>"+balance2+"</balance>");
                        sb.append("<adjustedDoc>"+docNum1+"</adjustedDoc>");
                        sb.append("</grid>");
                        System.out.println("if currentAdjustment1<=balance2--ends");
                        }
                        else if(balance1>balance2)
                        {
                        //Step 1 : deduct balance2 in currentAdjustment1 then if balance2=0, then close doctype2
                        System.out.println("if currentAdjustment1>balance2--strats");
                        amtAdjusted2=amtAdjusted2+balance2;
                        amtAdjusted1+=balance2;
                        currentAdjustment1=currentAdjustment1-balance2;
                        System.out.println("currentAdjustment1-2--"+currentAdjustment1);
                        balance1=balance1-balance2;
                        balance2=balance2-balance2;
                         sb.append("<grid>");
                        sb.append("<docnum>"+docNum1+"</docnum>");
                        sb.append("<amtadjusted>"+amtAdjusted1+"</amtadjusted>");
                        sb.append("<balance>"+balance1+"</balance>");
                        sb.append("</grid>");
                        sb.append("<grid>");
                        sb.append("<docnum>"+docNum2+"</docnum>");
                        sb.append("<amtadjusted>"+amtAdjusted2+"</amtadjusted>");
                        sb.append("<balance>"+balance2+"</balance>");
                        sb.append("<adjustedDoc>"+docNum1+"</adjustedDoc>");
                        sb.append("</grid>");
                        System.out.println("if currentAdjustment1>balance2--ends");

                        }
                        
                    } //FIFO end

                } //inner for loop


            } //outer for loop
            sb.append("</grids>");
            sb.append("</message>");
        } //try
        catch (Exception pe) {
            pe.printStackTrace();
        }


        System.out.println("sb.toString()-----" + sb.toString());
        response.getWriter().write(sb.toString());
        return null;

    }

    private String readJSON(HttpServletRequest request) {
        StringBuffer json = new StringBuffer();
        String line = null;
        try {
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine()) != null) {
                json.append(line);
            }
        } catch (Exception e) {
            System.out.println("Error reading JSON String: " + e.toString());
        }
        return json.toString();
    }