将HTML GeoLocations存储到PHP变量

时间:2012-11-14 11:12:44

标签: php javascript html html5 geolocation

我目前正在尝试通过html5获取地理位置并将它们存储到php变量中以进行进一步处理。

我已经关注了示例w3schools,但我无法弄清楚如何更改函数以返回纬度和经度,并将它们存储在单独的php变量$ lat和$ lon中。

这是我的代码(不正确)。它是一个php函数,用于打印地理定位的脚本。

function printGeoLocScript() {
    echo <<<_END

    <script>
function getLatitude()
  {
  if (navigator.geolocation)
    {
    return navigator.geolocation.getCurrentPosition(getLat,showError);
    }
  else{return  "Geolocation is not supported by this browser.";}
  }

function getLongitude()
  {
  if (navigator.geolocation)
    {
    return navigator.geolocation.getCurrentPosition(getLon,showError);
    }
  else{return  "Geolocation is not supported by this browser.";}
  }
function getLat(position)
  {
  return  position.coords.latitude;
  }

function getLon(position)
  {
   return position.coords.longitude;    
  }

function showError(error)
  {
  switch(error.code) 
    {
    case error.PERMISSION_DENIED:
      return "User denied the request for Geolocation."
    case error.POSITION_UNAVAILABLE:
      return "Location information is unavailable."
    case error.TIMEOUT:
      return "The request to get user location timed out."
    case error.UNKNOWN_ERROR:
     return "An unknown error occurred."

    }
  }
</script>

_END;
}

请注意,如果坐标不可用,我想要两个函数(getLongitude和getLatitude通过showError方法返回字符串错误。

最后,这些方法将通过表单的后期操作来调用..(虽然我不应该有任何问题)

0 个答案:

没有答案