此代码仅适用于脚本块中活动的 2 个警报,如果您注释第一个或第二个警报,则地理编码功能不起作用。这是一个简单的 asp.net 页面,它尝试从地址获取纬度和经度,在本例中是硬编码:
<%@ 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>