LBS, stores the latitude and longitude coordinates of each location, searches for nearby locations, and establishes a geographical location index to improve query efficiency.
mongodb geographical location index, 2d and 2dsphere, corresponding to plane and sphere.
1. Create the coordinates of the lbs collection storage location
use lbs; db.lbs.insert( { loc:{ type: "Point", coordinates: [113.332264, 23.156206] }, name: "广州东站" } ) db.lbs.insert( { loc:{ type: "Point", coordinates: [113.330611, 23.147234] }, name: "林和西" } ) db.lbs.insert( { loc:{ type: "Point", coordinates: [113.328095, 23.165376] }, name: "天平架" } )
2. Create a geographical location index
db.lbs.ensureIndex( { loc: "2dsphere" } )
3. Query nearby coordinates
The current location is: Times Square,
Coordinates: 113.323568, 23.146436
Search for points within one kilometer nearby, sort from nearest to far
db.lbs.find( { loc: { $near:{ $geometry:{ type: "Point", coordinates: [113.323568, 23.146436] }, $maxDistance: 1000 } } } )
Search results:
{ "_id" : ObjectId("556a651996f1ac2add8928fa"), "loc" : { "type" : "Point", "coordinates" : [ 113.330611, 23.147234 ] }, "name" : "林和西" }
php code is as follows:
<?php // 连接mongodb function conn($dbhost, $dbname, $dbuser, $dbpasswd){ $server = 'mongodb://'.$dbuser.':'.$dbpasswd.'@'.$dbhost.'/'.$dbname; try{ $conn = new MongoClient($server); $db = $conn->selectDB($dbname); } catch (MongoException $e){ throw new ErrorException('Unable to connect to db server. Error:' . $e->getMessage(), 31); } return $db; } // 插入坐标到mongodb function add($dbconn, $tablename, $longitude, $latitude, $name){ $index = array('loc'=>'2dsphere'); $data = array( 'loc' => array( 'type' => 'Point', 'coordinates' => array(doubleval($longitude), doubleval($latitude)) ), 'name' => $name ); $coll = $dbconn->selectCollection($tablename); $coll->ensureIndex($index); $result = $coll->insert($data, array('w' => true)); return (isset($result['ok']) && !empty($result['ok'])) ? true : false; } // 搜寻附近的坐标 function query($dbconn, $tablename, $longitude, $latitude, $maxdistance, $limit=10){ $param = array( 'loc' => array( '$nearSphere' => array( '$geometry' => array( 'type' => 'Point', 'coordinates' => array(doubleval($longitude), doubleval($latitude)), ), '$maxDistance' => $maxdistance*1000 ) ) ); $coll = $dbconn->selectCollection($tablename); $cursor = $coll->find($param); $cursor = $cursor->limit($limit); $result = array(); foreach($cursor as $v){ $result[] = $v; } return $result; } $db = conn('localhost','lbs','root','123456'); // 随机插入100条坐标纪录 for($i=0; $i
Demo php Code, first you need to create a user and execute auth in mongodb's lbs. Here’s how: use lbs;
db.createUser(
{
"user":"root",
"pwd":"123456",
"roles":[]
}
)
db.auth(
{
"user":"root",
"pwd":"123456"
}
)

标题:C#中使用Array.Sort函数对数组进行排序的示例正文:在C#中,数组是一种常用的数据结构,经常需要对数组进行排序操作。C#提供了Array类,其中有Sort方法可以方便地对数组进行排序。本文将演示如何使用C#中的Array.Sort函数对数组进行排序,并提供具体的代码示例。首先,我们需要了解一下Array.Sort函数的基本用法。Array.So

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code<form name="myform"

在进行PHP编程时,我们常常需要对数组进行合并。PHP提供了array_merge()函数来完成数组合并的工作,不过当数组中存在相同的键时,该函数会覆盖原有的值。为了解决这个问题,PHP在语言中还提供了一个array_merge_recursive()函数,该函数可以合并数组并保留相同键的值,使得程序的设计变得更加灵活。array_merge

在PHP中,有许多强大的数组函数可以使数组的操作更加方便和快捷。当我们需要将两个数组拼成一个关联数组时,可以使用PHP的array_combine函数来实现这一操作。这个函数实际上是用来将一个数组的键作为另一个数组的值,合并成一个新的关联数组。接下来,我们将会讲解如何使用PHP中的array_combine函数将两个数组拼成关联数组。了解array_comb

在PHP编程中,数组是一种非常重要的数据结构,能够轻松地处理大量数据。PHP中提供了许多数组相关的函数,array_fill()就是其中之一。本篇文章将详细介绍array_fill()函数的用法,以及在实际应用中的一些技巧。一、array_fill()函数概述array_fill()函数的作用是创建一个指定长度的、由相同的值组成的数组。具体来说,该函数的语法

Python中的array模块是一个预定义的数组,因此其在内存中占用的空间比标准列表小得多,同时也可以执行快速的元素级别操作,例如添加、删除、索引和切片等操作。此外,数组中的所有元素都是同一种类型,因此可以使用数组提供的高效数值运算函数,例如计算平均值、最大值和最小值等。另外,array模块还支持将数组对象直接写入和读取到二进制文件中,这使得在处理大量数值数据时更加高效。因此,如果您需要处理大量同质数据,可以考虑使用Python的array模块来优化代码的执行效率。要使用array模块,首先需要

在Java编程中,数组是一种重要的数据结构。数组可以在一个变量中存储多个值,更重要的是可以使用索引访问每个值。但是在使用数组时,可能会出现一些异常,其中之一是ArrayStoreException。本文将讨论ArrayStoreException异常的常见原因。1.类型不匹配数组在创建时必须指定元素类型。当我们试图将不兼容的数据类型存储到一个数组中时,就会抛

在PHP编程中,数组是一个经常用到的数据类型。而关于数组的操作函数也是相当多的,其中包括了array_change_key_case()函数。该函数可以将数组中键名的大小写进行转换,从而方便我们进行数据的处理。本文就来介绍一下PHP中array_change_key_case()函数的使用方法。一、函数语法及参数array_change_ke


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version
