开发者

How to get the longitude and lattitude from an address without showing map

开发者 https://www.devze.com 2022-12-19 06:17 出处:网络
I want to get latitude and longitude from address in one of my asp.net application, i know it s possible using google apis but i want to do it without showing google map. How can i do that?

I want to get latitude and longitude from address in one of my asp.net application, i know it s possible using google apis but i want to do it without showing google map. How can i do that? I also want to get distance between two pairs of latitude and longitude. Please guide me in that regards also.

开发者_如何学C

Thanks Pankaj


You need to do everything in an onload function, as described at http://code.google.com/apis/maps/documentation/introduction.html#Loading_the_Maps_API . The basic idea (using version 2 of the API) is :

var coder = new GClientGeocoder();
coder.getLatLng(
                "Addr to Geocode",
                function(point) {
                    if (point) {
                        // Do something with GLatLng point                
                    }
                }
            );

To get the distance (in meters), do:

point1.distanceFrom(point2)


Or try this..

Note: You will want to replace the “YabbaDabbaDoo” with your own Google Maps API Key.

In the body of your web page, lets place some textboxes on the form so that we can enter the address details that we want to find the co-ordinates for (and a button to perform the co-ordinates calculation):

Address Line 1: Address Line 2: Town: Postcode: Country: Latitude: Longitude:

Now all that is left to do is to create the javascript function that will perform the calculation. So here it is:

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

}


try this code........... In PAge load

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetPhysiciansForMap("");    
        }

        ((HtmlControl)Master.FindControl("myBody")).Attributes.Add("onload", "GenerateMap()");

    }

And in .aspx page

<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>
0

精彩评论

暂无评论...
验证码 换一张
取 消