javascript - how to get my current location on my browser -
hey guys i'm trying find current location on browser , html index file :
<!doctype html> <html> <head> <meta charset="utf-8"> <title>geocoding page</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script> function getlocation() { if (navigator.geolocation) { navigator.geolocation.getcurrentposition(saveposition, positionerror, {timeout:10000}); } else { //geolocation not supported browser } } // handle error here function positionerror(error) { var errorcode = error.code; var message = error.message; alert(message); } function saveposition(position) { $.post("geocoordinates.php", {lat: position.coords.latitude, lng: position.coords.longitude}); } </script> </head> <body> <button onclick="getlocation();">get location</button> </body> </html>
and that's geocoordinates.php file :
<?php if(isset($_post['lat'], $_post['lng'])) { $lat = $_post['lat']; $lng = $_post['lng']; $url = sprintf("https://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s", $lat, $lng); $content = file_get_contents($url); // json content $metadata = json_decode($content, true); //json decoder if(count($metadata['results']) > 0) { // format example @ url // https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452 $result = $metadata['results'][0]; // save in db further use echo $result['formatted_address']; } else { // no results returned } } ?>
i found answer previous question when run on browser gives me error : user denied goelocation can 1 ?????
<p><button class="w3-btn w3-blue" onclick="getlocation()">try it</button></p> <div id="mapholder"></div> <script src="http://maps.google.com/maps/api/js?sensor=false"></script> <script> var x=document.getelementbyid("demo"); function getlocation() { if (navigator.geolocation) { navigator.geolocation.getcurrentposition(showposition,showerror); } else{x.innerhtml="geolocation not supported browser.";} } function showposition(position) { lat=position.coords.latitude; lon=position.coords.longitude; latlon=new google.maps.latlng(lat, lon) mapholder=document.getelementbyid('mapholder') mapholder.style.height='250px'; mapholder.style.width='100%'; var myoptions={ center:latlon,zoom:14, maptypeid:google.maps.maptypeid.roadmap, maptypecontrol:false, navigationcontroloptions:{style:google.maps.navigationcontrolstyle.small} }; var map=new google.maps.map(document.getelementbyid("mapholder"),myoptions); var marker=new google.maps.marker({position:latlon,map:map,title:"you here!"}); } function showerror(error) { switch(error.code) { case error.permission_denied: x.innerhtml="user denied request geolocation." break; case error.position_unavailable: x.innerhtml="location information unavailable." break; case error.timeout: x.innerhtml="the request user location timed out." break; case error.unknown_error: x.innerhtml="an unknown error occurred." break; } } </script>
please see here more details:-http://www.w3schools.com/html/html5_geolocation.asp here javascript gives latitude , longitude , checks exact location of you.
Comments
Post a Comment