如何从浏览器发送地理定位数据到服务器? [英] How to send Geolocation data from browser to server?
问题描述
您在初始请求中可以做的是使用用户的IP至少获得一个近似的地理位置。我认为这是一个很好的做法,因为用户可能会拒绝或简单地忽略将地理位置数据提供给您的网站。使用基于IP的方法,您至少可以有一个后备解决方案。
It seems that I need to run a function on client-side in browser, then send latitude and longitude to server in, for example, an ajax call then use the response data to reflect it on the page? So for that I need to add a new endpoint on server side just to manage such requests? Isn't it possible to get that data from inside the incoming request when a user loads some page?
As you already said yourself, the geolocation is running in Javascript on the client side. This gets executed when the first request to your server is already finished, the HTML is delivered to the client and the Javascript is executed. So no, it is not possible to use the geolocation data with the first request, simply because this data gets executed later.
What you can do in your initial request is to use the IP of the user to get at least an approximate geolocation. I think in general this is a good practice because users may decline or simply ignore to give geolocation data to your website. With the IP-based approach you can at least have a fallback solution.
这篇关于如何从浏览器发送地理定位数据到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!