Extracting the latitude and longitude coordinates of the administrative region (German + Baidu)

Source: Internet
Author: User
Tags save file


Objective


Recently, due to work needs, need to extract some of the city latitude and longitude coordinates, a little search, found that Baidu map and the gold map provide relevant functions and examples. Then the rest of the work is relatively simple, save the coordinates, and then convert to WGS coordinates, so that the existing GPS data and map matching.


Major problems and workarounds local save file cross browser support


For security reasons, JavaScript natively saves files in a way that is usually only supported by IE's Activexobject/open method, and each time it is unsafe and allowed to run, it is cumbersome. Fortunately, other browsers currently support <a> The label implements the file download method. Tested for the latest Google Chrome, Mozilla Firefox, Baidu Browser, browser can be run. No nonsense, directly on the code:


function Download () {
     Ie
    if (/msie/i.test (navigator.useragent)) {
            var window. Open ("", " export ", "height=0,width=0,toolbar=no,menubar=no,scrollbars=no,resizable=on,location=no,status=no ");
    var document. getElementById ("filename"). Value  ;
            var document. getElementById ("content"). Value;
            W.document. CharSet = "UTF-8";
            W.document. Write (content);
            W.document. ExecCommand ("SaveAsfalse,  filename+ '. txt ');
            W.close();
}
    Firefox/chrome/safari/opera
    Else {
         var document. getElementById ("filename"). Value  ;
         var document. getElementById ("content"). Value;
         str =  encodeuricomponent (content);  
document. getElementById ("savechrome
var document. getElementById ("savechrome
         Alink.href = "data:text/csv;charset=utf-8," +STR;  
         Alink.click ();  
}
      }  
Latitude-Longitude Conversion


Friends interested in this topic can search their own Mars coordinate related conversion, accuracy in the 1m range of online services can be used free of charge. The self-written program has a proven accuracy within 6m.


Baidu Map method


The key function is the class generated by the Bmap.boundary (), and the method get that invokes it can be used to obtain the administrative regions above the county or city level by name.


        function getboundary () {
            var New Bmap.boundary ();
            var name document. getElementById ("districtname"). Value;


                Bdary.get (namefunction (RS) { //Get Administrative region
                    var fileName = "";
                    var newfileobject = fso. CreateTextFile (folderName + "\ \name +".txttrue);
                    Map.clearoverlays (); //Clear Map overlay
                    var count = rs.boundaries. length //How many points are there in the administrative region
                     for (var i = 0; i < count; i++) {
                        var New Bmap.polygon (Rs.boundaries[i], {strokeweight:2, Strokecolor: "#ff0000//Build Polygon Coverings
                        Map.addoverlay (ply); //Add overlay
                        Map.setviewport (Ply.getpath ()); //Adjust the field of view
                    }
                    Newfileobject.write (Rs.boundaries[0]);
                    Newfileobject.close ();
                });

        }
German map


The key code can be found in the drop-down list by reading the sample file where there is a boundary value appearing.


    function (adcodelevel, keyword, selectid) {//Query the Administrative Divisions list and generate the corresponding drop-down list
        var this;
        if (Adcodelevel = = ' district ' | | Adcodelevel = = ' City ') {//Third level query boundary point
            this. _district.setextensions (' all ');
        Else {
            this. _district.setextensions (' base ');
        }
         This //Administrative level
         This function (status, result) {//Note that the format returned by the API is not uniform and is processed in three conditions below
            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. Debugging, a bold guess is really an object that implements the ToString () method. "104.639106,26.863388,104.644771,26.861842,104.64767,26.854997,104.647748 ..." is obviously the GCJ coordinates that we need.





Summarize


At this point, basically there is no problem, the rest of the work is to parse the resulting files. The need to extract national data is to cycle through the National City list file. (usually search CityName, the computer will find, the reason, huh, guess is thunder, QQ, such as IP location needs it.)



It is important to recommend the use of the high-tak map, the reason is that Baidu map has been the administrative planning problems, not including county-level cities. The most typical is Guizhou province, many municipalities are separated, It is a complex polygon with an island or a hole. Baidu is defeated here. about how to handle complex polygons here to support display and processing in Mapwingis, next time write a note.






Extracting the latitude and longitude coordinates of the administrative region (German + Baidu)


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.