Saturday, May 28, 2011

MSN Empeded Map into your application

 Design the page to get user inputs(area,city,address,etc)

<div id="divScroll" runat="server">
        <table width="490px" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
                <td colspan="2">
                    <asp:Label ID="lblNotification" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td height="45" align="right" class="login_text" style="padding-right: 5px;">
                    Locaion name :
                </td>
                <td height="45" align="left">
                    <asp:TextBox ID="txtLocationname" class="textBox" runat="server" Width="300px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td height="45" align="right" class="login_text" style="padding-right: 5px;">
                    Address :
                </td>
                <td height="45" align="left">
                    <asp:TextBox ID="txtAddress1" runat="server" class="textBox" Width="300px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td height="45" align="right" class="login_text" style="padding-right: 5px;">
                    City :
                </td>
                <td height="45" align="left">
                    <asp:TextBox ID="txtCity" runat="server" class="textBox" Width="300px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td height="45" align="right" class="login_text" style="padding-right: 5px;">
                    Postal code :
                </td>
                <td height="45" align="left">
                    <asp:TextBox ID="txtPostalcode" class="textBox" runat="server" Width="300px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="right" class="login_text" style="padding-right: 5px; height: 35px;">
                    Country :
                </td>
                <td align="left" style="height: 40px">
                    <asp:DropDownList ID="ddlCountry" runat="server" AppendDataBoundItems="true">
                        <asp:ListItem Value="--select--" Text="--select--"></asp:ListItem>
                        <asp:ListItem Value="AF">Afghanistan</asp:ListItem>
                        <asp:ListItem Value="AX">Åland Islands</asp:ListItem>
                          <asp:ListItem Value="VA">Holy See (Vatican City State)</asp:ListItem>
                        <asp:ListItem Value="HN">Honduras</asp:ListItem>
                        <asp:ListItem Value="HK">Hong Kong</asp:ListItem>
                        <asp:ListItem Value="HU">Hungary</asp:ListItem>
                        <asp:ListItem Value="IS">Iceland</asp:ListItem>
                        <asp:ListItem Value="IN">India</asp:ListItem>
                        <asp:ListItem Value="ID">Indonesia</asp:ListItem>
                        <asp:ListItem Value="IR">Iran, Islamic Republic of</asp:ListItem>
                                           <asp:ListItem Value="VI">Virgin Islands, U.S.</asp:ListItem>
                        <asp:ListItem Value="WF">Wallis and Futuna</asp:ListItem>
                        <asp:ListItem Value="EH">Western Sahara</asp:ListItem>
                        <asp:ListItem Value="YE">Yemen</asp:ListItem>
                        <asp:ListItem Value="ZM">Zambia</asp:ListItem>
                        <asp:ListItem Value="ZW">Zimbabwe</asp:ListItem>
                    </asp:DropDownList>
                                   </td>
            </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td height="45" align="left">
                    <asp:Button ID="btnContinue" runat="server" Text="View Map" Width="93px" OnClick="btnContinue_Click" />
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <div id="divIframe" runat="server" visible="false">
                        <center>
                            <iframe width="400" height="300" frameborder="0" scrolling="no" marginheight="0"
                                marginwidth="0" id="iframeMap" runat="server" />
                        </center>
                    </div>
                </td>
            </tr>
        </table>
    </div>

In Code Behind Call the MSN API to Get Location by giving the inputs
   protected void btnContinue_Click(object sender, EventArgs e)
        {
            string countrycode = ddlCountry.SelectedValue.ToString();
            string locationName = txtLocationname.Text;
            string postalCode = txtPostalcode.Text;
            string address = txtAddress1.Text;
            string latitude, longitude;
            DataSet dsLat_Langitude = new DataSet();
//Below code request the MSN Empeded MAP API by giving inputs
            string url = string.Format("http://dev.virtualearth.net/REST/v1/Locations?countryRegion={0}&locality={1}&postalCode={2}&addressLine={3}&o=xml&key=Al-zQKFj0ONajQJaPnl6DfR-BJ_X3HqbtsvyaEa63k6XUTLdOGtqa5JKFUqG4UDA", countrycode, locationName, postalCode, address);

            HttpWebRequest request
      = WebRequest.Create(url) as HttpWebRequest;

            // Get response 
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                // Load data into a dataset 

                dsLat_Langitude.ReadXml(response.GetResponseStream());
                // Print dataset information 
            }
            if (dsLat_Langitude.Tables.Count > 0)
            {
                if (dsLat_Langitude.Tables["Point"] != null)
                {
                    latitude = dsLat_Langitude.Tables["Point"].Rows[0]["latitude"].ToString();
                    longitude = dsLat_Langitude.Tables["Point"].Rows[0]["Longitude"].ToString();
                    divIframe.Visible = true;
                    divIframe.Focus();
                    string iframeurl = string.Format("http://dev.virtualearth.net/embeddedMap/v1/ajax/road?zoomLevel=15&center={0}_{1}&pushpins={0}_{1}", latitude, longitude);

                    iframeMap.Attributes["src"] = iframeurl;
                   
                }
                else
                {
                    lblNotification.Text = "Provide Valid Inputs for to see the maps";
                }
            }
            else
            {
                lblNotification.Text = "Provide Valid Inputs for to see the maps";
            }
        }


No comments:

Post a Comment