0

Well,I am trying to find the latitude and longitude of a given address.The address comes from a textarea and then when i click a button,it gives me the latitude and longitude of the entered address as an alert.I am using the below script:

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

This works absolutely fine but what i really want is 1.I want to use a Label instead of a textarea because the address should come from database. 2.I want to pass the latitude and longitude value to couple of labels so that i can store them back in the database.

Is it possible? If not,are there any alternative ways which arent so complex?

Mash
  • 167
  • 1
  • 5
  • 24
  • does http://stackoverflow.com/questions/19232822/how-to-set-value-of-a-input-hidden-field-through-javascript cover what you need? – jball Aug 15 '14 at 16:14

1 Answers1

0

Here is how you do it:

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

    })
};
MartinWebb
  • 1,748
  • 1
  • 11
  • 12
  • I am a newbie when it comes to javascript so the above code does not make much sense to me.How do i pass the address to the var address because it keeps changing depending upon the database.its not a specific address. – Mash Aug 15 '14 at 16:28
  • And i still dont know how to pass the latitude and longitude values to a label,its still using txtAddress which i dont want. – Mash Aug 15 '14 at 16:30
  • Was small error in the code, you pass in your address to the function as label (var) address – MartinWebb Aug 15 '14 at 16:39
  • When you say to a label, do you mean a label in the HTML dom or are you referring to label as a variable. – MartinWebb Aug 15 '14 at 16:50
  • I have edited the entire code.Even this doesnt work.Any help would be appreciated. – Mash Aug 15 '14 at 17:31
  • I've updated the code, and also added a fiddle here http://jsfiddle.net/y7pdb5b2/ If you run the code it will pop up the alert, the code should explain how it works – MartinWebb Aug 15 '14 at 18:46