<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>区域内搜索 </title>
<style type="text/css">
body, html
{
width: 100%;
height: 100%;
overflow: hidden;
margin: 0;
}
#l-map
{
height: 100%;
width: 75%;
float: left;
border-right: 2px solid #bcbcbc;
}
#r-result
{
height: 100%;
width: 24%;
float: left;
overflow: auto;
}
</style>
<script src="/js/jquery.js" type="text/javascript"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.5&ak=abcd"></script>
</head>
<body>
<div id="l-map">
</div>
<div id="r-result">
</div>
</body>
</html>
<script type="text/javascript">
//地图
var map = new BMap.Map("l-map");
var mPoint = new BMap.Point(116.404, 39.915);
$.ajax({
type:"post",
url:"*.aspx?action=getdata",
success:function(msg){
mPoint = new BMap.Point(msg.split(',')[0],msg.split(',')[1]);
}
});
setTimeout(function(){
map.enableScrollWheelZoom();
map.centerAndZoom(mPoint,15);
var circle = new BMap.Circle(mPoint,1000,{fillColor:"blue", strokeWeight: 1 ,fillOpacity: 0.3, strokeOpacity: 0.3});
map.addOverlay(circle);
var options = { onSearchComplete: function(results){ if (local.getStatus() == BMAP_STATUS_SUCCESS){
var s=[];
for (var i = 0; i < results.getCurrentNumPois(); i ++)
{
s.push((i+1)+": "+results.getPoi(i).title + ", " + results.getPoi(i).address+"</br>");
}
document.getElementById("r-result").innerHTML = s.join("");
} } };
//1.将结果显示右边的标签中
var local = new BMap.LocalSearch(map, options);
//2.将结果显示在地图的区域内
// var local = new BMap.LocalSearch(map, {renderOptions: {map: map, autoViewport: false}});
var bounds = getSquareBounds(circle.getCenter(),circle.getRadius());
local.searchInBounds("公交车站",bounds);
},1000);
function getSquareBounds(centerPoi,r){
var a = Math.sqrt(2) * r; //正方形边长
mPoi = getMecator(centerPoi);
var x0 = mPoi.x, y0 = mPoi.y;
var x1 = x0 + a / 2 , y1 = y0 + a / 2;//东北点
var x2 = x0 - a / 2 , y2 = y0 - a / 2;//西南点
var ne = getPoi(new BMap.Pixel(x1, y1)), sw = getPoi(new BMap.Pixel(x2, y2));
return new BMap.Bounds(sw, ne);
}
function getMecator(poi){
return map.getMapType().getProjection().lngLatToPoint(poi);
}
function getPoi(mecator){
return map.getMapType().getProjection().pointToLngLat(mecator);
}
</script>
<head>
<title>区域内搜索 </title>
<style type="text/css">
body, html
{
width: 100%;
height: 100%;
overflow: hidden;
margin: 0;
}
#l-map
{
height: 100%;
width: 75%;
float: left;
border-right: 2px solid #bcbcbc;
}
#r-result
{
height: 100%;
width: 24%;
float: left;
overflow: auto;
}
</style>
<script src="/js/jquery.js" type="text/javascript"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.5&ak=abcd"></script>
</head>
<body>
<div id="l-map">
</div>
<div id="r-result">
</div>
</body>
</html>
<script type="text/javascript">
//地图
var map = new BMap.Map("l-map");
var mPoint = new BMap.Point(116.404, 39.915);
$.ajax({
type:"post",
url:"*.aspx?action=getdata",
success:function(msg){
mPoint = new BMap.Point(msg.split(',')[0],msg.split(',')[1]);
}
});
setTimeout(function(){
map.enableScrollWheelZoom();
map.centerAndZoom(mPoint,15);
var circle = new BMap.Circle(mPoint,1000,{fillColor:"blue", strokeWeight: 1 ,fillOpacity: 0.3, strokeOpacity: 0.3});
map.addOverlay(circle);
var options = { onSearchComplete: function(results){ if (local.getStatus() == BMAP_STATUS_SUCCESS){
var s=[];
for (var i = 0; i < results.getCurrentNumPois(); i ++)
{
s.push((i+1)+": "+results.getPoi(i).title + ", " + results.getPoi(i).address+"</br>");
}
document.getElementById("r-result").innerHTML = s.join("");
} } };
//1.将结果显示右边的标签中
var local = new BMap.LocalSearch(map, options);
//2.将结果显示在地图的区域内
// var local = new BMap.LocalSearch(map, {renderOptions: {map: map, autoViewport: false}});
var bounds = getSquareBounds(circle.getCenter(),circle.getRadius());
local.searchInBounds("公交车站",bounds);
},1000);
function getSquareBounds(centerPoi,r){
var a = Math.sqrt(2) * r; //正方形边长
mPoi = getMecator(centerPoi);
var x0 = mPoi.x, y0 = mPoi.y;
var x1 = x0 + a / 2 , y1 = y0 + a / 2;//东北点
var x2 = x0 - a / 2 , y2 = y0 - a / 2;//西南点
var ne = getPoi(new BMap.Pixel(x1, y1)), sw = getPoi(new BMap.Pixel(x2, y2));
return new BMap.Bounds(sw, ne);
}
function getMecator(poi){
return map.getMapType().getProjection().lngLatToPoint(poi);
}
function getPoi(mecator){
return map.getMapType().getProjection().pointToLngLat(mecator);
}
</script>