geolocation - Not able to get location via google maps API -
i trying location of user via geolocation
api, working fine till yesterday today getting "unable location" every attempt
script used api
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> <script type="text/javascript"> var geocoder; if (navigator.geolocation) { navigator.geolocation.getcurrentposition(successfunction, errorfunction); } //get latitude , longitude; function successfunction(position) { var lat = position.coords.latitude; var lng = position.coords.longitude; //var num = parsefloat(lat); //var str = num.tofixed(10); //str = str.substring(0, str.length-7); $('#yourlat').val(lat);$('#yourlat1').val(lat); //var num1 = parsefloat(lng); //var str1 = num1.tofixed(10); //str1 = str1.substring(0, str1.length-7); $('#yourlong').val(lng);$('#yourlong1').val(lng); codelatlng(lat, lng) } function errorfunction(){ alert("geocoder failed"); } function initialize() { geocoder = new google.maps.geocoder(); } function codelatlng(lat, lng) { var latlng = new google.maps.latlng(lat, lng); var addressget =''; geocoder.geocode({'latlng': latlng}, function(results, status) { if (status == google.maps.geocoderstatus.ok) { console.log(results) if (results[1]) { //formatted address //alert(results[0].formatted_address) addressget = results[0].formatted_address; $('#yourcity').html(addressget); $('#yourcitys').val(addressget); //find country name (var i=0; i<results[0].address_components.length; i++) { (var b=0;b<results[0].address_components[i].types.length;b++) { //there different types might hold city admin_area_lvl_1 in come cases looking sublocality type more appropriate if (results[0].address_components[i].types[b] == "administrative_area_level_1") { //this object looking city= results[0].address_components[i]; break; } } } //city data var stateget =''; stateget = city.short_name + " " + city.long_name; $('#yourstate').html(stateget); } else { alert("we not able location.."); } } else { alert("geocoder failed due to: " + status); } }); } </script>
i calling function on bodyload
<body onload="initialize()">
everytime allow location gives error we not able location..
i suggest checking status variable indicating. valuesfor class google.maps.geocoderstatus can found api documentation
Comments
Post a Comment