好吧,我正在尝试查找给定地址的纬度和经度。地址来自textarea然后当我点击一个按钮时,它会将输入地址的纬度和经度作为警报给我。我是使用以下脚本:
<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false&key=AIzaSyDVk87DSFyZlxmYAM8NPNr8sZPN60FYLNA"></script>
<script type="text/javascript">
function calculateCoordinates() {
var lblStreetAddress = document.getElementById('<%= lblStreetAddress.ClientID%>');
var lblLandmark = document.getElementById('<%= lblLandmark.ClientID%>');
var lblCity = document.getElementById('<%= lblCity.ClientID%>');
var lblState = document.getElementById('<%= lblState.ClientID%>');
var lblZipCode = document.getElementById('<%= lblZipCode.ClientID%>');
var txtLatitude = document.getElementById('<%= txtLatitude.ClientID%>');
var txtLongitude = document.getElementById('<%= txtLongitude.ClientID%>');
var address = lblStreetAddress.value + ', ';
address += lblLandmark.value + ', ';
address += lblCity.value + ', ';
address += lblZipCode.value + ', ';
address += lblState.value;
var geocoder;
geocoder = new google.maps.Geocoder();
geocoder.geocode({ address: address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var location = results[0].geometry.location;
txtLatitude.value = location.lat();
txtLongitude.value = location.lng();
}
else
alert(searchString + ' - not found');
});
}
</script>
<asp:TextBox id="txtLatitude" runat="server" Width="100px"></asp:TextBox>
<asp:TextBox id="txtLongitude" runat="server" Width="100px"></asp:TextBox>
这绝对没问题,但我真正想要的是 1.我想使用Label而不是textarea,因为地址应该来自数据库。 2.我想将纬度和经度值传递给几个标签,以便我可以将它们存储回数据库中。
有可能吗?如果没有,有没有其他方法不复杂?
答案 0 :(得分:0)
以下是您的操作方法:
<强> http://jsfiddle.net/y7pdb5b2/ 强>
// Here we set the address
var address = "oxford street london";
// We call get location, the callback will return the address;
// You need to add some error checking to ensure you get something back;
GetLocation(address, function (coord) {
alert(coord);
var lat = coord.lat,
lng = coord.lng;
});
function GetLocation(address, callback) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': address
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
callback(results[0].geometry.location);
} else {
callback();
}
})
};