Click here to Skip to main content
15,885,899 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
XML
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
    $(document).ready(function () {
        debugger;
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        }
        else {
            alert("Geolocation is not supported by this browser.");
        }


        function showPosition(position) {
            debugger;
            var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            var geocoder = geocoder = new google.maps.Geocoder();
            geocoder.geocode({ 'latLng': latlng }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (results[1]) {
                        document.getElementById("txtaddress").value = results[1].formatted_address;
                    }
                }
            });
        }
    });
</script>


I have checked javascript code showPosition function is not getting called that's why i am unable to get location.
Posted
Updated 22-Sep-15 0:52am
v3
Comments
BulletVictim 22-Sep-15 7:39am    
try this:
var marker;
var map;
geocoder.geocode({ 'address': latlng }, function (results, status) {
marker.setMap(null);
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});


Also noticed that there are now location settings sent to the methods.
Moykn 22-Sep-15 7:49am    
Have you included jQuery in your page? Do you plan to use jQuery anyway?
xszaboj 22-Sep-15 8:45am    
and where exactly are you calling showPosition?
Moykn 22-Sep-15 9:24am    
He is calling as a callback of navigator.geolocation.getCurrentPosition(showPosition);

XML
<!DOCTYPE html>
<html>
  <head>

    <script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js"></script>
    <script>
        jQuery(window).ready(function(){
            jQuery("#btnInit").click(initiate_geolocation);
        });

        function initiate_geolocation() {
            navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);
        }

        function handle_errors(error)
        {
            switch(error.code)
            {
                case error.PERMISSION_DENIED: alert("user did not share geolocation data");
                break;

                case error.POSITION_UNAVAILABLE: alert("could not detect current position");
                break;

                case error.TIMEOUT: alert("retrieving position timed out");
                break;

                default: alert("unknown error");
                break;
            }
        }

        function handle_geolocation_query(position){
            alert('Lat: ' + position.coords.latitude +
                  ' Lon: ' + position.coords.longitude);
        }
    </script>
  </head>
  <body>
    <div>
      <button id="btnInit" >Find my location</button>
    </div>
  </body>
</html>
 
Share this answer
 
Hi,

have you tried this solution?
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_nav_geolocation[^]

Two problems that I had with geolocation, don't know if that is you case:

1. If you don't allow geolocation than in future it won't work for the same page (don't remember which browser it was... it was long time ago)

2. geolocation doesn't worked for me if you open html page on disc as a file in a browser for example c:/mypage.html

hope it will help
 
Share this answer
 
XML
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
    $(document).ready(function () {
        debugger;
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        }
        else {
            alert("Geolocation is not supported by this browser.");
        }


        function showPosition(position) {
            debugger;
            var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            var geocoder = geocoder = new google.maps.Geocoder();
            geocoder.geocode({ 'latLng': latlng }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (results[1]) {
                        document.getElementById("txtaddress").value = results[1].formatted_address;
                    }
                }
            });
        }
    });
</script>
 
Share this answer
 

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