百度地图API 一些简单方法
时间:2017-01-17
-
<!--div width="300px">请输入:<input type="text" id="suggestId" size="20" value="" style="width:150px;" /><input type="button" value="submit" id="submit" onClick="getAddress()"></div>
-
<!--div id="result" style=" width:400px; float:left"></div-->
-
<div style="width:800px;height:600px;border:1px solid gray; float:right" id="container"></div>
-
-
-
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script>
-
<script type="text/javascript">
-
-
var search="frm_street";
-
var local="广汉市";
-
var lng="104.290493";
-
var lat="30.981528";
-
var spoi = new BMap.Point(lng,lat);
-
-
var map = new BMap.Map("container");
-
var point = new BMap.Point(lng,lat);
-
map.centerAndZoom(point,15);
-
map.enableScrollWheelZoom();
-
-
-
-
function G(id) {
-
return document.getElementById(id);
-
}
-
function GV(id){
-
return document.getElementById(id).value;
-
}
-
-
-
-
function add_flag0(){
-
var localmarker=new BMap.Marker(new BMap.Point(lng,lat));
-
map.addOverlay(localmarker);
-
}
-
-
-
function add_flag1(point){
-
map.centerAndZoom(point, 15);
-
map.clearOverlays();
-
add_flag0();
-
var newmarker=new BMap.Marker(new BMap.Point(point.lng,point.lat));
-
map.addOverlay(newmarker);
-
newmarker.enableDragging();
-
driveline(spoi,point);
-
longAndtime(spoi,point);
-
-
document.edit.lng.value=point.lng;
-
document.edit.lat.value=point.lat;
-
}
-
function add_flag2(){
-
-
}
-
-
function getAddress(){
-
get_point(GV(search));
-
}
-
-
-
-
function get_point(address){
-
var myGeo = new BMap.Geocoder();
-
myGeo.getPoint(address, function(point){
-
if (point) {
-
add_flag1(point);
-
}else{
-
alert("地址无法找到!");
-
}
-
},local);
-
}
-
-
-
-
function get_address(e){
-
var gc = new BMap.Geocoder();
-
var pt = e.point;
-
var result;
-
add_flag1(pt);
-
gc.getLocation(pt, function(rs){
-
var addComp = rs.addressComponents;
-
if (addComp.street + addComp.streetNumber=="")
-
{
-
result="地址无法找到!";
-
}
-
else{
-
result=addComp.street + addComp.streetNumber;
-
}
-
document.edit.frm_street.value=result;
-
return result;
-
-
});
-
-
}
-
-
-
-
-
var ac = new BMap.Autocomplete( {"input" : search,"location" : local} );
-
ac.addEventListener("onconfirm", function(e) {
-
var _value = e.item.value;
-
myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
-
setPlace();
-
});
-
-
function setPlace(){
-
-
function myFun(){
-
var pp = ls.getResults().getPoi(0).point;
-
add_flag1(pp);
-
}
-
var ls = new BMap.LocalSearch(local, {
-
onSearchComplete: myFun
-
});
-
ls.search(myValue);
-
}
-
-
-
-
-
function driveline(myP1,myP2){
-
var driving = new BMap.DrivingRoute(map);
-
driving.search(myP1, myP2);
-
driving.setSearchCompleteCallback(function(){
-
var pts = driving.getResults().getPlan(0).getRoute(0).getPath();
-
var polyline = new BMap.Polyline(pts);
-
map.addOverlay(polyline);
-
-
-
var lab1 = new BMap.Label("起点",{position:myP1});
-
var lab2 = new BMap.Label("终点",{position:myP2});
-
map.addOverlay(lab1);
-
map.addOverlay(lab2);
-
setTimeout(function(){
-
map.setViewport([myP1,myP2]);
-
},1000);
-
});}
-
-
-
-
function longAndtime(spoi,point){
-
var output="";
-
var searchComplete = function (results){
-
if (transit.getStatus() != BMAP_STATUS_SUCCESS){ return ;}
-
var plan = results.getPlan(0);
-
output += plan.getDuration(true) + "\n";
-
output += "总路程为:" ;
-
output += plan.getDistance(true) + "\n";
-
}
-
var transit = new BMap.DrivingRoute(map, {renderOptions: {map: map,panel:"result"},
-
-
-
-
-
});
-
transit.search(spoi, point);
-
}
-
-
-
-
-
map.addEventListener("click", function(e){
-
get_address(e);
-
-
-
});
-
-
-
newmarker.addEventListener("dragend", function(e){
-
-
});
-
-
</script>
-
<!--div style="width:800px;height:600px;border:1px solid gray; float:right" id="container"></div-->
-
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script>
-
<script type="text/javascript">
-
-
var local="广汉市";
-
var lng="104.290493";
-
var lat="30.981528";
-
var spoi = new BMap.Point(lng,lat);
-
var map = new BMap.Map("map");
-
var point = new BMap.Point(lng,lat);
-
map.centerAndZoom(point,15);
-
map.enableScrollWheelZoom();
-
-
var map = new BMap.Map("map");
-
var point = new BMap.Point(104.290493,30.981528);
-
map.centerAndZoom(point,15);
-
map.enableScrollWheelZoom();
-
creat("m4043",104.290493,30.981528,"2012年4月30日","汉口路28号","151038432334");
-
-
-
var infoWindow;
-
-
-
function creat(id,lng,lat,time,address,phone){
-
-
-
-
var message = "<table width='100%' border='0' cellspacing='2' cellpadding='5'>"+
-
"<tr class='even'>"+
-
"<td><div align='center'>呼救时间</div></td>"+
-
"<td><div align='center'>"+time+"</div></td>"+
-
"</tr>"+
-
"<tr class='even'>"+
-
"<td><div align='center'>事故地址</div></td>"+
-
"<td><div align='center'>"+address+"</div></td>"+
-
"</tr>"+
-
"<tr class='even'>"+
-
"<td><div align='center'>呼救电话</div></td>"+
-
"<td><div align='center'>"+phone+"</div></td>"+
-
"</tr>"+
-
"<tr class='odd'>"+
-
"<td colspan='2'><div align='center'>分派 细节 编辑</div></td>"+
-
"</tr>"+
-
"<tr class='odd'>"+
-
"<td colspan='2'><div align='center'>跟踪 打印派车单</div></td>"+
-
"</tr>"+
-
"</table>";
-
-
-
var myIcon = new BMap.Icon("./markers/white.png",new BMap.Size(28, 37));
-
-
-
-
id = new BMap.Marker(new BMap.Point(lng,lat),{icon: myIcon});
-
map.addOverlay(id);
-
id.addEventListener("click", function(e){
-
this.openInfoWindow(infoWindow);
-
});
-
-
infoWindow = new BMap.InfoWindow(message);
-
-
}
-
-
-
function click_list(point){
-
map.openInfoWindow(infoWindow,point);
-
}
-
-
</script>