【问题标题】:allow user to find other user within radius允许用户在半径内找到其他用户
【发布时间】:2015-01-21 14:07:21
【问题描述】:

我目前有这个代码,它将所有信息保存到数据库中,但我希望能够允许用户使用他们的邮政编码进行搜索,因此只显示他们选择的半径内的结果,例如。 1 英里、2 英里等我不知道该怎么做。

 <form action="addhobby.php" method="POST">
 Type:<input type="radio" name="type" value="meetup" checked>Meetup
<input type="radio" name="type" value="chat" checked>Chat
<br />

Hobby:<input type="text" name="hobby" value=""><br />

Location:<input type="text" name="location" value=""><br />
Postcode:<input type="text" name="postcode"><br />

Starts:  <input type="date" name="startdate"> <input type="time" name="starttime"><br />
Ends: <input type="date" name="enddate"> <input type="time" name="endtime"> <br />

Description: <input type="text" name="description"><br />


<input type="submit" name="submit" value="Create">

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $con);

   $sql="INSERT INTO activities (hobby, location, postcode, sdate, stime, edate,    etime, description, type) VALUES
('$_POST[hobby]','$_POST[location]', '$_POST[postcode]', '$_POST[startdate]', '$_POST[starttime]', '$_POST[enddate]', '$_POST[endtime]', '$_POST[description]', '$_POST[type]' )";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
 header("location:profile.php");

mysql_close($con)
?> 

【问题讨论】:

标签: php mysql html geolocation


【解决方案1】:

如果您可以使用 HTML5 Geolocation API 获取纬度和经度,为什么还要使用邮政编码?

您可以使用以下 SQL 语句:

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 5 ORDER BY distance;

您必须将lat 替换为纬度,将lng 替换为经度。要按公里而不是英里搜索,请将 3959 替换为 6371。

来源https://developers.google.com/maps/articles/phpsqlsearch_v3

【讨论】:

    【解决方案2】:

    为此,您需要一个邮政编码数据库,该数据库提供每个邮政编码的纬度和经度坐标。如果你有这个数据,那么你可以查询所有位于指定距离之间的邮政编码。

    您可以使用开源邮政编码数据库,例如 this(仅限美国)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      • 1970-01-01
      • 2014-07-04
      • 2015-01-14
      • 2020-11-14
      • 1970-01-01
      相关资源
      最近更新 更多