Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
Hi,
I am using Google Map in My Website. I need to pass the java script input values from C#.
In Database i have set of latitude,longitude values. I need to pass this values to java script.
Below i have mentioned my code:

HTML
<html>
	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
  <title>Google Maps Multiple Markers</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>
</head> 

	<body>
	<div id="map" style="width: 700px; height: 800px;"></div>
	<script id="js" type="text/javascript">
 

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 5,
      center: new google.maps.LatLng(21.7679, 78.8718),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    
	
    var markersarray = [];
	
    function clearoverlays()
    {
	if(markersarray)
	{
	    for(i in markersarray)
	    {
		markersarray[i].setMap(null);
	    }
	}
    }
    
      
    function javascript(city,latitude,longitude,count,DisName)
    {
     var infowindow = new google.maps.InfoWindow();

    var marker;

    
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(latitude,longitude),
        map: map

      });
	      markersarray.push(marker);

      google.maps.event.addListener(marker, 'click', (function(marker) {
        return function() {
        infowindow.setContent("<p>" +""+"Place : "+""+ city +"<br />" +""+"DiseaseName : "+"" +DisName+ "<br/>"+""+"Number of Patients Affected : "+""+ count+ "<br />"+
                  "</p>");
    zoom: 5,
          infowindow.open(map, marker);
        }
      })(marker));
     
    } 

    
  </script>
  
	</body>
</html>



My C# code is :


C#
string[,] data = new string[arraylength, 4];
           
            data = GoogleRegions(disease, Mon); // GoogleRegions function to select the DB Details
           

            int len = data.Length;

                
             
         
            ClientScript.RegisterStartupScript(this.GetType(), "clearoverlays", "clearoverlays");

                for (int i = 0; i < len / 4; i++)
                {
                    string place = data[i, 0].ToUpper();
                    disease = disease.ToUpper();

                    

                   ClientScript.RegisterStartupScript(this.GetType(), "javascript", string.Format("javascript('{1,2,3,4,5}')",place,data[i,1],data[i,2], data[i, 3], disease),false);
}



But i didnt get the Map Marker. How can i pass the values to above javascript function? Anyone help for this?
Posted
Comments
Sreekanth Mothukuru 30-Sep-14 7:44am    
Immediately call an Ajax method located on server soon after your Google map page loads. Return all your pointers with lat/long info to create a marker on map overlay.

am using like this in my project once you go through this you may get something from this
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WatchDoctorLocation.aspx.cs"
    Inherits="Inforaise.eMediTime.Web.ModalWindows.WatchDoctorLocation" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <%-- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
    --%>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var geocoder;
            jQuery.ajax({
                type: "GET",
                dataType: "json",
                url: "http://maps.googleapis.com/maps/api/geocode/json",
                data: { 'address': 'HYDERABAD', 'sensor': false },
                success: function (data) {
                    if (data.results.length) {
                        initialize('<%=docLatitude %>', '<%=docLongitude %>');
                    } else {
                        alert("Invalid Address");
                    }
                }
            });
        });
        function initialize(Latitude, Longitude) {
            var myLatlng = new google.maps.LatLng(Latitude, Longitude);
            geocoder = new google.maps.Geocoder();
            var mapOptions = {
                zoom: 19,
                center: myLatlng
            }
            var map = new google.maps.Map(document.getElementById('googleMap'), mapOptions);
            var marker = new google.maps.Marker({
                position: myLatlng,
                map: map,
                animation: google.maps.Animation.DROP
                //title: 'inforaise technologies'
            });


            google.maps.event.addListener(marker, 'click', function () {
                geocoder.geocode({ 'latLng': myLatlng }, function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        if (results[1]) {
                            map.setZoom(19);
                            marker = new google.maps.Marker({
                                position: myLatlng,
                                map: map
                            });
                            infowindow.setContent('' + '<%=DocLocation %>' + '' + ',' + '<br/>' + results[1].formatted_address);
                            infowindow.open(map, marker);
                        } else {
                            alert('No results found');
                        }
                    } else {
                        alert('Geocoder failed due to: ' + status);
                    }
                });
                //infowindow.open(map, marker);
            });
            var contentString;
            var infowindow = new google.maps.InfoWindow({
                content: contentString
            });
            var mapProp = {
                center: new google.maps.LatLng(Latitude, Longitude),
                zoom: 5,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            google.maps.event.addDomListener(window, 'load', initialize);
        }
    </script>
    <style type="text/css">
        #divGoogleMaps
        {
            height: 350px !important;
            padding: 0px 0px !important;
        }
        #googleMap
        {
            position: fixed !important;
            background-color: none !important;
        }
        .gm-style-iw
        {
            width: 120px !important;
            height: auto !important;
        }
    </style>
</head>
<body>
    <div id="googleMap" style="width: 100%; height: 100%">
    </div>
</body>
</html>




in server side just am assigning the longitude and latitude values


C#
public partial class WatchDoctorLocation : System.Web.UI.Page
   {
       public string docLongitude;
       public string docLatitude;
       public string DocLocation;

       protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               string DoctorLocation = Request.QueryString["DocLocation"];
               string[] altitudes = DoctorLocation.Split(':');
               docLongitude = altitudes[0];
               docLatitude = altitudes[1];
               DocLocation = altitudes[2];

           }

       }
   }
 
Share this answer
 
v4
 
Share this answer
 

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



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