How does the Baidu map JavaScript API display multiple maps on the same page?

Source: Internet
Author: User

Some people often ask, can Baidu map's JavaScript API display multiple maps on the same page? Of course. Why not? A map is nothing more than a div on a page. We can display multiple div on the page. Naturally, we can also display multiple maps.


Here are the effects of adding two maps to the page:

 

The core code of the page is as follows:


[Html]
<Style type = "text/css">
# MapDiv1 {height: 400px; width: 600px ;}
# MapDiv2 {height: 400px; width: 600px; left: 620px; top:-420px ;}
</Style>
<Body>
<Div id = "mapDiv1"> </div>
<Br/>
<Div id = "mapDiv2"> </div>
</Body>
</Html>
<Script type = "text/javascript">
// Map Initialization
 
Var map1 = new BMap. Map ("mapDiv1 ");
Map1.centerAndZoom (new BMap. Point (116.401394, 39.915156), 12 );
Map1.enableScrollWheelZoom ();
 
Var map2 = new BMap. Map ("mapDiv2", {mapType: BMAP_HYBRID_MAP}); // you can specify a satellite Map as a base Map.
Map2.centerAndZoom (new BMap. Point (116.401394, 39.915156), 12 );
Map2.enableScrollWheelZoom ();
</Script>

<Style type = "text/css">
# MapDiv1 {height: 400px; width: 600px ;}
# MapDiv2 {height: 400px; width: 600px; left: 620px; top:-420px ;}
</Style>
<Body>
<Div id = "mapDiv1"> </div>
<Br/>
<Div id = "mapDiv2"> </div>
</Body>
</Html>
<Script type = "text/javascript">
// Map Initialization

Var map1 = new BMap. Map ("mapDiv1 ");
Map1.centerAndZoom (new BMap. Point (116.401394, 39.915156), 12 );
Map1.enableScrollWheelZoom ();

Var map2 = new BMap. Map ("mapDiv2", {mapType: BMAP_HYBRID_MAP}); // you can specify a satellite Map as a base Map.
Map2.centerAndZoom (new BMap. Point (116.401394, 39.915156), 12 );
Map2.enableScrollWheelZoom ();
</Script>
The code above can display two side-by-side maps on the same page. If you want to drag or scale the map on the left, the map on the right will also move and zoom, you only need to register the zoomend and moveend events for the map on the left. The Code is as follows:

 

[Html]
Map1.addEventListener ("zoomend", function (){
Map2.zoomTo (map1.getZoom ());
Map2.panTo (map1.getCenter ());
});
 
Map1.addEventListener ("moveend", function (){
Map2.zoomTo (map1.getZoom ());
Map2.panTo (map1.getCenter ());
});

Map1.addEventListener ("zoomend", function (){
Map2.zoomTo (map1.getZoom ());
Map2.panTo (map1.getCenter ());
});

Map1.addEventListener ("moveend", function (){
Map2.zoomTo (map1.getZoom ());
Map2.panTo (map1.getCenter ());
});
The complete code is as follows:


[Html]
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Script type = "text/javascript" src = "http://api.map.baidu.com/api? V = 1.4 "> </script>
<Title> map interface </title>
</Head>
<Style type = "text/css">
# MapDiv1 {height: 400px; width: 600px ;}
# MapDiv2 {height: 400px; width: 600px; left: 620px; top:-420px ;}
</Style>
<Body>
<Div id = "mapDiv1"> </div>
<Br/>
<Div id = "mapDiv2"> </div>
</Body>
</Html>
<Script type = "text/javascript">
// Map Initialization
Var map1 = new BMap. Map ("mapDiv1 ");
Map1.centerAndZoom (new BMap. Point (116.401394, 39.915156), 12 );
Map1.enableScrollWheelZoom ();
 
 
Var map2 = new BMap. Map ("mapDiv2", {mapType: BMAP_HYBRID_MAP}); // you can specify a satellite Map as a base Map.
Map2.centerAndZoom (new BMap. Point (116.401394, 39.915156), 12 );
Map2.enableScrollWheelZoom ();
 

Map1.addEventListener ("zoomend", function (){
Map2.zoomTo (map1.getZoom ());
Map2.panTo (map1.getCenter ());
});
 
Map1.addEventListener ("moveend", function (){
Map2.zoomTo (map1.getZoom ());
Map2.panTo (map1.getCenter ());
});
 
</Script>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Script type = "text/javascript" src = "http://api.map.baidu.com/api? V = 1.4 "> </script>
<Title> map interface </title>
</Head>
<Style type = "text/css">
# MapDiv1 {height: 400px; width: 600px ;}
# MapDiv2 {height: 400px; width: 600px; left: 620px; top:-420px ;}
</Style>
<Body>
<Div id = "mapDiv1"> </div>
<Br/>
<Div id = "mapDiv2"> </div>
</Body>
</Html>
<Script type = "text/javascript">
// Map Initialization
Var map1 = new BMap. Map ("mapDiv1 ");
Map1.centerAndZoom (new BMap. Point (116.401394, 39.915156), 12 );
Map1.enableScrollWheelZoom ();


Var map2 = new BMap. Map ("mapDiv2", {mapType: BMAP_HYBRID_MAP}); // you can specify a satellite Map as a base Map.
Map2.centerAndZoom (new BMap. Point (116.401394, 39.915156), 12 );
Map2.enableScrollWheelZoom ();

 
Map1.addEventListener ("zoomend", function (){
Map2.zoomTo (map1.getZoom ());
Map2.panTo (map1.getCenter ());
});

Map1.addEventListener ("moveend", function (){
Map2.zoomTo (map1.getZoom ());
Map2.panTo (map1.getCenter ());
});

</Script>

 

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.