As an important part of the o2o system, the store system naturally provides the LBS function as an essential tool. When building a system for a nearby store, we usually need to meet the following requirements:
(1) A store of a merchant within a default distance (for example, two kilometers) nearby
(2) If no query is found, extend the distance to continue the query.
(3) query the store and return the distance data
In this case, the distance between two points in the spherical space is analyzed. However, with the increase of store data, the number of simple distance calculations will be magnified, And we will naturally consider whether the store location is fixed and whether some preprocessing can be performed, first slice the store, then place the user's location in a certain film, and then calculate the distance.
The first approach is to exclude longitude and latitude. Each degree of latitude is about 111 km, but the distance between each degree of longitude ranges from 0km to 111km. The distance varies with the latitude, which is equal to the cosine of the 111km multiplied latitude. Therefore, the use of natural separation of latitude is a solution. Assume that the latitude and longitude of a user is (22.534756, 113.922333). to query the data within 10 kilometers, you can use the decimal digit after the latitude to exclude the longitude. Obviously, this sharding method is cumbersome, and the efficiency and scalability are not very good. However, it provides a way of thinking. Since the longitude and latitude can be divided into blocks, numbers these blocks and fix the merchants on these blocks as pre-processing data, after the user enters the system, the user is fixed to a small block using the same encoding method, and the distance is precisely calculated in the same small block. Can this solve the problem?
As a result, the second approach is the plane slicing method. Geohash is one method that uses binary splitting. It briefly introduces its implementation principle.
(1) first, divide the latitude range (-90, 90) into two intervals (-90, 0) and (0, 90). If the target latitude is located in the first interval, it is encoded as 0, otherwise it is encoded as 1.
(2) Use the area encoded as 1 before segmentation until the accuracy requirement is met.
(3) the codes of the longitude and latitude are merged. The odd digit is the latitude, and the even digit is the longitude. Returns a binary sequence.
(4) Each five-digit split sequence (base32 encoding) is encoded using digits 0-9 and letters B-z (excluding a, I, L, and O.
(5) because of the huge differences in encoding between the two squares, it is necessary to search for the eight neighboring grids if the two vertices to be searched are within a certain range.
Geohash precision list
In the following example, hash1 is a letter-encoded value. Similarly, hash6 is a six-character letter-encoded value.
Hash Value |
Area (km ^ 2) |
Approximate vertical and horizontal (latitude * longitude) Product (km * km) |
1 (total Earth Area) |
510,065,600 |
16384*32768 |
Hash1 |
15,939,500 |
4096*4096 |
Hash2 |
498,110 |
512*1024 |
Hash3 |
15,565 |
128*128 |
Hash4 |
486 |
32*16 |
Hash5 |
15 |
4*4 |
Hash6 |
0.5 |
1*0.5 |
Hash7 |
15625 (M ^ 2) |
125x125 (M * m) |
Hash8 |
488.28125 (M ^ 2) |
31.25x15.625 (M * m) |
Speaking of this, if you need to query the stores in the vicinity of the two kilometers, which hash value should be used for calculation?
[object Object]
It is arranged for the nine cells of hash6. In the figure, each cell has an area of 1 KM * 5KM. You can see it at any position in the E Cell, in jiugongge, at least 5KM of nearby stores can be queried. To meet 2KM of the requirements, 81 grids are required. In the same way, we can find that hash5 only needs nine grids, but the maximum coverage is 10 km. If it does not meet the distance requirement, the number of stores may be large.
After partitioning and filtering, you can get the set of stores that basically meet your needs, and then calculate the sphere distance to get the store distance.
Implementation of nearby stores