首页 \ 问答 \ 搜索数据库与经纬度和长半径使用PHP的MySQL(Search database with lat and long by radius using php mysql)

搜索数据库与经纬度和长半径使用PHP的MySQL(Search database with lat and long by radius using php mysql)

我试图开发一个小型站点,通过设置的半径从数据库中检索结果并将它们放置在地图上。 我使用谷歌地图api来获取经纬度,并将其传递给我的php脚本,该脚本查询数据库并将结果集作为JSON对象返回。

我有一个小的问题,使用json将经纬度和长度都发送到php。

我的主要问题是,我的sql搜索数据库似乎是错误的,因为它只是检索我的数据库中的前10个结果。 我希望它从搜索点返回半径范围内的前10个结果。

这是我的ajax代码

function showCarPark(location)
{
    var lat = location.lat();
    var lng = location.lng();
    //alert("Lat: " +lat.toFixed(6));
    //alert("Lng: " +lng.toFixed(6));
    document.getElementById('carParkResults').innerHTML = "";
    var criterion = document.getElementById("address").value;
    var count = 0;
    $.ajax({    
              url: 'process.php',
              type: 'GET',
              data: "lat=" + lat + "&lng=" + lng,
              dataType: 'json',
              success: function(data) 
              {
                  jQuery.each(data, function()
                  {
                    $('<p>').text("Car Park: " + data[count].name).appendTo('#carParkResults');
                    placeCarParks(data[count].postcode,data[count].name, data[count].street, data[count].type);
                    count++;
                  });
              },
              error: function(e) 
              {
                //called when there is an error
                console.log(e.message);
                alert("error" + e.message);
              }
    });

这是我的PHP脚本

$rad = 20;
$lat = $_GET['lat'];
$lng = 1.4681464; //put a temporary number in as it wont pass the lng in the JSON

$sql="SELECT *, (3959 * acos(cos(radians('".$lat."')) * cos(radians(lat)) * cos( radians(long) - radians('".$lng."')) + sin(radians('".$lat."')) * 
sin(radians(lat)))) 
AS distance 
FROM carpark HAVING distance < 15 ORDER BY distance LIMIT 0 , 10";

$result = mysql_query($sql);

while($r = mysql_fetch_assoc($result)) $rows[] = $r;

echo json_encode($rows);

我的表中的列被称为拉特和长任何帮助表示赞赏。


Im trying to develop a small site which retrieves a results from a database by a set radius and places them on a map. I use Google maps api to get the lat and long from there search criteria, pass these to my php script which queries the database and returns the result set as a JSON object.

Im having small a problem sending both the lat and long to the php using json.

My main problem is that my sql to search the database appears to be wrong as it just retrieves the first 10 results in my database. I want it to return the first 10 results within the radius from the search point.

Here is my ajax code

function showCarPark(location)
{
    var lat = location.lat();
    var lng = location.lng();
    //alert("Lat: " +lat.toFixed(6));
    //alert("Lng: " +lng.toFixed(6));
    document.getElementById('carParkResults').innerHTML = "";
    var criterion = document.getElementById("address").value;
    var count = 0;
    $.ajax({    
              url: 'process.php',
              type: 'GET',
              data: "lat=" + lat + "&lng=" + lng,
              dataType: 'json',
              success: function(data) 
              {
                  jQuery.each(data, function()
                  {
                    $('<p>').text("Car Park: " + data[count].name).appendTo('#carParkResults');
                    placeCarParks(data[count].postcode,data[count].name, data[count].street, data[count].type);
                    count++;
                  });
              },
              error: function(e) 
              {
                //called when there is an error
                console.log(e.message);
                alert("error" + e.message);
              }
    });

And here is my php script

$rad = 20;
$lat = $_GET['lat'];
$lng = 1.4681464; //put a temporary number in as it wont pass the lng in the JSON

$sql="SELECT *, (3959 * acos(cos(radians('".$lat."')) * cos(radians(lat)) * cos( radians(long) - radians('".$lng."')) + sin(radians('".$lat."')) * 
sin(radians(lat)))) 
AS distance 
FROM carpark HAVING distance < 15 ORDER BY distance LIMIT 0 , 10";

$result = mysql_query($sql);

while($r = mysql_fetch_assoc($result)) $rows[] = $r;

echo json_encode($rows);

The columns in my table are called lat and long Any help is appreciated.


原文:https://stackoverflow.com/questions/10621345
更新时间:2023-11-09 08:11

最满意答案

正如你所提到的那样,头文件是提供给你的,我想,这是一个任务。 getGrid方法旨在为外部调用者提供接口以获取Tetrmino对象网格的副本。 由于无法从函数返回数组,因此getGrid方法提供输出参数。

用法示例:

void Tetrimino::getGrid(int gridOut[][TETRIMINO_GRID_SIZE]) {
   for(int i = 0; i < TETRMINO_GRID_SIZE; i++) { 
      for(int j = 0; j < TETRMINO_GRID_IZE; j++ ) { 
          gridOut[i][j] = grid[i][j];
      }
   }
}

...
...

Tetrmino obj(3);

... 
... 

int grid[TETRIMINO_GRID_SIZE][TETRMINO_GRID_SIZE];
obj.getGrid(grid);
// now grid holds the copy of interal grid
for(int i = 0; i < TETRMINO_GRID_SIZE; i++) { 
   for(int j = 0; j < TETRMINO_GRID_IZE; j++ ) { 
       std::cout << grid[i][j] << " ";
   }
   std::cout << "\n";
}
std::cout << std::flush;

编辑:扩展答案:为什么没有分配网格?

问题是,在构造函数中,您声明了一个与类成员同名的新int数组。 这意味着,您没有初始化成员变量。 C ++在初始化后不允许分配给原始数组,只需复制即可。

将新变量更改为gridNew或类似的东西,并从gridNew复制到grid逐个元素,就像您现在在getGrid方法中从grid复制到gridOut getGrid


As you mention, that the header file was provided to you, I guess, this is an assignment. getGrid method is meant to provide interface for outside caller to get a copy of the Tetrmino objects grid. As you cannot return an array from function, the getGrid method provides an output parameter.

Example usage:

void Tetrimino::getGrid(int gridOut[][TETRIMINO_GRID_SIZE]) {
   for(int i = 0; i < TETRMINO_GRID_SIZE; i++) { 
      for(int j = 0; j < TETRMINO_GRID_IZE; j++ ) { 
          gridOut[i][j] = grid[i][j];
      }
   }
}

...
...

Tetrmino obj(3);

... 
... 

int grid[TETRIMINO_GRID_SIZE][TETRMINO_GRID_SIZE];
obj.getGrid(grid);
// now grid holds the copy of interal grid
for(int i = 0; i < TETRMINO_GRID_SIZE; i++) { 
   for(int j = 0; j < TETRMINO_GRID_IZE; j++ ) { 
       std::cout << grid[i][j] << " ";
   }
   std::cout << "\n";
}
std::cout << std::flush;

Edit: To expand on the answer: why grid is not assigned?

The problem is, that within your constructor you are declaring a new int array with the same name as the class member. This means, that you are not initializing the member variable. C++ does not allow to assign to raw array after its initialization, you are left just with copying.

Change the new variable to gridNew or something similar and copy from gridNew to grid element by element just like you are now copying from grid to gridOut in getGrid method.

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)