These two days in the experiment Baidu Map API, frankly do is still pretty good, but I need the administrative Region selection and anti-election function is not well realized, practice a bit finally found a better solution.
District Selection In fact, there is nothing to say, is directly to take out the name of the administrative region to call Bmap.boundary (). Get (Districtname,callback) is fine.
Counter-election, especially the uncertainty of how many districts of the anti-election is a problem.
My own choice is to find the latitude and longitude of our country's Four corners, then draw a closed polyline, connect the selected area, and then connect the four corners, and so on.
Advantage is easy & save resources (I lazy _ (: З"∠) _)
The downside is that you can't open a stroke, or you'll see a bunch of extra lines connected to the Northwest (upper left).
The specific code is dropped directly below (including selection & Binding events & Painted Screen shield)
The first time to write a little messy ... Anyway, it was a memo, so I was free.
In addition, there is a little to vomit groove ... Why is it not valid to set the transparency to 0?
Source:
//Baidu Map APIvarMap =NewBmap.map ("container", {enablemapclick:false});//Create a map instance, no click on the map Basemap//Set StyleMap.setmapstyle ({stylejson: [{//do not display point information"Featuretype": "Poi", "ElementType": "All", "Stylers": { "Color": "#ffffff", "Visibility": "Off"} ]}); map.disabledragging ();//prohibit draggingMap.disabledoubleclickzoom ();//Suppress double-click ScalingvarBlist = [];vardistrictloading = 0;functiongetboundary () {adddistrict ("Shibei District"); Adddistrict (Shinan District); Adddistrict ("Lichang District"); Adddistrict ("Sifang District");}/** Add Administrative divisions * @param {} districtname Division name * @returns no return value*/functionadddistrict (districtname) {//using counters to control the loading processdistrictloading++; varBdary =Newbmap.boundary (); Bdary.get (Districtname,function(RS) {//access to administrative regions varCount = Rs.boundaries.length;//How many points are there in the administrative region? if(count = = 0) {alert (' Failed to obtain the current input administrative area '); return; } for(vari = 0; I < count; i++{Blist.push ({points:rs.boundaries[i], name:districtname}); }; //counter after loading completion area Point-1districtloading--; if(districtloading = = 0) { //draw Endpoints When full load is completedrawboundary (); } });}/** * Various mouse event bindings*/functionClick (evt) {alert (evt.target.name);}functionmouseover (evt) {Evt.target.label.setZIndex (99); Evt.target.label.setPosition (Evt.point); Evt.target.label.show ();}functionMouseMove (evt) {evt.target.label.setPosition (evt.point);}functionmouseout (evt) {evt.target.label.hide ();}functiondrawboundary () {varPointarray = []; //painting a masking layer //define China's cardinal points as the first tier varPNW = {lat:53.0, lng:73.0 } varPNE = {lat:53.0, lng:136.0 } varPSE = {lat:3.0, lng:136.0 } varPSW = {lat:3.0, lng:73.0 } varParray = []; Parray.push (PNW); Parray.push (PSW); Parray.push (PSE); Parray.push (PNE); Parray.push (PNW); for(vari = 0; i < blist.length; i++) { //add a label layer for display varLabel =NewBmap.label (Blist[i].name, {offset:NewBmap.size (20,-10) }); Label.hide (); Map.addoverlay (label); //Adding polygon layers varply =NewBmap.polygon (blist[i].points, {strokeweight:2, Strokecolor: "#000000", fillopacity:0.01, FillColor: "#FFFFFF"});//Creating Polygon CoveringsPly.name =Blist[i].name; Ply.label=label; Ply.addeventlistener ("Click", click); Ply.addeventlistener ("MouseOver", mouseover); Ply.addeventlistener ("Mouseout", mouseout); Ply.addeventlistener ("MouseMove", MouseMove); Map.addoverlay (ply); //Add Name label layer varCenterlabel =NewBmap.label (Blist[i].name, {offset:NewBmap.size (0, 0) }); Centerlabel.setposition (Ply.getbounds (). Getcenter ()); Map.addoverlay (Centerlabel); //add points to the field of viewPointarray =Pointarray.concat (Ply.getpath ()); Parray=Parray.concat (Ply.getpath ()); Parray.push (Ply.getpath () [0]); Parray.push (PNW); Parray.push (PSW); Parray.push (PSE); Parray.push (PNE); Parray.push (PNW); } map.setviewport (Pointarray); //Adjust your horizons //Add a masking layer varPlyall =NewBmap.polygon (Parray, {strokeopacity:0.0000001, Strokecolor: "#000000", strokeweight:0.00001, FillColor: "#000000", fillopacity:0.8});//Creating Polygon CoveringsMap.addoverlay (Plyall);} SetTimeout (function() {getboundary ();},100);
Baidu map api-in China-wide selection and anti-selection (increase shielding layer)