我想从我的一个 asp.net 应用程序中的地址获取纬度和经度,我知道可以使用 google apis,但我想在不显示 google 地图的情况下做到这一点。我怎样才能做到这一点?我也想得到两对纬度和经度之间的距离。在这方面也请指导我。
谢谢潘卡伊
我想从我的一个 asp.net 应用程序中的地址获取纬度和经度,我知道可以使用 google apis,但我想在不显示 google 地图的情况下做到这一点。我怎样才能做到这一点?我也想得到两对纬度和经度之间的距离。在这方面也请指导我。
谢谢潘卡伊
您需要在 onload 函数中执行所有操作,如http://code.google.com/apis/maps/documentation/introduction.html#Loading_the_Maps_API中所述。基本思想(使用 API 的第 2 版)是:
var coder = new GClientGeocoder();
coder.getLatLng(
"Addr to Geocode",
function(point) {
if (point) {
// Do something with GLatLng point
}
}
);
要获得距离(以米为单位),请执行以下操作:
point1.distanceFrom(point2)
或者试试这个..
注意:您需要将“YabbaDabbaDoo”替换为您自己的 Google Maps API 密钥。
在您的网页正文中,让我们在表单上放置一些文本框,以便我们可以输入我们想要查找坐标的地址详细信息(以及执行坐标计算的按钮):
地址行 1: 地址行 2: 城镇: 邮编: 国家: 纬度: 经度:
现在剩下要做的就是创建将执行计算的 javascript 函数。所以这里是:
function calculateCoordinates() {
var txtAddress1 = document.getElementById('<%= txtAddress1.ClientID%>');
var txtAddress2 = document.getElementById('<%= txtAddress2.ClientID%>');
var txtTown = document.getElementById('<%= txtTown.ClientID%>');
var txtPostcode = document.getElementById('<%= txtPostcode.ClientID%>');
var txtCountry = document.getElementById('<%= txtCountry.ClientID%>');
var txtLatitude = document.getElementById('<%= txtLatitude.ClientID%>');
var txtLongitude = document.getElementById('<%= txtLongitude.ClientID%>');
var address = txtAddress1.value + ', ';
address += txtAddress2.value + ', ';
address += txtTown.value + ', ';
address += txtPostcode.value + ', ';
address += txtCountry.value;
var geocoder;
geocoder = new GClientGeocoder();
geocoder.getLatLng(address, function(latlng) {
if (!latlng) {
alert(address + ' not found');
} else {
txtLatitude.value = latlng.lat();
txtLongitude.value = latlng.lng();
}
});
}
试试这个代码............在页面加载
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetPhysiciansForMap("");
}
((HtmlControl)Master.FindControl("myBody")).Attributes.Add("onload", "GenerateMap()");
}
并在 .aspx 页面中
<script type="text/javascript">
var geocoder;
var map = null;
var lat, lng;
var infowindow = new google.maps.InfoWindow(
{
size: new google.maps.Size(150,50)
});
function GenerateMap() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(40.730885,-73.997383);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});
getHiddenValues();
for (var i = 0; i < lat.length-1; i++) {
var point = new google.maps.LatLng(lat[i], lng[i]);
var marker = createMarker(point,'<div style="width:240px">Address: <br /> City: <br /> State: <br /> Lat n Lng: '+lat[i]+ ', ' +lng[i]+'<\/div>')
}
}
function createMarker(latlng, html) {
var contentString = html;
var marker = new google.maps.Marker({
position: latlng,
map: map,
zIndex: Math.round(latlng.lat()*-100000)<<5
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(contentString);
infowindow.open(map,marker);
});
}
function getHiddenValues()
{
lat = document.getElementById("ctl00_ContentPlaceHolder1_hfLat").value.split(',');
lng = document.getElementById("ctl00_ContentPlaceHolder1_hfLng").value.split(',');
}
</script>