根据地址描述获得坐标
百度地图API提供Geocoder类进行地址解析,您可以通过Geocoder.getPoint()方法来将一段地址描述转换为一个坐标。
// 创建地址解析器实例
var myGeo = new BMap.Geocoder();
// 将地址解析结果显示在地图上,并调整地图视野
myGeo.getPoint(“南京市紫峰大厦”, function(point){
if (point) {
//经度
var pointx = point.lng;
//纬度
var pointy = point.lat;
showmap(jd, wd, addr);
}
}, “南京”);
function showmap(jd, wd, addr) {
var map = new BMap.Map('map');
var poi = new BMap.Point(jd, wd);
map.centerAndZoom(poi, 19);
map.enableScrollWheelZoom();
Propertys(map);
showmark(map, poi, addr);
}
function showmark(map, poi, addr) {
var content = '<div style="margin:0;line-height:20px;padding:2px;">地铁2号线到雨润大街站</div>';
//创建检索信息窗口对象
var searchInfoWindow = null;
searchInfoWindow = new BMapLib.SearchInfoWindow(map, content, {
title: addr, //标题
width: 100, //宽度
height: 48, //高度
panel: "panel", //检索结果面板
enableAutoPan: true, //自动平移
enableSendToPhone: false,
searchTypes: [
BMAPLIB_TAB_SEARCH, //周边检索
BMAPLIB_TAB_TO_HERE, //到这里去
BMAPLIB_TAB_FROM_HERE //从这里出发
]
});
var marker = new BMap.Marker(poi); //创建marker对象
marker.setAnimation(BMAP_ANIMATION_BOUNCE);
searchInfoWindow.open(marker);
marker.addEventListener("click", function (e) {
searchInfoWindow.open(marker);
})
map.addOverlay(marker); //在地图中添加marker
var opts = {
position: poi, // 指定文本标注所在的地理位置
offset: new BMap.Size(3, 3) //设置文本偏移量
}
var label = new BMap.Label("南京保利香槟", opts); // 创建文本标注对象
label.setStyle({
backgroundColor: "#fff",
borderColor: "#808080",
color: "#333",
fontSize: "12px",
height: "20px",
paddingLeft: "10px",
paddingRight: "10px",
lineHeight: "20px",
fontFamily: "微软雅黑"
});
map.addOverlay(label);
}
var isPanelShow = false;
//显示结果面板动作
$("showPanelBtn").onclick = function () {
if (isPanelShow == false) {
isPanelShow = true;
$("showPanelBtn").style.right = "300px";
$("panelWrap").style.width = "300px";
$("map").style.marginRight = "300px";
$("showPanelBtn").innerHTML = "隐藏检索结果面板<br/>>";
} else {
isPanelShow = false;
$("showPanelBtn").style.right = "0px";
$("panelWrap").style.width = "0px";
$("map").style.marginRight = "0px";
$("showPanelBtn").innerHTML = "显示检索结果面板<br/><";
}
}
function Propertys(maps){
//explain:三维地图加载百度内部js出错,暂时取消三维地图控件,author:fpf
maps.addControl(new BMap.MapTypeControl({mapTypes: [BMAP_NORMAL_MAP, BMAP_SATELLITE_MAP, BMAP_HYBRID_MAP]}));
maps.addControl(new BMap.ScaleControl());
maps.addControl(new BMap.NavigationControl());
maps.enableScrollWheelZoom();
maps.addControl(new BMap.OverviewMapControl());
}