Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="container"></div> 
<div id="box">
	<input type="text" id="city" placeholder="请输入城市...">
	<input type="button" value="搜索" id="btn">
</div>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.3&key=278b7b8b4728ba302b7e566fc2a97b36"></script>
              
            
!

CSS

              
                #container {width:500px; height:500px; margin:100px auto;}  
			.menu{
				width:100px;
				box-shadow: 0 0 5px #000;
				margin:auto;
				background:#fff;
			}
			.menu ul li{
				list-style:none;
				line-height:30px;
				text-align:center;
				cursor:pointer;
			}
			#box{
				width:400px;
				height:40px;
				position:absolute;
				top:150px;
				left:50%;
				margin-left:-200px;
				background:#fff;
				box-shadow:0 0 10px #000;
			}
			input{
				height:38px;
				width:300px;
				border:none;
				outline:none;
			}
			#btn{
				width:80px;
			}
              
            
!

JS

              
                var btn = document.getElementById("btn");
var city = document.getElementById("city");
var map = new AMap.Map('container');
var toolBar,mouseTool,contextMenu;
//在地图中添加操作toolBar插件、mouseTool插件
map.plugin(["AMap.ToolBar","AMap.MouseTool"],function(){
	toolBar = new AMap.ToolBar();
	map.addControl(toolBar);
	mouseTool = new AMap.MouseTool(map);
});
var menuContext = document.createElement("div");
menuContext.innerHTML = "<div class=menu><ul><li onclick='zoomMenu(0)'>缩小</li><li onclick='zoomMenu(1)'>放大</li><li onclick='distanceMeasureMenu()'>距离量测</li><li onclick = 'addMarkerMenu()'>添加标记</li></ul></div>";
//创建一个自定义的右键菜单
contextMenu = new AMap.ContextMenu({isCustom:true,content:menuContext});
//给地图绑鼠标右键功能弹出右键菜单
AMap.event.addListener(map,"rightclick",function(e){
	contextMenu.open(map,e.lnglat);//e.lnglat鼠标点击的经纬度
	contextMenuPosition = e.lnglat;
})
//右键菜单缩放地图
function zoomMenu(n){
	if(n === 0){map.zoomOut();}
	if(n === 1){map.zoomIn();}
	contextMenu.close();
}
contextMenu.close();
//测量距离功能
function distanceMeasureMenu(){
	mouseTool.rule();
	contextMenu.close();
}
//添加标注功能
function addMarkerMenu(){
	mouseTool.close();
	var marker = new AMap.Marker({
		map: map,
		position: contextMenuPosition, //基点位置
		offset: {x:-5,y:-10} //相对于基点位置
	});
	contextMenu.close();
}
//搜索城市
btn.onclick = function(){
	var val = city.value;
	map.setCity(val);
}
              
            
!
999px

Console