Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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..

XML
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <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>
</asp:Content>


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

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


any help!!!! thanks in advance
Posted

1 solution

Hi Agustus Jackson,

If you are using jQuery, the I would suggest you to try this below one.

JavaScript
// jQuery
$(document).ready( function () {
  location();
}


If not, let me know. I will try to provide you something else to fix this issue.

Regards,
RelicV
 
Share this answer
 
Comments
Codes DeCodes 10-Apr-14 7:38am    
thanks man.. its working.. knew that mozilla doesnot support my code...
RelicV 11-Apr-14 5:35am    
I'm glad it worked for you. Happy coding. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900