如何在不显示地图的情况下从地址获取经度和纬度

时间:2010-02-01 10:34:30

标签: asp.net google-maps latitude-longitude

我想从我的一个asp.net应用程序中获取地址的纬度和经度,我知道可以使用谷歌api,但我想这样做而不显示谷歌地图。我怎样才能做到这一点?   我还希望获得两对纬度和经度之间的距离。请指导我。

由于 的Pankaj

3 个答案:

答案 0 :(得分:1)

您需要在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)

答案 1 :(得分:1)

或试试这个..

注意:您需要将“YabbaDabbaDoo”替换为您自己的Google Maps API密钥。

在您的网页正文中,我们可以在表单上放置一些文本框,以便输入我们想要查找坐标的地址详细信息(以及执行坐标计算的按钮): / p>

地址第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();
        }
    });

}

答案 2 :(得分:0)

试试这段代码........... 在PAge加载

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>