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>