您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
百度地图javascript接口开发-地址的解析与反解析
发布时间:2016-11-20 14:28:46编辑:雪饮阅读()
地址解析示例:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style type="text/css">
html{height:100%;}
body{height:100%;margin:0px;padding:0px;}
#container{
clear:both;
height:100%;
width:100%;
}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=s9vYSVwPjtPFv00vuxKzoNxD"></script>
<script type="text/javascript">
BMap.Point.prototype.toString=function(){
return "当前的经度="+this.lng+",纬度="+this.lat;
}
BMap.Pixel.prototype.toString=function(){
return "当前点坐标x="+this.x+",y="+this.y;
}
var map;
function InitMap(){
map=new BMap.Map("container");
var point=new BMap.Point(104.06,30.67);//成都的经纬度点
map.centerAndZoom(point,12);
map.enableScrollWheelZoom();
var geocoder=new BMap.Geocoder();
//正向地址解析(给定地址名称解析地理位置)
geocoder.getPoint("四川师范大学",function(point){
if(point!=null){
console.log(point);
map.setCenter(point);//重新将中心点改变为获得的指导地点“四川师范大学”的位置
var marker=new BMap.Marker(point);
map.addOverlay(marker);
}
},"成都市");
//正向解析完成
map.addEventListener("click",function(event){
//console.log(event.point);
});
}
</script>
</head>
<body onLoad="InitMap();">
<div id="container"></div>
<div id="result"></div>
</body>
</html>
地址反解析示例:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style type="text/css">
html{height:100%;}
body{height:100%;margin:0px;padding:0px;}
#container{
clear:both;
height:100%;
width:100%;
}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=s9vYSVwPjtPFv00vuxKzoNxD"></script>
<script type="text/javascript">
BMap.Point.prototype.toString=function(){
return "当前的经度="+this.lng+",纬度="+this.lat;
}
BMap.Pixel.prototype.toString=function(){
return "当前点坐标x="+this.x+",y="+this.y;
}
var map;
function InitMap(){
map=new BMap.Map("container");
var point=new BMap.Point(104.06,30.67);//成都的经纬度点
map.centerAndZoom(point,12);
map.enableScrollWheelZoom();
var locationOptions={
poiRadius:50,
numPois:8
};
var point=new BMap.Point(104.08243,30.659518);
var marker=new BMap.Marker(point);
map.addOverlay(marker);
map.setCenter(point);
var geocoder=new BMap.Geocoder();
geocoder.getLocation(point,
function(geocoderResult){
if(geocoderResult!=null){
console.log(geocoderResult);
}
},
locationOptions);
map.addEventListener("click",function(event){
//console.log(event.point);
});
}
</script>
</head>
<body onLoad="InitMap();">
<div id="container"></div>
<div id="result"></div>
</body>
</html>
关键字词:百度地图api,javascript