7-高級視圖定位

本博客合集是我的openlayers學(xué)習(xí)筆記缤至,希望能幫助到剛開始接觸openlayers的同學(xué)
@commnet 所用openlayers版本:v5.3.0
@commnet 閱讀本文前需要對前端知識有一定的了解
@comment 本文內(nèi)容只提供參考盐欺,建議結(jié)合openlayers官網(wǎng)的APIexamples來學(xué)習(xí)
@comment 部分代碼參考了@老胡

有些情景需要在地圖上對圖形進(jìn)行定位,如點擊了某文本中的“泰坦尼克號”鏈接凰棉,希望自動在地圖上定位出該船的位置梁剔。

在確定了地圖視角和位置的情況下瞬哼,圖形也就定位了奄薇,常有以下幾種方式的定位(請忽略我這吃力的網(wǎng)速):

  • 地圖內(nèi)容區(qū)外接圖形(內(nèi)容區(qū)外接多邊形的上下頂點)
  • 地圖內(nèi)容區(qū)以最大的整數(shù)zoom,定位完整圖形(當(dāng)前zoom是能看到完整多邊形最大的整數(shù)zoom了)
  • 地圖內(nèi)容區(qū)以最小的整數(shù)zoom脑豹,讓圖形鋪滿地圖
  • 地圖內(nèi)容區(qū)以某分辨率郑藏,中心定位圖形(中間的小圓圈)
  • 地圖內(nèi)容區(qū)相對于地圖視窗大小,以指定偏移量定位圖形(小圓圈偏移到了指定的正方形區(qū)域中)

注:這里的“內(nèi)容區(qū)”是我的理解瘩欺,可以把地圖的view理解成css中的盒模型必盖,當(dāng)view.fit函數(shù)(下面會提到)沒有指定padding參數(shù)時,內(nèi)容區(qū)就是整個view俱饿,一旦指定了padding參數(shù)歌粥,內(nèi)容區(qū)就變成了padding圈起來的視區(qū)了。

  • 創(chuàng)建5個功能測試按鈕和地圖容器
<button id="zoomtoswitzerlandbest">外接匹配</button><br />
<button id="zoomtoswitzerlandconstrained">最佳分辨率匹配</button><br />
<button id="zoomtoswitzerlandnearest">最佳范圍匹配</button><br />
<button id="zoomtolausanne">點居中</button><br />
<button id="centerlausanne">定位點到指定位置</button>
<div class="mapcontainer">
    <div id="map" class="map"></div>
    <div class="padding-top"></div>
    <div class="padding-left"></div>
    <div class="padding-right"></div>
    <div class="padding-bottom"></div>
    <div class="center"></div>
</div>
  • 添加樣式
.mapcontainer {
    position: relative;
    margin-bottom: 20px;
}

.map {
    width: 1000px;
    height: 600px;
}

div.ol-zoom {
    top: 178px;
    left: 158px;
}

div.ol-rotate {
    top: 178px;
    right: 58px;
}

.map div.ol-attribution {
    bottom: 30px;
    right: 50px;
}

.padding-top {
    position: absolute;
    top: 0;
    left: 0px;
    width: 1000px;
    height: 170px;
    background: rgba(255, 255, 255, 0.5);
}

.padding-left {
    position: absolute;
    top: 170px;
    left: 0;
    width: 150px;
    height: 400px;
    background: rgba(255, 255, 255, 0.5);
}

.padding-right {
    position: absolute;
    top: 170px;
    left: 950px;
    width: 50px;
    height: 400px;
    background: rgba(255, 255, 255, 0.5);
}

.padding-bottom {
    position: absolute;
    top: 570px;
    left: 0px;
    width: 1000px;
    height: 30px;
    background: rgba(255, 255, 255, 0.5);
}

.center {
    position: absolute;
    border: solid 1px black;
    top: 490px;
    left: 560px;
    width: 20px;
    height: 20px;
}
  • 創(chuàng)建地圖對象
//在矢量圖層上添加一個多邊形(ol.geom.Polygon)和一個點(ol.geom.Point)
var source = new ol.source.Vector({
    url: '../data/switzerland.geojson',
    format: new ol.format.GeoJSON()
});

//創(chuàng)建樣式對象
var style = new ol.style.Style({
    fill: new ol.style.Fill({
        color: 'rgba(255, 255, 255, 0.6)'
    }),
    stroke: new ol.style.Stroke({
        color: '#319FD3',
        width: 1
    }),
    image: new ol.style.Circle({
        radius: 5,
        fill: new ol.style.Fill({
            color: 'rgba(255, 255, 255, 0.6)'
        }),
        stroke: new ol.style.Stroke({
            color: '#319FD3',
            width: 1
        })
    })
});

//創(chuàng)建矢量圖層
var vectorLayer = new ol.layer.Vector({
    source: source,
    style: style
});

var view = new ol.View({
    center: [0, 0],
    zoom: 1
});

//地圖包含底圖和矢量圖層
var map = new ol.Map({
    layers: [
        new ol.layer.Tile({
            source: new ol.source.OSM()
        }),
        vectorLayer
    ],
    target: 'map',
    view: view
});
  • 為5個功能測試按鈕添加點擊事件拍埠,注釋中詳細(xì)說明了幾種定位的用法和區(qū)別
