geocoder.geocode仅在特定情况下有效

问题描述:

如果您评论第一个或第二个警报,地理编码功能不起作用,则此代码仅适用于脚本块中处于活动状态的2个警报。 这是一个简单的asp.net页面,试图从一个地址获取经纬度,在这种情况下,硬编码:geocoder.geocode仅在特定情况下有效

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="Test" %> 

<!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 runat="server"> 
    <title></title> 
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script> 
    <script type="text/javascript" src="/Global/js/jquery-1.8.3.min.js"></script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <div style="width: 100%"> 
      <div style="width: 50%; clear: left; float: left; text-align: right;"> 
       Latitudine: 
      </div> 
      <div style="width: 50%; float: left; text-align: left;"> 
       <asp:TextBox ID="TxbLatitudine" runat="server" Columns="20" /> 
       <asp:Button ID="BtnFind" runat="server" Text="Cerca cordinate" OnClientClick="javascript: findLocation();" /> 
      </div> 
     </div> 
     <div style="width: 100%"> 
      <div style="width: 50%; clear: left; float: left; text-align: right;"> 
       Longitudine: 
      </div> 
      <div style="width: 30%; float: left; text-align: left;"> 
       <asp:TextBox ID="TxbLongitudine" runat="server" Columns="20" ></asp:TextBox> 
      </div> 
     </div> 
    </form> 
    <script type="text/javascript"> 
     function findLocation() { 
      var geocoder = new google.maps.Geocoder(); 

      alert("before"); //if you remove this doesn't work 
      geocoder.geocode({ 'address': "Cagliari, Italy" }, function (results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 
        $('#<%= TxbLatitudine.ClientID %>').val("a"); 
       } else { 
        $('#<%= TxbLongitudine.ClientID %>').val("not OK"); 
       } 
      }); 
      alert("after"); //if you remove this doesn't work 
     } 
    </script> 
</body> 
</html> 

我更换了ASP:按钮与HTML输入按钮:

<input id="Button1" type="button" value="Find..." onclick="javascript: findLocation();" /> 

第一个导致页面回发,干扰客户端脚本块功能。