简体   繁体   English

ASP google map 在按钮单击上添加标记

[英]ASP google map add marker on button click

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).我有一个 asp gridview,在该按钮上单击按钮字段列我需要通过获取单击的行值(经度和纬度)在 Google map 上添加一个点。 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 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.我曾尝试使用隐藏字段,但新的 google.maps.LatLng() 采用双精度值,因此无法正常工作。 Here is my javascript function,这是我的 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 代码(这里我没有 gridview 只是一个按钮,我希望在 gridview RowCommand 事件上分配隐藏字段值。),

<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.请有人帮我解决这个问题。当我在 javascript 中硬编码经度和纬度值时,此解决方案有效。 But I want to pass them when user clicks on the gridview row button.但是当用户单击 gridview 行按钮时,我想传递它们。

You are creating a new google.maps.Map on each click - probably not what you want to do.您在每次点击时创建一个新的 google.maps.Map - 可能不是您想要做的。 Create you map elsewhere, as the page loads, and use the 'mao' variable you assign there to add your markers.在页面加载时在别处创建 map,并使用您在此处分配的“mao”变量添加标记。

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,我在更新面板中添加了一个按钮,然后我在该按钮上调用了我的 javascript function 点击这里是按钮点击代码,

        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. addDoctorLocation() 是我的 javascript function 需要很长时间,latt 并在我的 map 上添加一个点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM