Closing minutes 21. querying the IP address of the map user site for the service of the Administrative Region by latitude and longitude

Source: Internet
Author: User

Now my retreat is coming to an end. According to the initial plan, I plan to spend about a month studying JavascriptCodeObfuscation. Later, as the engine of Google App Engine is being studied more and more deeply, and code obfuscation is more complicated than I thought, I canceled the plan to study code obfuscation, focus on website-related research.

This article introduces three Rest interface services that I implemented on Google App Engine:

1. query the service of administrative divisions by latitude and longitude, and transmit a coordinate of latitude and longitude. The returned result is the administrative division of the location. Generally, it is used to implement the "breadcrumb navigation" function on the map.

2. query the longitude and latitude of the IP address, and pass an IP address (or do not pass it, the IP address connected to the client is used by default). The returned result is the location text description and latitude and longitude of the IP address.

3. Load a simple joke information, which is a very simple and funny function.

Originally, these functions were available on the map man website, but I used to use ASP.. net, and the website runs on a host. In order to avoid the impact of host performance, these interfaces are not published.ProgramNow I have moved all the similar services on the map man website to the Google App Engine, and the performance and stability of the Google App Engine are relatively reliable, therefore, I publish these services for your reference.

Last time, because 0.18 million pieces of data on the website were not successfully uploaded to Gae due to a bug in Gae, the migration of Website Service Code was affected, but I recently learned that, previously, Gae was able to access local files (Khan! I may be confused by the 'yun' and cannot even access local files.) My data contains obvious rules that allow faster access to local files than databases, therefore, I decided to rewrite the Code, provide services by accessing files, skip the bug, and bring better performance.

However, Google's maximum size of a single file is 1 MB, which is quite harmful. After all, it is not easy to compress pieces of data into 1 MB, therefore, I had a hard time studying how to read and write Python binary files and data types for a few nights, and finally successfully compressed the data to 800 KB, in this way, the administrative divisions can be queried through the longitude and latitude.

The following describes the call process and implementation of each service. It must be noted that, except for the third serviceArticleClosing minutes 20. apart from the source code, parse RSS in Google App Engine has made public. The other two services are not made public because they are related to data and the source code is meaningless, however, if the second service is queried through IP addresses, my main source code is to be opened by others.Source codeAfter simple integration, it is easy to implement it by myself based on my ideas.

1. query administrative divisions by latitude and longitude. This service reads a local binary file to match the number of administrative divisions at a certain latitude and longitude. Then, it queries the administrative divisions information in Google datastore. After my observation, the performance is not bad. According to the statistics of the Google App Engine background, the average CPU time consumption is 80, which is relatively small, although the service looks complicated. Service call example:

Http://dituren-service.appspot.com/services/rgeo_ll?Ll=39.94691582580937, 116.38320347900391&C=Onrgeoloaded

Here, the ll parameter is separated by commas (,). Note that the latitude is at the beginning, and the longitude is at the end. The C parameter is the name of the rest callback function. The content returned in the preceding example is:

Onrgeoloaded ({area: 'Beijing-Xicheng district ', regioncode: '000000 '});

Among the returned objects, the area attribute is the hierarchical region name separated by minus signs, and the regioncode attribute is the administrative region code. If the result does not match the administrative region, the callback function is called with an empty parameter.

It should be noted that because this interface only queries the domestic administrative divisions, we recommend that you only query the latitude range (from 3 to 54) and the longitude range (from 73 to 136) match the content in the specified range. Otherwise, it makes no sense to connect the content in vain;

2. to query the longitude and latitude of an IP address, you must first thank gohsy , my IP address query function uses his code, and even the IP address library uses his version. I originally wanted to replace a more comprehensive (at least a larger file) ip address library, unfortunately, files that exceed 1 MB cannot be uploaded, but gohsy the IP address library file is exactly 1 MB. If I don't know how it works, I'm welcome to use it, after using this library to obtain the text description of an address through IP address, I use Google's Address Resolution Service it is clear to match the longitude and latitude. However, I will cache the Google address resolution results in the database, avoid performance and stability issues caused by frequent Google request address matching This service has the ability to obtain URL content, so the performance is unstable. According to statistics, the average CPU time consumption is 140, but this service has been running for several hours, as the cache address matching data increases, the average CPU usage will definitely decrease. Service call example:

