Implementation of high-German map and Baidu map extraction based on JavaScript the _javascript technique of the boundary latitude and longitude coordinate of the administrative region

Source: Internet
Author: User

Objective

Recently due to the need for work, need to extract some of the city's latitude and longitude coordinates, a little search, and found that Baidu map and the gold map have provided relevant functions and examples. Then the rest of the work is relatively simple, save the coordinates, and then converted to WGS coordinates, so as to match the existing GPS data and maps.

Main problems and Solutions

Save files locally support across browsers

For security reasons, JavaScript saves files locally in a way that is usually only supported by IE, and is activexobject/open to be unsafe and allowed to run each time. Fortunately, other browsers currently support <a> Tags to implement the method of downloading files. After testing the latest Google Chrome, Mozilla Firefox, Baidu Browser, 360 browser can run. Don't say nonsense, directly on the code:

function Download () {
//IE
if (/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"). Value;
W.document.charset = "UTF-8";
W.document.write (content);
W.document.execcommand ("SaveAs", false, filename+ ' txt ');
W.close ();
}
Firefox/chrome/safari/opera
else {
var filename = document.getElementById ("filename"). Value;
var content = document.getElementById ("content"). Value;
str = encodeuricomponent (content); 
document.getElementById ("Savechrome"). Download = filename+ ' txt '; 
var aLink = document.getElementById ("Savechrome"); 
Alink.href = "Data:text/csv;charset=utf-8," +STR; 
Alink.click (); 
}

Converted by latitude

This topic of interest to friends can search for their own Mars coordinate related conversion, the accuracy of the 1m range of online services available free to use. Self-writing program has proven accuracy within 6m.

Baidu Map method

A key function is a class that is generated by bmap.boundary (), and the method of calling it gets can be obtained by name from a county or municipal administrative area.

function Getboundary () {
var bdary = new Bmap.boundary ();
var name = document.getElementById ("Districtname"). Value;
Bdary.get (name, function (RS) {//Get administrative area
var fileName = "";
var newfileobject = fso. CreateTextFile (folderName + "\" + name + ". txt", true);
Map.clearoverlays (); Clear Map Overlay
var count = rs.boundaries.length;//Administrative point number for
(var i = 0; i < count; i++) {
var ply = new B Map.polygon (Rs.boundaries[i], {strokeweight:2, Strokecolor: "#ff0000"}); Establish polygon covering
map.addoverlay (ply);///Add Overlay
Map.setviewport (Ply.getpath ());//adjust field of view
}
Newfileobject.write (Rs.boundaries[0]);
Newfileobject.close ();
});

German map

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

 Amapadcode.search = function (adcodelevel, keyword, selectid) {//Query the Administrative division list and generate the corresponding Drop-down list var me
= this; if (Adcodelevel = = ' district ' | | Adcodelevel = = ' City ') {//Third level query boundary point this._district.setextensions (' All ');} else {this._district.setextensions (' base ')
; } this._district.setlevel (Adcodelevel); Administrative level this._district.search (keyword, function (status, result) {//Note that the format of the API returns is not uniform, and the Var districtdata is handled under three conditions respectively =
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); 

One of the districtdata.boundaries is what we need. After debugging, a bold guess is an object that implements the ToString () method.
"104.639106,26.863388,104.644771,26.861842,104.64767,26.854997,104.647748 ..." is clearly the GCJ coordinates we need.

Summarize

At this point, basically there is no problem, the rest of the work is to resolve the document. Need to extract the national data is the circular reading of the National City list file. (usually search CityName, the computer will find the reason, oh, guess is the Thunderbolt, QQ, such as IP positioning needs it.)

Important point, the recommended use of the map, the reason is that Baidu map to get the administrative planning problem, does not include county-wide. The most typical is Guizhou province, many cities are separated, It's a complex polygon with an island or a hole. Baidu is here. about how to handle complex polygons here to support display and processing in Mapwingis, a note will be written next time.

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.