0

I have a asp gridview with button field column on that button click I need to add a point on Google map by taking clicked row values(longitude and latitude). I have written a javascript function for this and I have called that function on OnClientClick but the problem is I dont know how to pass long and latt to that function. I have tried to use hiddenfields but new google.maps.LatLng() takes double values so it's not working. Here is my javascript function,

function addDoctorLocation() 
{
      var lat = document.getElementById('<%= hfLong.ClientID %>').value;
      var longt = document.getElementById('<%= hfLong.ClientID %>').value;
      var myLatlng = new google.maps.LatLng(parseFloat(document.getElementById('<%= hfLong.ClientID %>').value), parseFloat(document.getElementById('<%= hfLong.ClientID %>').value));
      var myOptions = {
        zoom: 10,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
     var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

     var marker = new google.maps.Marker({
          position: myLatlng,
          title:"Hello World!"
      });

      // To add the marker to the map, call setMap();
      marker.setMap(map); 
}

and my asp code(here I am not having gridview just a one button I am hoping to assign hidden field values on gridview RowCommand event.),

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Button ID="Button1" runat="server" Text="Button" 
            OnClientClick="addDoctorLocation()" onclick="Button1_Click" />
    </ContentTemplate>
</asp:UpdatePanel>

Please someone help me to solve this problem.This solution works when I hard code the long and latitude values in the javascript. But I want to pass them when user clicks on the gridview row button.

3
  • This probably isn't related to your problem, but I noticed you are using the value of hfLong for both lat and long.
    – Ray
    Jul 16, 2011 at 12:10
  • Yes Ray I am assigning values for that two hidden fields on page load it's just for testing purpose and here I am just added only one button not a gridview I hope to assign those values in the gridview RowCommand event. Jul 16, 2011 at 12:16
  • You are using the value of only one of your hidden fields for both lat and long. This will not put a point on the map where you think it should go.
    – Ray
    Jul 16, 2011 at 12:57

2 Answers 2

0

You are creating a new google.maps.Map on each click - probably not what you want to do. Create you map elsewhere, as the page loads, and use the 'mao' variable you assign there to add your markers.

if this doesn't help, please post more info about what you are seeing - error messages, etc.

4
  • Sorry Ray I am new to google map so can u pls explain me what do u mean by mao variable. Jul 16, 2011 at 12:23
  • The line which starts 'var map = ...' should not execute within your click handler. It should execute only once, when the page loads.
    – Ray
    Jul 16, 2011 at 12:56
  • I have declared var map globally and on the page load I create Map obj and assigned to that variable. But after that nothings happened when I clicks on button. Jul 16, 2011 at 13:04
  • I suggest you put some 'alert()'s in your click handler code to be sure it is getting called, and then to be sure the values you are using are actually what you think they are.
    – Ray
    Jul 16, 2011 at 14:18
0

thanks everyone I found a solution. I have added a button inside a update panel and then I have called my javascript function on that button click here is the button click code,

        string lat = "-25.363882";
        string longt = "131.044922";

        ScriptManager.RegisterStartupScript(this,this.GetType(),
           "addDoctorLocation",
           "addDoctorLocation(" + lat + "," + longt + ")", true);

addDoctorLocation() is my javascript function that takes long,latt and add a poin on my map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.