百度地图api的使用
参考文档
http://lbsyun.baidu.com/jsdemo.htm#i1_6
http://lbsyun.baidu.com/index.php?title=jspopular
获取百度地图api的密匙
简单实例
<!DOCTYPE html>
<!-- saved from url=(0040)chrome-search://local-ntp/local-ntp.html -->
<html><!-- Copyright 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. --><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="google" value="notranslate">
<meta name="referrer" content="strict-origin">
</head>
<!-- Remember to update the test HTML files in chrome/test/data/local_ntp/
whenever making changes to this file.-->
<body class="non-google-page inited" style="background: rgb(255, 255, 255);">
<script src="jquery.js"></script>
<!--百度地图 -->
<style type="text/css">
body, html{width: 100%;height: 100%;margin:0;font-family:"微软雅黑";font-size:14px;}
#l-map{height:300px;width:100%;}
#r-result{width:100%;}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你的密匙"></script>
<table>
<tr>
<td style="width:75px;text-align: right;padding-top: 13px;" id="r-result">请输入地址:</td>
<td><input type="text" name="ADDRESS" id="ADDRESS" size="20" value="" style="width:98%;" /></div></td>
</tr>
<tr><td></td>
<td colspan="10"><div id="l-map"></div></td>
<div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
</tr>
<tr><td style="width:75px;text-align: right;padding-top: 13px;">纬度:</td>
<td><input type="text" name="X_LOCATION" id="X_LOCATION" value="" maxlength="50" placeholder="这里是纬度" title="名称" style="width:98%;"/></td>
<td style="width:75px;text-align: right;padding-top: 13px;">经度:</td>
<td><input type="text" name="Y_LOCATION" id="Y_LOCATION" value="" maxlength="50" placeholder="这里是经度" title="名称" style="width:98%;"/></td></tr>
</table>
<script type="text/javascript">
// 百度地图API功能
function G(id) {
return document.getElementById(id);
}
var map = new BMap.Map("l-map");
//设置经纬度
var point = new BMap.Point(116.404, 39.915);
map.centerAndZoom(point,18); // 初始化地图,设置城市和地图级别。
var marker = new BMap.Marker(point); // 创建点
map.addOverlay(marker);
var ac = new BMap.Autocomplete( //建立一个自动完成的对象
{"input" : "ADDRESS"
,"location" : map
});
//设置默认地址名称
ac.setInputValue("");
//map.addEventListener("tilesloaded",function(){$("#ADDRESS").val("${pd.ADDRESS }")});
ac.addEventListener("onhighlight", function(e) { //鼠标放在下拉列表上的事件
var str = "";
var _value = e.fromitem.value;
var value = "";
if (e.fromitem.index > -1) {
value = _value.province + _value.city + _value.district + _value.street + _value.business;
}
str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
value = "";
if (e.toitem.index > -1) {
_value = e.toitem.value;
value = _value.province + _value.city + _value.district + _value.street + _value.business;
}
str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
G("searchResultPanel").innerHTML = str;
});
var myValue;
ac.addEventListener("onconfirm", function(e) { //鼠标点击下拉列表后的事件
var _value = e.item.value;
myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
setPlace();
});
function setPlace(){
map.clearOverlays(); //清除地图上所有覆盖物
function myFun(){
var pp = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
$("#Y_LOCATION").val(local.getResults().getPoi(0).point.lng);
$("#X_LOCATION").val(local.getResults().getPoi(0).point.lat);
map.centerAndZoom(pp, 18);
map.addOverlay(new BMap.Marker(pp)); //添加标注
}
var local = new BMap.LocalSearch(map, { //智能搜索
onSearchComplete: myFun
});
local.search(myValue);
}
//单击获取点击的经纬度
map.addEventListener("click",function(e){
$("#X_LOCATION").val(e.point.lat);
$("#Y_LOCATION").val(e.point.lng);
});
map.enableScrollWheelZoom(); //启用滚轮放大缩小,默认禁用
map.enableContinuousZoom(); //启用地图惯性拖拽,默认禁用
</script>
</body></html>