Openlayers顯示地理位置坐標的方法
更新時間:2020年09月28日 10:54:36 作者:桃李不言_下自成蹊
這篇文章主要為大家詳細介紹了Openlayers顯示地理位置坐標,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Openlayers顯示地理位置坐標的具體代碼,供大家參考,具體內(nèi)容如下
1、新建一個html頁面,引入ol.js和ol.css文件,然后在body中創(chuàng)建兩個div標簽,分別用來作為地圖和鼠標位置控件的容器;
2、代碼實現(xiàn)
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="../lib/ol/ol.js"></script>
<link href="../css/ol.css" rel="stylesheet" />
<style type="text/css">
#myposition
{
float:left;
position:absolute;
bottom:10px;
width:400px;
height:20px;
z-index:2000;
}
.mosuePosition
{
color:blue;
font-size:20px;
font-family:'微軟雅黑';
}
</style>
<script type="text/javascript">
window.onload = function () {
//初始化鼠標位置控件
var mousePositionControl = new ol.control.MousePosition({
//樣式類名稱
className: 'mosuePosition',
//投影坐標格式,顯示小數(shù)點后邊多少位
coordinateFormat: ol.coordinate.createStringXY(8),
//指定投影
projection: 'EPSG:4326',
//目標容器
target:document.getElementById('myposition')
});
//初始化地圖容器
var map = new ol.Map({
target:'map',
layers:[
new ol.layer.Tile({
source:new ol.source.OSM()
}),
],
view:new ol.View({
center:[0,0],
zoom:3
})
});
//將鼠標位置坐標控件加入到map中
map.addControl(mousePositionControl);
}
</script>
</head>
<body>
<div id="map">
<div id="myposition"></div>
</div>
</body>
</html>
3、結(jié)果展示
當鼠標在地圖上移動時,會在左下角顯示當前位置的地理坐標

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
動態(tài)創(chuàng)建script標簽實現(xiàn)跨域資源訪問的方法介紹
本篇文章主要是對動態(tài)創(chuàng)建script標簽實現(xiàn)跨域資源訪問的方法進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02
javascript html實現(xiàn)網(wǎng)頁版日歷代碼
這篇文章主要介紹了javascript html實現(xiàn)網(wǎng)頁版日歷代碼,需要的朋友可以參考下2016-03-03
js?字符串反轉(zhuǎn)(倒序)的幾種方式總結(jié)
這篇文章主要介紹了js?字符串反轉(zhuǎn)(倒序)的幾種方式總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10

