Openlayers顯示地理位置坐標(biāo)的方法
本文實(shí)例為大家分享了Openlayers顯示地理位置坐標(biāo)的具體代碼,供大家參考,具體內(nèi)容如下
1、新建一個(gè)html頁面,引入ol.js和ol.css文件,然后在body中創(chuàng)建兩個(gè)div標(biāo)簽,分別用來作為地圖和鼠標(biāo)位置控件的容器;
2、代碼實(shí)現(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 () { //初始化鼠標(biāo)位置控件 var mousePositionControl = new ol.control.MousePosition({ //樣式類名稱 className: 'mosuePosition', //投影坐標(biāo)格式,顯示小數(shù)點(diǎn)后邊多少位 coordinateFormat: ol.coordinate.createStringXY(8), //指定投影 projection: 'EPSG:4326', //目標(biāo)容器 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 }) }); //將鼠標(biāo)位置坐標(biāo)控件加入到map中 map.addControl(mousePositionControl); } </script> </head> <body> <div id="map"> <div id="myposition"></div> </div> </body> </html>
3、結(jié)果展示
當(dāng)鼠標(biāo)在地圖上移動(dòng)時(shí),會(huì)在左下角顯示當(dāng)前位置的地理坐標(biāo)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript async/await原理及實(shí)例解析
這篇文章主要介紹了JavaScript async/await原理及實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12動(dòng)態(tài)創(chuàng)建script標(biāo)簽實(shí)現(xiàn)跨域資源訪問的方法介紹
本篇文章主要是對動(dòng)態(tài)創(chuàng)建script標(biāo)簽實(shí)現(xiàn)跨域資源訪問的方法進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02javascript html實(shí)現(xiàn)網(wǎng)頁版日歷代碼
這篇文章主要介紹了javascript html實(shí)現(xiàn)網(wǎng)頁版日歷代碼,需要的朋友可以參考下2016-03-03js?字符串反轉(zhuǎn)(倒序)的幾種方式總結(jié)
這篇文章主要介紹了js?字符串反轉(zhuǎn)(倒序)的幾種方式總結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10