搜索数据库与经纬度和长半径使用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
最满意答案
正如你所提到的那样,头文件是提供给你的,我想,这是一个任务。
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 fromgridNew
togrid
element by element just like you are now copying fromgrid
togridOut
ingetGrid
method.
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
将公共数据和私有基类的成员公开为派生类中的公共数据是一个坏主意。 但是,如果必须,您可以使用: class B : private A { public: using A::a; using A::b; using A::c; }; Exposing the public data and members of a private base class as public data in a derived class is a bad idea. However ...
-
如果你问是否可以让私有成员对派生类可见,答案是否定的 - 这就是为什么它们是私有的。 如果希望派生类能够访问它们,请在基类中使用protected成员。 If you're asking whether you can make private members visible to derived classes, the answer is no - that's why they are private. Use protected members in the base class if you wa ...
-
是的,你可以使用基类功能。 class A { protected: void foo() {} void do_other_foo(A& ref) { ref.foo(); } }; class B : public A { public: void bar(A& a) { // edit: called with &a != this this->do_other_foo(a); } }; Yes, you can use a base-class funct ...
-
正如你所提到的那样,头文件是提供给你的,我想,这是一个任务。 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_G ...
-
逻辑可能如下所示: 声明变量: mouseIsDown = False; 释放=假; 在mouseMotion中: if (mouseIsDown && ! released) { // your current code } 在mouseButtonDown中: if (mouseIsDown) { released = True; } else { mouseIsDown = True; released = False; } 在mouseButtonUp中: if ...
-
这是不使用构造函数和析构函数的方式,我希望你发现它很有用。 #include
class Fruit { private : int* no; public : void initialize(); public : void clean(); public : int print_type(); }; void Fruit::initialize() { no = new int[ 3 ]; ... -
在类中定义的属性在编译期间实例化。 这意味着不评估该值,但必须是常量值。 函数返回不是常量值,因此必须在运行时进行评估。 为此,您应该在构造函数中实例化它们。 有关PHP文档的更多信息,请参阅 Properties which are defined in your class are instantiated during compile-time. This means that the value is not evaluated, but has to be a constant value. Fu ...
-
需要一个私有和公共方法来获得相同的递归函数(Needing a private and public method for the same recursive function)[2022-02-14]
“我的直觉是,这可能是为了确保使用公共类方法的人不会进入完全的dingus模式并最终调用带有参数的递归函数,这些参数不会导致停止/基本情况。” 你的直觉在某种程度上是正确的。 head在内部使用类进行管理,不应作为调用者的参数引入。 与递归没有特定的相关性,而是数据封装OOP原则: 应该在内部管理类,尽管函数findMax()应该在公共类接口中可用。 为了提供适当的内部实现,搜索被委托给private实现,在这种情况下,它是递归使用的。 但这并不像提到的那样重要。 至于你在问题中的编辑。 您应该将尽可能多的 ...