Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,


I am trying to display google map in master page.But map was not loading only empty page was displaying.

Please find my code below:
ASP.NET
<asp:Content ID="Content1" ContentPlaceHolderID="Contentplaceholder1" runat="server">

    <script type="text/javascript" src = "https://maps.googleapis.com/maps/api/js?key=AIzaSyC6v5-2uaq_wusHDktM9ILcqIrlPtnZgEk&sensor=false">
</script>
<script type="text/javascript">
    function initialize() {
        alert('here');
        var markers = JSON.parse('<%=ConvertDataTabletoString() %>');
    var mapOptions = {
        center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
        zoom: 5,
        mapTypeId: google.maps.MapTypeId.ROADMAP
        //  marker:true
    };
    var infoWindow = new google.maps.InfoWindow();
    var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
    for (i = 0; i < markers.length; i++) {
        var data = markers[i]
        var myLatlng = new google.maps.LatLng(data.lat, data.lng);
        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: data.title
        });
        (function (marker, data) {

            // Attaching a click event to the current marker
            google.maps.event.addListener(marker, "click", function (e) {
                infoWindow.setContent(data.description);
                infoWindow.open(map, marker);
            });
        })(marker, data);
    }
}
</script>
<section id="content">
<body onload="initialize()" onunload="GUnload()">
<div id="map_canvas" style="width: 800px; height: 600px"></div>
</body>
    </section>

</asp:Content>



In Sample.aspx.cs code:

C#
public string ConvertDataTabletoString()
       {
           DataTable dt = new DataTable();
           using (SqlConnection con = new SqlConnection("Server=acysemf4vo.database.windows.net;Database=test;User ID=sa;Password=test;Trusted_Connection=False;Encrypt=True;Connection Timeout=60;"))
           {
               using (SqlCommand cmd = new SqlCommand("SELECT lat=LATITUDE,lng=LONGITUDE,info.CUSTCODE,description=CUSTNAME FROM table2 INNER JOIN info ON table2.CUSTCODE=info.CUSTCODE and CSTATUS = 0", con))
               {
                   con.Open();
                   SqlDataAdapter da = new SqlDataAdapter(cmd);
                   da.Fill(dt);
                   System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                   List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
                   Dictionary<string, object> row;
                   foreach (DataRow dr in dt.Rows)
                   {
                       row = new Dictionary<string, object>();
                       foreach (DataColumn col in dt.Columns)
                       {
                           row.Add(col.ColumnName, dr[col]);
                       }
                       rows.Add(row);
                   }
                   return serializer.Serialize(rows);
               }
           }
       }


Any one can help me on this issues.

Thanks
Krishna
Posted
Updated 17-Nov-14 18:33pm
v2
Comments
vbmike 18-Nov-14 13:37pm    
The only way I learned google maps display was to just display a simple map, then start adding things as I went along. Google maps is touchy in my opinion and any little mistake and the whole thing bombs. I also have their maps api open in another window while I am working. In other words you will have to step into this code one little bit at a time to see what and where it is failing. The usual suspect of not having a div sized appropriately etc seems to be ok but as far as the other stuff it is hard to troubleshoot for you. At least you can be working on it while waitng for someone to answer your question fully. Check all your code semantics also, very important.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900