//多邊形的外接匹配:地圖的內(nèi)容區(qū)外接多邊形
var zoomtoswitzerlandbest = document.getElementById('zoomtoswitzerlandbest');
zoomtoswitzerlandbest.addEventListener('click', function() {
    var feature = source.getFeatures()[0];
    var polygon = feature.getGeometry();
    //將view理解成css中的盒模型失驶,多邊形相對于padding里的內(nèi)容區(qū)進(jìn)行定位
    //constrainResolution默認(rèn)為true,這里設(shè)為false即不限制分辨率枣购,zoom不必是整數(shù)嬉探,因此可以實現(xiàn)精確的外接定位
    view.fit(polygon, {
        padding: [170, 50, 30, 150],
        constrainResolution: false
    });
    console.log(map.getView().getZoom())
}, false);

//多邊形的最佳分辨率匹配:再繼續(xù)放大,多邊形就超出內(nèi)容區(qū)了
var zoomtoswitzerlandconstrained =
    document.getElementById('zoomtoswitzerlandconstrained');
zoomtoswitzerlandconstrained.addEventListener('click', function() {
    var feature = source.getFeatures()[0];
    var polygon = feature.getGeometry();
    //如果不寫constrainResolution棉圈,默認(rèn)為true
    //即該定位結(jié)果是在zoom為整數(shù)的前提下涩堤,能看到完整多邊形的最大分辨率
    view.fit(polygon, {
        padding: [170, 50, 30, 150]
    });
    console.log(map.getView().getZoom())
}, false);

//多邊形的最佳范圍匹配
var zoomtoswitzerlandnearest =
    document.getElementById('zoomtoswitzerlandnearest');
zoomtoswitzerlandnearest.addEventListener('click', function() {
    var feature = source.getFeatures()[0];
    var polygon = feature.getGeometry();
    //設(shè)置nearest為true,以最小的整數(shù)zoom迄损,讓圖形鋪滿地圖
    view.fit(polygon, {
        padding: [170, 50, 30, 150],
        nearest: true
    });
    console.log(map.getView().getZoom())
}, false);

//將點以某分辨率居中
var zoomtolausanne = document.getElementById('zoomtolausanne');
zoomtolausanne.addEventListener('click', function() {
    var feature = source.getFeatures()[1];
    var point = feature.getGeometry();
    //將點point以分辨率minResolution居中
    view.fit(point, {
        padding: [170, 50, 30, 150],
        minResolution: 250
    });

}, false);

//將點相對map.getSize()以指定偏移量定位
var centerlausanne = document.getElementById('centerlausanne');
centerlausanne.addEventListener('click', function() {
    var feature = source.getFeatures()[1];
    var point = feature.getGeometry();
    var size = map.getSize();
    console.log(size)
    //相對于size定躏,點point距離頂部偏移了500px账磺,距離左邊偏移了570px
    view.centerOn(point.getCoordinates(), size, [570, 500]);
}, false);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末芹敌,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子垮抗,更是在濱河造成了極大的恐慌氏捞,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,682評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件冒版,死亡現(xiàn)場離奇詭異液茎,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評論 3 395
  • 文/潘曉璐 我一進(jìn)店門捆等,熙熙樓的掌柜王于貴愁眉苦臉地迎上來滞造,“玉大人,你說我怎么就攤上這事栋烤≮搜” “怎么了?”我有些...
    開封第一講書人閱讀 165,083評論 0 355
  • 文/不壞的土叔 我叫張陵明郭,是天一觀的道長买窟。 經(jīng)常有香客問我,道長薯定,這世上最難降的妖魔是什么始绍? 我笑而不...
    開封第一講書人閱讀 58,763評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮话侄,結(jié)果婚禮上亏推,老公的妹妹穿的比我還像新娘。我一直安慰自己年堆,他們只是感情好径簿,可當(dāng)我...
    茶點故事閱讀 67,785評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著嘀韧,像睡著了一般篇亭。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上锄贷,一...
    開封第一講書人閱讀 51,624評論 1 305
  • 那天译蒂,我揣著相機(jī)與錄音,去河邊找鬼谊却。 笑死柔昼,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的炎辨。 我是一名探鬼主播捕透,決...
    沈念sama閱讀 40,358評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼碴萧!你這毒婦竟也來了乙嘀?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,261評論 0 276
  • 序言:老撾萬榮一對情侶失蹤破喻,失蹤者是張志新(化名)和其女友劉穎虎谢,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體曹质,經(jīng)...
    沈念sama閱讀 45,722評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡婴噩,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年擎场,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片几莽。...
    茶點故事閱讀 40,030評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡迅办,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出章蚣,到底是詐尸還是另有隱情礼饱,我是刑警寧澤,帶...
    沈念sama閱讀 35,737評論 5 346
  • 正文 年R本政府宣布究驴,位于F島的核電站镊绪,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏洒忧。R本人自食惡果不足惜蝴韭,卻給世界環(huán)境...
    茶點故事閱讀 41,360評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望熙侍。 院中可真熱鬧榄鉴,春花似錦、人聲如沸蛉抓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽巷送。三九已至驶忌,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間笑跛,已是汗流浹背付魔。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留飞蹂,地道東北人几苍。 一個月前我還...
    沈念sama閱讀 48,237評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像陈哑,于是被迫代替她去往敵國和親妻坝。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,976評論 2 355