duanhuilao0787 2018-12-04 22:46 采纳率: 100%
浏览 85
已采纳

在PHP If和Else语句不适用于JavaScript位置函数

I have a script in javascript and php that detects my position, the first part of the script works fine, after I would like if i set to a certain latitude and longitude value it do something, if latidudine and longitude are equal to preset values ​​for example, write "My House", otherwise write "out of my house". Even if I set the values ​​obtained from the script in the if the condition remains always false, perhaps because the script runs before the user gives consent to the location. Does anyone know how I could solve? Thanks in advance Here my code:

<html>
<body>

  <SCRIPT>
  function showlocation(){
  navigator.geolocation.watchPosition(callback);
  }

  function callback(position){
  document.getElementById('latitude').innerHTML = position.coords.latitude;
  document.getElementById('longitude').innerHTML = position.coords.longitude;
  }

  </SCRIPT>


  <?




  echo "<script>showlocation()</script>";

  $latitudine= "<span id=latitude></span><br>";
  $longitudine= "<span id=longitude></span>";

  echo "latitudine: ".$latitudine ."<br>";
  echo "longitudine: ".$longitudine."<br>";

  echo "latitudine: ".$latitudine ."<br>";
  echo "longitudine: ".$longitudine."<br>";
  if ($latitudine == "41.9473578" && $longitudine =="$longitudine")
  echo "My house";
  else echo "Out of my house"





  ?>

</body>
</html>
  • 写回答

1条回答 默认 最新

  • douzi7711 2018-12-04 23:10
    关注

    The PHP code run always in the server side and has not communication with JS code, that run in the browser.

    You should make all the code in JS to make it work. This is a running example:

    <html>
    <head>
    <script>
    function showlocation() {
        navigator.geolocation.watchPosition(callback);
    }
    
    function callback(position){
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
    
        document.getElementById('latitude').innerHTML = latitude;
        document.getElementById('longitude').innerHTML = longitude;
    
        var location = 'Out of my house';
    
        if (latitude == '41.9473578') {
            location = 'My house';
        }
    
        document.getElementById('location').innerHTML = location;
    }
    </script>
    </head>
    
    <body onload="showlocation()">
    
        <label>latitudine</label>: <span id=latitude></span>
        <br />
        <label>longitudine</label>: <span id=longitude></span>
        <br /><br /><br />
    
        <label>Location</label>: <span id="location"></span>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?