Based on JavaScript, AMAP and Baidu map are used to extract the coordinates of the longitude and latitude of the administrative region boundary.

Source: Internet
Author: User
This article introduces how to use javascript to extract the coordinates of the administrative region boundary longitude and latitude from AMAP and Baidu. This article is highly practical and easy to understand. For more information, see. Using JavaScript to extract the coordinates of longitude and latitude of administrative region boundary using AMAP and Baidu map _ javascript skills

Preface

Recently, due to work needs, We need to extract the coordinates of the longitude and latitude of some cities. After a slight search, we found that Baidu map and AMAP both provide related functions and examples. then the remaining work is relatively simple. Save the coordinates and convert them to the WGS coordinates to match the existing GPS data and map.

Main Problems and Solutions

Cross-browser support for local file storage

For security reasons, the method for saving files locally in JavaScript is usually only the ActiveXObject/Open method supported by IE. Every time, it is very troublesome to prompt for uneasiness and allow running. fortunately, other browsers currently support tag-based file download methods. the latest Google Chrome, Mozilla Firefox, Baidu browser, and 360 browsers can all run. without talking nonsense, go directly to the Code:


Function Download () {// IEif (/msie/I. test (navigator. userAgent) {var w = window. open ("", "Export", "height = 0, width = 0, toolbar = no, menubar = no, scrollbars = no, resizable = on, location = no, status = no "); var filename = document. getElementById ("filename "). value; var content = document. getElementById ("content" mirror.value?#doc ument. charset = "UTF-8" registry.document.write(content={document.exe cCommand ("SaveAs", false, filename='.txt '); w. close ();} // Firefox/Chrome/Safari/Operaelse {var filename = document. getElementById ("filename "). value; var content = document. getElementById ("content "). value; str = encodeURIComponent (content); document. getElementById ("SaveChrome "). download = filenamepolic'.txt '; var aLink = document. getElementById ("SaveChrome"); aLink. href = "data: text/csv; charsets = UTF-8," + str; aLink. click ();}}

Longitude and latitude Conversion

If you are interested in this topic, you can search for the conversions related to the Mars coordinates by yourself. Services provided on the Internet with a precision of 1 MB can be used for free. The self-writing program has been verified to be within 6 MB.

Baidu Map Method

The key function is the class generated by BMap. Boundary (). By calling the get method, you can obtain the administrative region of the county or municipal level by name.


Function getBoundary () {var bdary = new BMap. boundary (); var name = document. getElementById ("districtName "). value; bdary. get (name, function (rs) {// obtain the administrative region var fileName = ""; var newFileObject = fso. createTextFile (folderName + "\" + name + ". txt ", true); map. clearOverlays (); // clear the map covering var count = rs. boundaries. length; // The number of for (var I = 0; I <count; I ++) {var ply = new BMap. polygon (rs. boundaries [I], {strokeWeight: 2, strokeColor: "# ff0000"}); // create a polygon covering map. addOverlay (ply); // Add a covering map. setViewport (ply. getPath (); // adjust the field of view} newFileObject. write (rs. boundaries [0]); newFileObject. close ();});}

AMAP

By reading the sample file, you can see that the returned boundary value appears in the drop-down list.


AmapAdcode. search = function (adcodeLevel, keyword, selectId) {// query the list of administrative divisions and generate the corresponding drop-down list var me = this; if (adcodeLevel = 'District '| adcodeLevel = 'city') {// query the boundary point this at level 3. _ district. setExtensions ('all');} else {this. _ district. setExtensions ('base');} this. _ district. setLevel (adcodeLevel); // administrative level this. _ district. search (keyword, function (status, result) {// note that the formats returned by the api are not uniform. the following three conditions are used to process var districtData = result. districtList [0]; if (districtData. districtList) {me. createSelectList (selectId, districtData. districtList);} else if (districtData. districts) {me. createSelectList (selectId, districtData. districts);} else {document. getElementById (selectId ). innerHTML = '';} map. setCenter (districtData. center); me. clearMap (); me. addPolygon (districtData. boundaries );

The districtData. boundaries is what we need. After debugging, we can guess that it is an object that implements the Tostring () method.
"104.639106, 26.863388, 104.644771, 26.861842, 104.64767, 26.854997, 104..." it is obvious that we need gcj coordinates.

Summary

At this point, there is basically no problem, and the rest of the work is to parse the file. the national data needs to be extracted, that is, the nationwide city list file is read cyclically. (I usually search for cityname and find it in my computer. The reason is that I guess I need IP address location like thunder and QQ .)

Importantly, we recommend that you use AMAP because the administrative plan obtained by Baidu map is incorrect and does not include the county magistrate. the most typical is Guizhou Province, where many cities are separated and are complex polygon with islands or holes. baidu is defeated here. the complex polygon can be displayed and processed in MapWinGIS. I will write a note next time.

The above is based on JavaScript to implement AMAP and Baidu map to extract the coordinates of the administrative region boundary latitude and longitude _ javascript skills. For more information, please follow the PHP Chinese Network (www.php1.cn )!

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.