如何从浏览器发送地理定位数据到服务器? [英] How to send Geolocation data from browser to server?

查看:134
本文介绍了如何从浏览器发送地理定位数据到服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来我需要在浏览器中运行客户端的函数,然后将纬度和经度发送给服务器,例如,ajax调用,然后使用响应数据在页面上反映它?那么为此我需要在服务器端添加一个新端点来管理这些请求?当用户加载一些页面时,是不是有可能从传入的请求中获取数据? 正如你已经说过的那样你自己,地理位置在客户端使用Javascript运行。当你的服务器的第一个请求已经完成,HTML被传送到客户端并且执行Javascript时,它会被执行。因此,不可以在第一个请求中使用地理定位数据,因为这些数据会在稍后执行。



您在初始请求中可以做的是使用用户的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