locked
Find out lattitude and longitude based on ZIP RRS feed

  • Question

  • HI,

     

    Please let me know, How I can get cordinates(lat & long) on the basis of ZIP code of a city.

     

     

    Thanks

    pushpendra singh

    Wednesday, October 8, 2008 1:55 PM

Answers

  • Try the following code out. It allows you to plot an anddress on a map by either filling in a form type address or by entering in the complete address in one text box. Enter your zip code into either the "Where" field or the "Postal/Zip code" field. Note that zip code regions are often oddly shaped and the pushpins are placed at the approximate "center of mass" location.


    Code Snippet

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html>
       <head>
        <title>Map Query</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <script src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2"></script>
        <script>
            var map = null;
            var layer = null;
            function GetMap()
            {
                map = new VEMap('myMap');
                map.LoadMap();
                layer = new VEShapeLayer();
                map.AddShapeLayer(layer);
               
                map.AttachEvent("onmousedown",MouseHandler);
                var options = new VEPrintOptions(true);
                map.SetPrintOptions(options);
            }  
            
              function AddPin(ll)
              { 
                   pin = new VEShape(VEShapeType.Pushpin,ll);
                layer.AddShape(pin);
                document.coord.lat.value = ll.Latitude;
                document.coord.long.value = ll.Longitude; 
              } 
       
            function getLatLong(layer, resultsArray, places, hasMore, veErrorMessage)
            {   
                var ll = places[0].LatLong;
                AddPin(ll);
            }
       
            function FindAddy()
            {
                var street=document.getElementById("street").value;
                var city=document.getElementById("city").value;
                var province=document.getElementById("province").value;
                var country=document.getElementById("country").value;
                var psotalcode=document.getElementById("postalcode").value;
               
                var address="";
               
                if(street!=null&street!="")
                    address+=street;
               
                if(city!=null&city!="")
                {
                    if(address!="")
                        address+=", ";
                       
                    address+=city;
                }
               
                if(province!=null&province!="")
                {
                    if(address!="")
                        address+=", ";
                       
                    address+=province;
                }
               
                if(country!=null&country!="")
                {
                    if(address!="")
                        address+=", ";
                       
                    address+=country;
                }
               
                if(psotalcode!=null&psotalcode!="")
                {
                    if(address!="")
                        address+=", ";
                       
                    address+=psotalcode;
                }
               
                map.Find(null, address, null, null, null, null, null, null, null,null, getLatLong);
            }
           
            function FindLoc()
              {
                 try
                 {
                    var iframe = document.getElementsByTagName("IFRAME");
                   
                    for(var i=0;i<iframe.length;i++)
                    {
                        try{
                        if(iframe[i].nextSibling.id=="myMap_veplacelistpanel")
                        {
                            iframe[i].style.display="none";
                            document.getElementById("myMap_veplacelistpanel").style.display="none";
                        }
                        }catch(e){}
                    }
                   
                     var what=document.getElementById('txtWhat').value;
                    map.Find(what, document.getElementById('txtWhere').value, null, null, null, null, null, null, null, null, getLatLong);
                 }
                 catch(e)
                 {
                    alert(e.message);
                 }
              }
           
         function MouseHandler(e)
         {
             if(e.rightMouseButton)
             {
                 var x = e.mapX;
                 var y = e.mapY;
                 var origin = map.PixelToLatLong(new VEPixel(x,y));
                    
                 AddPin(origin);
             }
         }
        </script>
        <style>
          #myMap
          {
              cursor:crosshair !important;
           }
        </style>
    </head>

    <body onload="GetMap();">
    <CENTER>
    What: <INPUT id="txtWhat" type="text" name="txtWhat" value=>
    Where:    <INPUT id="txtWhere" type="text" name="txtWhere" value=>
        <INPUT id="find" type="button" value="Find" name="find" onclick="FindLoc();"><br/>
        <hr style="width:600px;"/>
        <table>
        <tr>
        <td>Street:</td>
        <td><INPUT id="street" type="text" name="street" value=></td>
        </tr>
        <tr>
        <td>City:</td>
        <td><INPUT id="city" type="text" name="city" value=></td>
        </tr>
        <tr>
        <td>Province/State:</td>
        <td><INPUT id="province" type="text" name="province" value=></td>
        </tr>
        <tr>
        <td>Country:</td>
        <td><INPUT id="country" type="text" name="country" value=></td>
        </tr>
        <tr>
        <td>Postal/Zip Code:</td>
        <td><INPUT id="postalcode" type="text" name="postalcode" value=></td>
        </tr>
        </table>
              <INPUT id="findaddress" type="button" value="Find Address" name="findaddress" onclick="FindAddy();"><br/>
        <div id='myMap' style="position:relative; width:600px; height:400px;"></div>
             
        <FORM ACTION="#" NAME=coord>
            <INPUT TYPE=TEXT NAME="lat" SIZE=20 >
            <INPUT TYPE=TEXT NAME="long" SIZE=20>
        </FORM>     
       
        Right mouse click on map, or enter location
    </CENTER>
    </body>
    </html>

    Wednesday, October 8, 2008 3:36 PM
    Moderator