下面的HTML代码就是实例完整代码,如果你需要本项目完整实例代码,下载链接在文章最底部
网页中自动获取经纬度值并在地图中显示当前位置,可在地图中进行拖动选择当前位置并输出经纬度,这是项目实例代码,可参考用于你的项目中。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>经纬度获取示例代码-520YM.NET</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
/>
<meta content="yes" name="apple-mobile-web-app-capable" />
<meta content="yes" name="apple-touch-fullscreen" />
<meta content="telephone=no,email=no" name="format-detection" />
<meta
name="App-Config"
content="fullscreen=yes,useHistoryState=yes,transition=yes"
/>
<link rel="stylesheet" type="text/css" href="static/css/scs.min.css" />
<link rel="stylesheet" type="text/css" href="static/css/main.css" />
<style type="text/css">
#mapcontainer {
width: 100%;
height: 300px;
}
a[title="到百度地图查看此区域"] {
display: none;
}
span[_cid="1"] {
display: none;
}
</style>
</head>
<body>
<section>
<div class="main cc">
<h3 class="indextt1">获取当前位置经纬度并在地图中显示</h3>
<ul class="nytxt1 cc">
<form enctype="multipart/form-data">
<li>
<input
class="nytxt1input1"
id="coordinate"
name="info[coordinate]"
type="text"
readonly=""
value="位置经纬度:"
/>
</li>
<li>
<div>请在下方地图选择您的位置</div>
<div id="mapcontainer"></div>
</li>
<li>
<input
class="nytxt1btn1"
id="btnsumttj"
name="dosave"
type="button"
onclick="check_message()"
value="集源码"
/>
</li>
</form>
</ul>
</div>
</section>
<script src="static/js/public.js"></script><!--这个js是用于页面样式用途,无关紧要-->
<script src="http://api.map.baidu.com/api?v=3.0&ak=这里填写你的百度地图ak密钥"></script>
<script>
var map = new BMap.Map("mapcontainer");
var point = new BMap.Point(116.404, 39.915);
map.centerAndZoom(point, 15);
var geolocation = new BMap.Geolocation();
var marker = new BMap.Marker(new BMap.Point(116.404, 39.915));
map.addOverlay(marker);
marker.getIcon().setSize({ width: 30, height: 40 });
var selectedPosition = point;
geolocation.getCurrentPosition(
function (r) {
if (this.getStatus() == BMAP_STATUS_SUCCESS) {
//- var mk = new BMap.Marker(r.point);
//- map.addOverlay(mk);
marker.setPosition(r.point);
map.panTo(r.point);
document.getElementById("coordinate").value =
"位置经纬度:" + r.point.lng + "," + r.point.lat;
// alert('您的位置:'+r.point.lng+','+r.point.lat);
selectedPosition = r.point;
} else {
// alert('failed'+this.getStatus());
}
},
{ enableHighAccuracy: true }
);
function showInfo(e) {
// alert(e.point.lng + ", " + e.point.lat);
marker.setPosition(e.point);
selectedPosition = e.point;
document.getElementById("coordinate").value =
"位置经纬度:" + e.point.lng + "," + e.point.lat;
}
map.addEventListener("touchend", showInfo);
map.addEventListener("touchmove", function (e) {
map.enableDragging();
});
map.addEventListener("touchend", function (e) {
map.disableDragging();
});
</script>
</body>
</html>