利用百度地圖作的定位,獲取位置和經緯度

有時候咱們在使用一個新的技術知識技能的時候,看問題有時不是咱們想要的功能,看了還不是很理解,javascript

看了文檔,查了資料後本身總結了一下,但願有幫助html

 

<!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>點擊地圖獲取地址和經緯度map,address,lng,lat</title>
<meta name="robots" content="noindex, nofollow">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<!-- 將百度地圖API引入,設置好本身的key -->
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=ltIDt5gWj2BFwDha2OyIFXXqONgzK42s"></script>
</head>
<body>

<div class="main-div">
<form method="post" action="" name="theForm" enctype="multipart/form-data" onsubmit="return validate()">
<table cellspacing="1" cellpadding="3" width="100%">
<tr>
<td class="label">經度</td>
<td><input type="text" name="lng" id="lng" value=""/>
</td>
</tr>
<tr>
<td class="label">緯度</td>
<td><input type="text" name="lat" id="lat" value=""/>
</td>
</tr>
<tr>
<td class="label">地址</td>
<td>
<input type='text' value='' name='sever_add' id='sever_add' readonly>
<input type='button' value='點擊顯示地圖獲取地址經緯度' id='open'>
</td>
</tr>
</table>
</form>
<div id='allmap' style='width: 50%; height: 50%; position: absolute; display: block'></div>
</div>
</body>
</html>
<script type="text/javascript">
function validate() {
var sever_add = document.getElementsByName('sever_add')[0].value;
if (isNull(sever_add)) {
alert('請選擇地址');
return false;
}
return true;
}

//判斷是不是空
function isNull(a) {
return (a == '' || typeof(a) == 'undefined' || a == null) ? true : false;
}

document.getElementById('open').onclick = function () {
if (document.getElementById('allmap').style.display == 'none') {
document.getElementById('allmap').style.display = 'block';
} else {
// document.getElementById('allmap').style.display = 'none';
}
}

var map = new BMap.Map("allmap");
var geoc = new BMap.Geocoder(); //地址解析對象
var markersArray = [];
var geolocation = new BMap.Geolocation();


var point = new BMap.Point(116.404, 39.915);
map.centerAndZoom(point, 12); // 中心點
geolocation.getCurrentPosition(function (r) {
if (this.getStatus() == BMAP_STATUS_SUCCESS) {
var mk = new BMap.Marker(r.point);
map.addOverlay(mk);
map.panTo(r.point);
map.enableScrollWheelZoom(true);
}
else {
alert('failed' + this.getStatus());
}
}, {enableHighAccuracy: true})
map.addEventListener("click", showInfo);


//清除標識
function clearOverlays() {
if (markersArray) {
for (i in markersArray) {
map.removeOverlay(markersArray[i])
}
}
}
//地圖上標註
function addMarker(point) {
var marker = new BMap.Marker(point);
markersArray.push(marker);
clearOverlays();
map.addOverlay(marker);
}
//點擊地圖時間處理
function showInfo(e) {
document.getElementById('lng').value = e.point.lng;
document.getElementById('lat').value = e.point.lat;
geoc.getLocation(e.point, function (rs) {
var addComp = rs.addressComponents;
var address = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
if (confirm("肯定要地址是" + address + "?")) {
document.getElementById('allmap').style.display = 'none';
document.getElementById('sever_add').value = address;
}
});
addMarker(e.point);
}
</script>java

相關文章
相關標籤/搜索