Mobile Internet, in addition to the characteristics of the online, there is an important feature, can be positioned to the location of the mobile phone. Find nearby people, nearby restaurants and more, as well as a large number of apps that need to use lbs (Location Based services). So, how is the user's geographic coordinates? How do I find the data that is near you? In this article, for your one by one.
1. How to get the geographic coordinates of a user
Now, based on the phone to get the user's geographical coordinates, mainly the following two ways:
(1) using the GPS module on the phone
(2) using the mobile network to connect the base station location
The first method is generally used, when the phone does not have a GPS module, the second method to locate.
The app side recommends using the Map SDK to obtain geographic coordinates directly from the method of obtaining geographic coordinates, which automatically determines which method to use.
For the first time to do lbs's small partner, the geographical coordinates of the migration problem needs attention. Through the mobile phone to get coordinates, put on Baidu map or Gao de map, there will always be offset, for example, at that time was in the former headquarters "Southern Communications building" around the coordinates obtained, it is the coordinates on the Baidu map to the South China Normal University (that is my alma mater).
Was plagued by this problem for a long time, looked up a lot of information to find is this reason: the national law stipulates that all electronic map service providers need to add the map data offset and encryption, for example, you get the coordinates is 100,90, this coordinate to the map has become 105, 95, the difference is offset.
Moreover, this offset is not clearly defined, which creates a phenomenon, different electronic map service providers have different coordinate system, for example, Google Maps, gold map, Apple map is the same set of coordinate system, Baidu Map is another set of coordinate system.
So, how to solve this problem? The method is very simple, using the Map SDK to provide the ability to obtain geographic coordinates, the coordinates obtained are already offset.
2. How to find a nearby user (merchant, person)
The general business logic is that there is a lot of coordinates in the data, knowing a coordinate to find out the other coordinates within a certain range (e.g. within 500 meters).
Here are 3 implementation scenarios:
(1) MySQL's spatial database
Reference: http://blog.csdn.net/chaiqi/article/details/23099407
Starting with MySQL4.1, MySQL has introduced a series of spatial expansions that allow it to have a certain amount of space processing power.
In simple terms, it is possible for MySQL to think of geographic coordinates as a separate data type, and to provide relevant spatial functions (such as finding coordinates within a rectangular range) to be called directly to the developer.
(2) Geohash
Geohash's detailed introduction, can refer to http://www.cnblogs.com/dengxinglin/archive/2012/12/14/2817761.html
Geohash code, you can convert the geographical coordinates into a value, simply to convert the two-dimensional coordinates into one-dimensional coordinates. Geohash represents not a point, but a rectangular area. For example, the code W23YR3, which represents a rectangular region. Geohash encoded prefixes can represent larger areas. For example, W23yr3, whose prefix w23yr represents a larger range including encoded w23yr. This feature can be used to search nearby locations.
It's very convenient to find nearby, and in SQL, like ' w23yr3% ' can query all nearby locations.
In the previous product, a requirement is to find the store near the user (including keywords), the store's data 1.3 million, all in MySQL, found to use MySQL like ' w23yr3% ' this way to retrieve Geohash performance bottleneck is very large, retrieving 1.3 million rows of data, It takes about 8 seconds on average, which is unbearable for the response speed.
Later, after continuous optimization, determined the following using the Coreseek+redis+mysql solution, the response speed was reduced to an average of 1 seconds, the scheme is as follows:
1. Calculate Geohash with the coordinate value of each shop, put Geohash as key, store ID as value, and put it into Redis set set.
2. According to the user's coordinates calculation Geohash, in Redis with "keys *" method matching find nearby Shops Geohash (remember Geohash features? Geohash encoded prefix can represent a region), and then get the store's ID
3. Use the ID of the store in 2 as filter and continue searching in the Coreseek.
(3) MongoDB
Location support is a major feature of MongoDB, the world's most popular lbs service Foursquare, the domestic fast, the choice is also MongoDB.
Using MongoDB to develop LBS services has the following advantages:
1. The performance of MongoDB itself is high, update, query fast.
2. With the method of replica set, Shard and so on, it is easy to realize the distributed deployment of MongoDB and solve the performance bottleneck.
3. MongoDB has been used extensively in the app backend, and development deployment is familiar to many developers.
With MongoDB, you can support:
1. The coordinates of the query polygon range.
2. Query for nearby coordinates.
3. Queries the coordinates within the circular area.
It is highly recommended that mongodb!!! be preferred in lbs service
--------------------------------------------------------------------------------------------------------------- ------------
Open the total list of articles in the Link app backend series to see all the original "app backend" articles I've published.
"Author" Zeng Jiansen
"QQ" 190678908
"app back-end QQ Group" 254659220
"public number" Appbackend
"Sina Weibo" @newjueqi
"blog" Http://blog.csdn.net/newjueqi
18.app backend How to achieve lbs