Baidu map with Java code to make a map

Source: Internet
Author: User

Introduction of Baidu API, JS and map style

<script type= "Text/javascript" src= "http://api.map.baidu.com/api?v=1.4" ></script>
<script type = "Text/javascript" src= "Http://api.map.baidu.com/library/SearchInfoWindow/1.5/src/SearchInfoWindow_min.js" > </script>
<link rel= "stylesheet" href= "http://api.map.baidu.com/library/SearchInfoWindow/1.5/src/ Searchinfowindow_min.css "/>


Initialize Map

var map = new Bmap.map ("Popclsbox");//Create a map Window.map=map in the Baidu map container
	 ;
    Createmap ()///Create map
	function Createmap () {
	        map.centerandzoom ("Shanghai", 12);//define a central point coordinate
		Map.addcontrol (new Bmap.navigationcontrol ()); Add a Pan zoom control
		Map.addcontrol (New Bmap.scalecontrol ());//Add a scale control
		Map.addcontrol (New Bmap.overviewmapcontrol ( )); Add thumbnail map control
		map.enablescrollwheelzoom ();//enable wheel Zoom
		Map.addcontrol (New Bmap.maptypecontrol ({
			maptypes : [Bmap_normal_map, Bmap_hybrid_map]
		});		
	}

Add right menu to map

function Createmap () {
	        ditto
		... var ContextMenu = new Bmap.contextmenu ();
		var Txtmenuitem = [
				{
					text: ' Placed to the maximum level ',
					callback:function () {
						map.setzoom ()
					}
				},
				{
					text: ' View the country ',
					callback:function () {
						map.setzoom (4);
					}
				}];
		for (var i = 0; i < txtmenuitem.length i++) {
			Contextmenu.additem (new Bmap.menuitem (Txtmenuitem[i].text,
					t Xtmenuitem[i].callback));
			if (i = = 1 | | | i = = 3) {
				contextmenu.addseparator ();
			}
		}
		Map.addcontextmenu (ContextMenu);
	}


Automatically add coordinates when initializing

<%
        passes a maps collection to the page, showing
	if (Mapmodels!=null&&mapmodels.size () >0) {for
		(int i=0;i< Mapmodels.size (); i++) {
			mapmodels mapmodel=mapmodels.get (i);
			%>
			var point = new Bmap.point (<%=map.getx ()%>, <%=mapmodel.gety ()%>);//Initialize Map center
			var Marker <%=i%> = new Bmap.marker (point); Initialize the map tag
			map.addoverlay (marker<%=i%>);//Add marker <%} to the map
	%>


Add Tip Information window to coordinates

such as using Tip information window display, directly replace the above content, content in bold words to add tip content <% to the page to pass a maps collection, corresponding to show if (Mapmodels!=null&&mapmodels.size () >0
			{for (int i=0;i<mapmodels.size (); i++) {mapmodels mapmodel=mapmodels.get (i); %> var point = new Bmap.point (<%=map.getx ()%>, <%=mapmodel.gety ()%>);			
			Initialize the map center point <strong>var manage= ' Custom tip Information window display content ';			
			For example: Manage + = ' <p> address: <%=mapmodel.getaddress ()%></p> ';
			Create a Retrieval Information Window object var searchinfowindow<%=i%> = null;       searchinfowindow<%=i%> = new Bmaplib.searchinfowindow (map, manage, {title: "<%=mapmodel.getname ()%>", Title, where you can customize HTML content width:458,//Width height:150,//Height enableautopan:true ,//Automatic translation Enablesendtophone:false, searchtypes: []});</strong> var marker<%=i%> = new BM Ap. Marker (point); Initialize map tag <strong>marker<%=i%>.addeventlistener ("Click", Function (e) {searchinfowindow<%=i%>.open (marker<%=i%>); });</strong> Map.addoverlay (marker<%=i%>); Add marker <%} to the map%>



This article is mainly for the display of the summary page, does not include background input coordinates, later will gradually add, thank you.

The following part is not aimed at mapabc, also not high praise Baidu Map, is entirely personal experience, if there are objections can be commented, please understand.

Experience Summary: I use the map in the process, the first use of the MAPABC and then into the Baidu map, the main reason is because the coordinates in the input, through the search and other ways to find an address, mapabc for the content of the search is too broad or can be said that the lack of detailed search, thus unable to accurately find most So change from the use of Baidu map, the map in the search for the night, its data precision is high, you can quickly find an address to accurately locate. Another is to consider the Baidu map for the current industry, map do or want to when you can, so personally recommended into the use of maps, as far as possible use of Baidu map.


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.