Http://dituren-service.appspot.com/services/ip_lookup?C=Oniplookuploaded&IP=202.204.59.204

The parameter IP address is an IP address (nonsense). If the IP parameter does not exist, the client connection IP address is used by default. The parameter C is the name of the rest callback function and the returned content is:

Oniplookuploaded ({IP: '192. 204.59.204 ', address: 'Beijing University of Science and Technology', Lat: 202, Lon: 39.987700, Zoom: 14 })

In the returned object, the IP attribute is the current IP address. No matter whether the IP parameter is passed or not, an IP address is returned. The address attribute is the query result string in the IP address library, lon is the longitude and latitude information, and the zoom attribute is suitable for displaying the zoom level. Because of the precision of matching, for example, matching "Beijing University of Science and Technology" and matching "Beijing" both have the longitude and latitude, however, it is best to show the user a different zoom plane. The zoom level is based on Google's map. If you use it on the 51ditu interface, use 17-zoom.

Note that IP address query uses the IP address library on the Internet, and address matching uses the Google interface. I only make a simple integration. Therefore, data correctness and accuracy cannot be guaranteed.

3. loading a joke is a joke that cannot be used on a disk. It cannot be compared with the two interfaces above. However, I will also give a brief introduction, the source code of this function has been published in "closing minutes 20. parse RSS in Google App Engine

Sometimes, due to the speed problem, map loading is slow (MAP APIs are usually relatively large, and there are many map-related additional functions), allowing users to wait for the map download process, loading a joke or other text content in the map display area is good, but it is required that the content be loaded before the body content is loaded (otherwise it will lose its meaning ), in addition, it is required to load the page quickly. Do not slow down the page loading speed. After the body content is loaded, the body content is displayed and the original temporary content is replaced.

Because the page cannot be opened with the same content every time (if so, you can simply write it on a static page, in addition, the meaning of waiting for the page loading time is lost when users view a content each time. Therefore, the page content must be automatically changed. However, it is best that there should not be too much content to be changed, you can use the advantage of cache to make loading faster.

Based on the above idea, a service is developed. The client dynamically changes a random number ID and sends a request to the service. The service returns the content of the specified ID:

Http://dituren-service.appspot.com/services/joke_random?C=Onjokeloaded&ID=12

The parameter ID is a random integer passed in. Note that the parameter ID is not randomly displayed on the server. cache cannot be enabled if it is randomly displayed on the server. Parameter C is the name of the rest callback function, sample content returned by this service:

Onjokeloaded ({Title: "aristocratic kindergarten interview", content: "Rose went to the aristocratic kindergarten. During the interview, the teacher took out a 10-dollar bill and asked: What is this? Little Rose quickly replied: This is the waste paper that grandma gave to the beggar. The teacher said: Well, you don't have to take the test. You have already been admitted. "})

The title parameter is the title, and the content parameter is the content (all nonsense ),On the page, define the onjokeloaded function to display the joke in the specified place. After the page body content is loaded, erase the content.

The above is the description of the three related services provided on my website. These services do not consider using the XML format, but adopt the rest JavaScript format, it is expected to be called directly without cross-domain issues (because these services are on the dedicated domain name service.dituren.cn, and are not in the same domain as my own website) the content returned by the above service is all the javascript content in the UTF-8 format, you need to pay attention to the encoding problem when calling

This article only introduces these service interfaces. Later, I will encapsulate these services into simple Script objects that users can directly use, these objects will be published in the k_map class that I am preparing to update (originally named k_revertermap). Now I think some classes of k_map have not been updated for a long time.

after that, I will gradually add related services, for example, I can query weather information and other things based on the longitude and latitude!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.