asp.netアプリケーションの1つで住所から緯度と経度を取得したいのですが、Google APIを使用して可能であることはわかっていますが、Googleマップを表示せずに実行したいと考えています。どうやってやるの?また、緯度と経度の 2 つのペア間の距離も取得したいと考えています。その辺もご指導ください。
ありがとうございます
asp.netアプリケーションの1つで住所から緯度と経度を取得したいのですが、Google APIを使用して可能であることはわかっていますが、Googleマップを表示せずに実行したいと考えています。どうやってやるの?また、緯度と経度の 2 つのペア間の距離も取得したいと考えています。その辺もご指導ください。
ありがとうございます
またはこれを試してください..
注: 「YabbaDabbaDoo」を独自の Google Maps API キーに置き換える必要があります。
Web ページの本文で、フォームにいくつかのテキストボックスを配置して、座標を見つけたい住所の詳細を入力できるようにします (および座標計算を実行するボタン)。
住所行 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();
}
});
}
http://code.google.com/apis/maps/documentation/introduction.html#Loading_the_Maps_APIで説明されているように、onload 関数ですべてを行う必要があります。基本的な考え方 (API のバージョン 2 を使用) は次のとおりです。
var coder = new GClientGeocoder();
coder.getLatLng(
"Addr to Geocode",
function(point) {
if (point) {
// Do something with GLatLng point
}
}
);
距離 (メートル単位) を取得するには、次のようにします。
point1.distanceFrom(point2)
このコードを試してください...........ページロードで
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>