如何用百度地圖api地圖上顯示自己位置以及資料庫裡的位置
先定位自己位置
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=dDOKBdXFCG7TnyFvMScTlOmI"></script>
<title>定位您的位置</title>
</head>
<body>
<h4>您當前位置:</h4>
<div id="allmap" style="width: 50%;height: 500px;"></div>
<form method="post" action="dingweijw.php" enctype="multipart/form-data">
<input type="hidden" name="place" value="" id="testdiv">
<input type="hidden" name="jw" value="" id="testdivip">
<input type="submit" value="獲取周圍文章位置">
</form>
<center><a href="front_index.php"><h5>返回<h5></a></center>
<script type="text/javascript">
// 百度地圖API功能
var map = new BMap.Map("allmap");
var point = new BMap.Point(108.95,34.27);
map.centerAndZoom(point,12);
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(r){console.log(r.point)
if(this.getStatus() == BMAP_STATUS_SUCCESS){
var mk = new BMap.Marker(r.point);
map.addOverlay(mk);//標出所在地
map.panTo(r.point);//地圖中心移動
//alert('您的位置:'+r.point.lng+','+r.point.lat);
var point = new BMap.Point(r.point.lng,r.point.lat);//用所定位的經緯度查詢所在地省市街道等資訊
var gc = new BMap.Geocoder();
gc.getLocation(point, function(rs){
var addComp = rs.addressComponents; console.log(rs.address);//地址資訊
alert(rs.address);//彈出所在地址
var testdiv = document.getElementById("testdiv");
testdiv.value=rs.address;
var testdivip = document.getElementById("testdivip");
testdivip.value=r.point.lng+','+r.point.lat;
});
}else {
alert('failed'+this.getStatus());
}
},{enableHighAccuracy: true})
</script>
</body>
</html>
用post傳到php檔案
php引進下面這個h5 頁面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>自己位置和周圍文章位置</title>
<link href="style/demo.css" rel="stylesheet" type="text/css" />
<script src="scripts/jquery-1.9.1.js" type="text/javascript"></script>
<script src="scripts/demo.js" type="text/javascript"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=IDvNBsejl9oqMbPF316iKsXR"></script>
<script type="text/javascript" src="http://api.map.baidu.com/library/SearchInfoWindow/1.5/src/SearchInfoWindow_min.js"></script>
<link rel="stylesheet" href="http://api.map.baidu.com/library/SearchInfoWindow/1.5/src/SearchInfoWindow_min.css" />
</head>
<body>
<div class="demo_main">
<fieldset class="demo_title">
多點標註中》》》》
</fieldset>
<fieldset class="demo_content">
<div style="min-height:300px; width: 100%;" id="map">
</div>
<center><a href="front_index.php"><h5>返回<h5></a></center>
<script type="text/javascript">
var markerArr = [
<?php foreach ($data as $v) { ?>
{ title: "名稱:<?php echo $v['place']; ?>", point: "<?php echo $v['jw']; ?>", address: "<?php echo $v['place']; ?>", tel: "12306" },
<?php } ?>
{ title: "名稱:你的位置<?php echo $place; ?>", point: "<?php echo $jw; ?>", address: "<?php echo $place; ?>", tel: "XXX" }
];
function map_init() {
var map = new BMap.Map("map"); // 建立Map例項
var point = new BMap.Point(<?php echo $jw; ?>); //地圖中心點,廣州市
map.centerAndZoom(point, 13); // 初始化地圖,設定中心點座標和地圖級別。
map.enableScrollWheelZoom(true); //啟用滾輪放大縮小
//地圖、衛星、混合模式切換
map.addControl(new BMap.MapTypeControl({mapTypes: [BMAP_NORMAL_MAP, BMAP_SATELLITE_MAP, BMAP_HYBRID_MAP]}));
//向地圖中新增縮放控制元件
var ctrlNav = new window.BMap.NavigationControl({
anchor: BMAP_ANCHOR_TOP_LEFT,
type: BMAP_NAVIGATION_CONTROL_LARGE
});
map.addControl(ctrlNav);
//向地圖中新增縮圖控制元件
var ctrlOve = new window.BMap.OverviewMapControl({
anchor: BMAP_ANCHOR_BOTTOM_RIGHT,
isOpen: 1
});
map.addControl(ctrlOve);
//向地圖中新增比例尺控制元件
var ctrlSca = new window.BMap.ScaleControl({
anchor: BMAP_ANCHOR_BOTTOM_LEFT
});
map.addControl(ctrlSca);
var point = new Array(); //存放標註點經緯資訊的陣列
var marker = new Array(); //存放標註點物件的陣列
var info = new Array(); //存放提示資訊視窗物件的陣列
var searchInfoWindow =new Array();//存放檢索資訊視窗物件的陣列
for (var i = 0; i < markerArr.length; i++) {
var p0 = markerArr[i].point.split(",")[0]; //
var p1 = markerArr[i].point.split(",")[1]; //按照原陣列的point格式將地圖點座標的經緯度分別提出來
point[i] = new window.BMap.Point(p0, p1); //迴圈生成新的地圖點
marker[i] = new window.BMap.Marker(point[i]); //按照地圖點座標生成標記
map.addOverlay(marker[i]);
marker[i].setAnimation(BMAP_ANIMATION_BOUNCE); //跳動的動畫
//顯示marker的title,marker多的話可以註釋掉
var label = new window.BMap.Label(markerArr[i].title, { offset: new window.BMap.Size(20, -10) });
marker[i].setLabel(label);
// 建立資訊視窗物件
info[i] = "<p style=’font-size:12px;lineheight:1.8em;’>" + "</br>地址:" + markerArr[i].address + "</br> 電話:" + markerArr[i].tel + "</br></p>";
//建立百度樣式檢索資訊視窗物件
searchInfoWindow[i] = new BMapLib.SearchInfoWindow(map, info[i], {
title : markerArr[i].title, //標題
width : 290, //寬度
height : 55, //高度
panel : "panel", //檢索結果面板
enableAutoPan : true, //自動平移
searchTypes :[
BMAPLIB_TAB_SEARCH, //周邊檢索
BMAPLIB_TAB_TO_HERE, //到這裡去
BMAPLIB_TAB_FROM_HERE //從這裡出發
]
});
//新增點選事件
marker[i].addEventListener("click",
(function(k){
// js 閉包
return function(){
//將被點選marker置為中心
//map.centerAndZoom(point[k], 18);
//在marker上開啟檢索資訊視窗
searchInfoWindow[k].open(marker[k]);
}
})(i)
);
}
}
//非同步呼叫百度js
function map_load() {
var load = document.createElement("script");
load.src = "http://api.map.baidu.com/api?v=2.0&ak=IDvNBsejl9oqMbPF316iKsXR&callback=map_init";
document.body.appendChild(load);
}
window.onload = map_load;
</script>
</fieldset>
</div>
</body>
</html>
然後不要搞錯經緯度,就可以做到一張圖上顯示自己位置和他人位置