0

this is my code to get latitude and longitude and return my address... code for getting latitude and longitude and returning address is using Google map Geo-location and reverse Geo-coding which works fine.. I have a problem in matching up java script.. my code is..

<p onload="location()">                      
   <script src="jquery.min.js"></script>
    <script type="text/javascript">         
       function location(){
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(c);
                return false;
            } else {
                alert("Geo Location is not supported on your current browser!");
            }
        }
        var c = function (pos) {
            var latitude = pos.coords.latitude;
            var longitude = pos.coords.longitude;

            Convert_LatLng_To_Address(latitude, longitude, AlertAddress);
            function AlertAddress() {
                alert("Current address is " + address["formatted_address"] + " lat: " + latitude + " longitude: " + longitude);  
                document.getElementById('txtLocationDetail').innerText = address["formatted_address"];
            }
        }            
        var address = new Array();
        function Convert_LatLng_To_Address(lat, lng, callback) {
            var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," + lng + "&sensor=false";
            jQuery.getJSON(url, function (json) {
                Create_Address(json, callback);
            });
        }
        function Create_Address(json, callback) {
            if (!check_status(json)) // If the json file's status is not ok, then return
                return 0;
            address['country'] = google_getCountry(json);
            address['province'] = google_getProvince(json);
            address['city'] = google_getCity(json);
            address['street'] = google_getStreet(json);
            address['postal_code'] = google_getPostalCode(json);
            address['country_code'] = google_getCountryCode(json);
            address['formatted_address'] = google_getAddress(json);
            callback();
        }
        function check_status(json) {
            if (json["status"] == "OK") return true;
            return false;
        }
        function google_getCountry(json) {
            return Find_Long_Name_Given_Type("country", json["results"][0]["address_components"], false);
        }
        function google_getProvince(json) {
            return Find_Long_Name_Given_Type("administrative_area_level_1", json["results"][0]["address_components"], true);
        }
        function google_getCity(json) {
            return Find_Long_Name_Given_Type("locality", json["results"][0]["address_components"], false);
        }
        function google_getStreet(json) {
            return Find_Long_Name_Given_Type("street_number", json["results"][0]["address_components"], false) + ' ' + Find_Long_Name_Given_Type("route", json["results"][0]["address_components"], false);
        }
        function google_getPostalCode(json) {
            return Find_Long_Name_Given_Type("postal_code", json["results"][0]["address_components"], false);
        }
        function google_getCountryCode(json) {
            return Find_Long_Name_Given_Type("country", json["results"][0]["address_components"], true);
        }
        function google_getAddress(json) {
            return json["results"][0]["formatted_address"];
        }

        function Find_Long_Name_Given_Type(t, a, short_name) {
            var key;
            for (key in a) {
                if ((a[key]["types"]).indexOf(t) != -1) {
                    if (short_name)
                        return a[key]["short_name"];
                    return a[key]["long_name"];
                }
            }
        }
    </script>        
    <asp:TextBox ID="txtLocationDetail" runat="server"></asp:TextBox>
</p>

what i wanted is as page loads, the JavaScript function is executed to return my current address and address returned is to be shown in text box.. however, on as page loads java script function is not called.. i had tried this code in server side but none worked..

protected void Page_Load(object sender, EventArgs e) { ClientScript.RegisterStartupScript(this.GetType(), ClientID, "location();", true); }

any help!!!! thanks in advance

Agustus Codes
  • 57
  • 1
  • 10

1 Answers1

1

<p> does not support onload. w3schools

Take a look at this: forums.asp.net. What you should do is add 'onload' attribute to <body> in PageLoad(), like this:

protected void Page_Load(object sender, EventArgs e)
{
HtmlGenericControl body = this.Master.FindControl("body") as HtmlGenericControl;
body.Attributes.Add("onLoad", "alert('Hello');");
}

and ensure that <body> has an id (in master page):

<body id="body" runat="server">
borkovski
  • 938
  • 8
  • 12
  • thanks for the code.. but in doing so, my current address is not returned.. guess there is a problem in javascript code. can you plz help me!!! or tell me how can i get formatted_address address in textbox that is accessible in server side as well... – Agustus Codes Apr 08 '14 at 09:16
  • for doing so, i had added following code.
    idk why it is not showing..
    – Agustus Codes Apr 08 '14 at 09:50
  • innerText is not W3C compliant - try the following: http://stackoverflow.com/questions/1359469/innertext-works-in-ie-but-not-in-firefox – borkovski Apr 08 '14 at 12:33
  • thanks man.. i used jquery instead of javascript; for javascript was not supported by my browser... now its working.. – Agustus Codes Apr 09 '14 at 07:33