开发者

ASP google map add marker on button click

开发者 https://www.devze.com 2023-03-21 04:49 出处:网络
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 t

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(parseFlo开发者_如何学运维at(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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消