jQuery 和/或 PHP 位置查找器:邮政编码和城市/州
我希望使用用户的城市、州自动填充表单输入文本区域。我还需要邮政编码才能与表格一起传递。我不需要地图。
有什么建议吗? (我更喜欢 jQuery 和/或 PHP。)
更新 - 我发现这个小js可以获取城市、州、纬度、经度、区号等......但它没有获取邮政编码。可能是因为很多城市都有多个邮政编码。但我所需要的只是距城市一趟......无论是哪一趟都没关系,因为我正在抓取 50 英里半径内的数据。那么,是否可以使用下面的脚本并根据它返回的数据从该位置查找 zip。
<html>
<head>
<script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>
</head>
<body>
<script language="Javascript">
document.write("Welcome to our visitors from "+geoplugin_city()+", "+geoplugin_region()+", Area code = "+geoplugin_areaCode()+", Latitude = "+geoplugin_latitude()+geoplugin_areaCode()+", Longitude = "+geoplugin_longitude());
</script>
</body>
</html>
来源:GeoPlugin
I'm looking to auto-populate a form input textarea with the user's city, state. I also need the zip code to pass with the form. I don't need a map.
Any suggestions? (I prefer jQuery and/or PHP.)
UPDATE -
I discovered this little js to get the city, state, latitude, longitude, area code, etc... except it doesn't get the zip. Probably because a lot of cities have multiple zips. But all I need is one zip from the city... it doesn't matter which one, because I'm grabbing data within a 50 mile radius. So is there anyway to take this script below and find a zip from the location based on the data that it returns.
<html>
<head>
<script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>
</head>
<body>
<script language="Javascript">
document.write("Welcome to our visitors from "+geoplugin_city()+", "+geoplugin_region()+", Area code = "+geoplugin_areaCode()+", Latitude = "+geoplugin_latitude()+geoplugin_areaCode()+", Longitude = "+geoplugin_longitude());
</script>
</body>
</html>
Source: GeoPlugin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 GeoLite City 将用户的 IP 转换为城市、州邮政编码
You can use GeoLite City to convert the user's IP into the City, State Zip
如果没有服务器端代码就不可能做到这一点,考虑到潜在的安全风险,这是显而易见的。然而,您可以做的是向某些服务器端脚本发出请求并让它解析用户的 IP,但这存在一定的陷阱 - 1.)您可以获得的所有城市/州,以及 2.)用户可以使用代理,最重要的是,IP 有时不准确。
还有 HTML5 地理定位 API:http://html5demos.com/geo
Not possible to do this without server-side code, which is kind of obvious considering the potential security risk. What you can do however, is make a request to some server-side script and have it parse the user's IP, but there are certain pitfalls with that - 1.) city/state are all you can get, and 2.) Users could be using a proxy and on top of that, IPs sometimes aren't accurate.
There's also the HTML5 Geolocation API: http://html5demos.com/geo
有两种方法可以做到这一点。
1) 服务器端使用 php 库基于 ip 进行地理定位(仅当 ISP 正确输入 ip 的地理数据时才有效)并将数据插入到您喜欢的任何位置
2) 使用 google 地图的 地理编码服务动态检索您需要的信息。
ps 我最喜欢的方法是#2 提供了更多的灵活性。
There are 2 ways to do this.
1) server-side using php library for geolocation based on the ip (works only if the geodata for the ip is entered correctly by the ISP) and insert the data wherever you like
2) use google maps' geocoding service to dynamically retrieve the info you need.
p.s. my favorite way is #2 offers much more flexibility.