Use Baidu map JSAPI to generate the h7n9 Avian Influenza distribution chart implementation code

Source: Internet
Author: User

An example of the H7N9 infection distribution chart generated using Baidu map JSAPI. The data is from xinhuanet (No. 4.8), as shown below:

The list of functions used is as follows:
1. Customize the copyright control function, that is, (1) the corresponding data source section. The Code is as follows: Copy codeThe Code is as follows: var cr = new BMap. CopyrightControl ({anchor: BMAP_ANCHOR_TOP_RIGHT });
Map. addControl (cr); // Add a copyright control
Var bs = map. getBounds ();
Cr. addCopyright ({id: 1, content: "<a href = 'HTTP: // your style = 'fontfamily:; font-size: 18px; background: white '> H7N9 data source </a> ", bounds: bs });

2. Obtain the administrative boundary function, that is, the border of the four provinces and cities corresponding to (2). The Code is as follows:Copy codeCode: var bdary = new BMap. Boundary (); // administrative Boundary service
Var length = data. length;
For (var index = 0; index <length; index ++)
{
(Function (index) {// closure usage
Bdary. get (data [index]. city, function (rs) {// obtain the Administrative Region
Var count = rs. boundaries. length; // The administrative area consists of several polygon.
Var bounds;
Var center;
For (var I = 0; I <count; I ++ ){
Ply = new BMap. polygon (rs. boundaries [I], {strokeWeight: 2, strokeColor: data [index]. color, fillColor: data [index]. color, fillOpacity: 0.8}); // create a polygon covering.
If (! Bounds) // obtain the central point of the first part of the administrative area
{
Center = ply. getBounds (). getCenter ();
}
Map. addOverlay (ply); // Add administrative boundaries
}
});
}) (Index );
}

3. Add the marker and label, that is, (3, 4) to display the picture of the infected person and the label of the infected person:Copy codeThe Code is as follows: var myIcon = new BMap. Icon ("infection.png", new BMap. Size (27,60 ));
Var marker1 = new BMap. Marker (center, {icon: myIcon}); // marker
Map. addOverlay (marker1 );
Var label = new BMap. Label ("count" + data [index]. infect, {offset: new BMap. Size (-10, 0)}); // display label
Marker1.setLabel (label );

The complete code is as follows:Copy codeThe Code is as follows: <! DOCTYPE html>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Style type = "text/css">
Body, html, # allmap {width: 100%; height: 100%; overflow: hidden; margin: 0 ;}
# L-map {height: 100%; width: 78%; float: left; border-right: 2px solid # bcbcbcbc ;}
# R-result {height: 100%; width: 20%; float: left ;}
</Style>
<Script type = "text/javascript" src = "http://api.map.baidu.com/api? V = 1.4 "> </script>
<Title> H7N9 distribution chart </title>
</Head>
<Body>
<Div id = "allmap"> </div>
</Body>
</Html>
<Script type = "text/javascript">
Var map = new BMap. Map ("allmap ");
Map. centerAndZoom (new BMap. Point (116.403765, 39.914850), 5 );
Map. enableScrollWheelZoom ();
Var cr = new BMap. CopyrightControl ({anchor: BMAP_ANCHOR_TOP_RIGHT });
Map. addControl (cr); // Add a copyright control
Var bs = map. getBounds ();
Cr. addCopyright ({id: 1, content: "<a href = 'HTTP: // your style = 'fontfamily:; font-size: 18px; background: white '> H7N9 data source </a> ", bounds: bs });
Map. setViewport (new BMap. Bounds (new BMap. Point (114.603589, 26.853244), new BMap. Point (122.863975, 35.288772); // adjust the field of view
Var data = [{city: 'shanghai', infect: 10, color: '# FF0000'}, {city: 'jiangsu province ', infect: 6, color: '# EE3B3B'}, {city: 'anhui province ', infect: 2, color:' # FFAEB9 '}, {city: 'zhejiang province', infect: 3, color: '# EE3B3B'}];
// Shanghai (10 cases, 4 deaths), Jiangsu (6 cases), Anhui (2 cases), Zhejiang (3 cases, 2 cases of deaths) data no. 4.8
Var bdary = new BMap. Boundary ();
Var length = data. length;
For (var index = 0; index <length; index ++)
{
(Function (index) {// closure usage
Bdary. get (data [index]. city, function (rs) {// obtain the Administrative Region
Var count = rs. boundaries. length; // The administrative area consists of several polygon.
Var bounds;
Var center;
For (var I = 0; I <count; I ++ ){
Ply = new BMap. polygon (rs. boundaries [I], {strokeWeight: 2, strokeColor: data [index]. color, fillColor: data [index]. color, fillOpacity: 0.8}); // create a polygon covering.
If (! Bounds) // obtain the central point of the first part of the administrative area
{
Center = ply. getBounds (). getCenter ();
}
Map. addOverlay (ply); // Add a covering
}

Var myIcon = new BMap. Icon ("infection.png", new BMap. Size (27,60 ));
Var marker1 = new BMap. Marker (center, {icon: myIcon}); // marker
Map. addOverlay (marker1 );
Var label = new BMap. Label ("count" + data [index]. infect, {offset: new BMap. Size (-10, 0)}); // display label
Marker1.setLabel (label );
});
}) (Index );
}
</Script>

Done!

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.