javascript - How to handle user approval in browser geolocation with Gmap? -


i'm building html5 geolocation function along gmaps.

i'm having small issue when waiting user approval geolocated: user asked browser wether or not wants geolocated. when clicks yes, geolocated gmap thing isn't triggered , map not displayed on screen. then, if refresh it, working fine. particulary happen on mobile phone.

i give piece of code when handling geolocation thing:

    <script>     // <!-- google maps & position -->      var radius = 1;     var map;     var position;      jquery(window).ready(function(){          //if click on find me lakes         jquery("#btninit").click(initiate_geolocation);         initiate_geolocation();          $('#radius').change(function(v){             handle_geolocation_query(position);         });     });      function initiate_geolocation() {          if (navigator.geolocation)           {               var script = document.createelement("script");             script.type = "text/javascript";             script.src = "https://maps.googleapis.com/maps/api/js?key=aizasybbfjjvh0jl1x9b7xfdcpuv7nhd1hlfsks&sensor=true&callback=initialize";             document.body.appendchild(script);             navigator.geolocation.getcurrentposition(append_google_map, handle_errors);         }           else           {               yqlgeo.get('visitor', normalize_yql_response);         }       }      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 timedout");               break;               default: alert("unknown error");               break;           }       }        function append_google_map(pos){           position = pos;          var mapoptions = {             zoom: 15,             center: new google.maps.latlng(position.coords.latitude, position.coords.longitude),             streetviewcontrol: false,             maptypeid : google.maps.maptypeid.roadmap         }          if (typeof map == 'undefined') { //we make sure map object hasn't been toogled page              map = new google.maps.map(document.getelementbyid("gmap"), mapoptions);              //$('#gmap').slidetoggle('slow');              setmarkerposition(position.coords.latitude, position.coords.longitude);          }          handle_geolocation_query();     } 

as can see here, i'm initiating geolocation stuff in initiate function error callback , success callback. that's when pop appears on user's screen ask if approves geolocation. if clicks yes, seems part of code not triggered (or redirected handle_errors dunno) , therefore, maps doesn't appear on screen (it handled bit further in code in append_google_map.

could me figure why happening , how handle please ?

many thanks

you can't, it's security feature. code must wait user approve or decline whether want share location or not.

your code must able react upon both responses user. if chose not share code, try fallback coordinates of city, or display message.

it ok have message on page telling them share location, provided explain them why sharing location you.


Comments