如何在我的浏览器上获取当前位置

如何在我的浏览器上获取当前位置

问题描述:

hey guys i'm trying to find my current location on my browser , this is my 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 is not supported by this browser
      }
  }

  // handle the 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 My Location</button>
</body>
</html>

and that's the 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); // get json content

    $metadata = json_decode($content, true); //json decoder

    if(count($metadata['results']) > 0) {
        // for format example look at url
        // https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452
        $result = $metadata['results'][0];

        // save it in db for further use
        echo $result['formatted_address'];

    }
    else {
        // no results returned
    }
}

?>

i found this as an answer for a previous question but when i run it on browser it gives me an error as : user denied goelocation so can any one help ?????

嘿伙计们我正试图在我的浏览器上找到我当前的位置,这是我的html索引文件: p>

 &lt;!DOCTYPE html&gt; 
&lt; html&gt; 
&lt; head&gt; 
&lt; meta charset =“utf-8”&gt; 
&lt; title&gt; Geocoding Page&lt;  ; / title&gt; 
&lt; script src =“https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js”&gt;&lt; / script&gt; 
&lt; script&gt  ; 
函数getLocation(){
 if(navigator.geolocation){
 navigator.geolocation.getCurrentPosition(savePosition,positionError,{timeout:10000}); 
} else {
 //不支持地理定位 这个浏览器
} 
} 
 
 //在这里处理错误
函数positionError(错误){
 var errorCode = error.code; 
 var message = error.message; 
 
 alert( 消息); 
} 
 
函数savePosition(position){
 $ .post(“geocoordinates.php”,{lat:position.coords.latitude,lng:position.coords.longitude}); 
}  
&lt; / script  &gt; 
&lt; / head&gt; 
&lt; body&gt; 
&lt; button onclick =“getLocation();”&gt;获取我的位置&lt; / button&gt; 
&lt; / body&gt; 
&lt; / html&gt; 
   pre> 
 
 

这是geocoordinates.php文件: p>

 &lt;?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内容
 
 $ metadata = json_decode($ content,true);  // json decoder 
 
 if(count($ metadata ['results'])&gt; 0){
 //格式示例请查看url 
 // https://maps.googleapis.com/maps  /api/geocode/json?latlng=40.714224,-73.961452
 $ result = $ metadata ['results'] [0]; 
 
 //将其保存在db中以供进一步使用
 echo $ result ['formatted_address  ']; 
 
} 
其他{
 //没有结果返回
} 
} 
 
?&gt; 
  code>  pre> 
 
 

i 发现这是前一个问题的答案但是当我在浏览器上运行时它给我一个错误:用户拒绝goelocation所以可以任何人帮助????? p> div>

<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 is not supported by this 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 are here!"});
  }

function showError(error)
  {
  switch(error.code) 
    {
    case error.PERMISSION_DENIED:
      x.innerHTML="User denied the request for Geolocation."
      break;
    case error.POSITION_UNAVAILABLE:
      x.innerHTML="Location information is unavailable."
      break;
    case error.TIMEOUT:
      x.innerHTML="The request to get user location timed out."
      break;
    case error.UNKNOWN_ERROR:
      x.innerHTML="An unknown error occurred."
      break;
    }
  }
</script>

Please see here for more details:-http://www.w3schools.com/html/html5_geolocation.asp Here the javascript gives the latitude and longitude and checks the exact location of you.