1+ <!DOCTYPE html>
2+ < html >
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < title > openlayers 建筑立体效果</ title >
6+ < link href ="http://cdn.bootcss.com/openlayers/4.0.1/ol.css " rel ="stylesheet ">
7+ < script type ="text/javascript " src ="http://cdn.bootcss.com/openlayers/4.0.1/ol-debug.js "> </ script >
8+ < script type ="text/javascript " src ="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js "> </ script >
9+ < script type ="text/javascript " src ="http://iclient.supermapol.com/libs/osmbuildings/OSMBuildings-OL3.js "> </ script >
10+ < style >
11+ .ol-popup {
12+ position : absolute;
13+ background-color : white;
14+ -webkit-filter : drop-shadow (0 1px 4px rgba (0 , 0 , 0 , 0.2 ));
15+ filter : drop-shadow (0 1px 4px rgba (0 , 0 , 0 , 0.2 ));
16+ padding : 15px ;
17+ border-radius : 10px ;
18+ border : 1px solid # cccccc ;
19+ bottom : 12px ;
20+ left : -50px ;
21+ font-size : 14px ;
22+ min-width : 250px ;
23+ z-index : 100 ;
24+ }
25+
26+ .ol-popup : after , .ol-popup : before {
27+ top : 100% ;
28+ border : solid transparent;
29+ content : " " ;
30+ height : 0 ;
31+ width : 0 ;
32+ position : absolute;
33+ pointer-events : none;
34+ }
35+
36+ .ol-popup : after {
37+ border-top-color : white;
38+ border-width : 10px ;
39+ left : 48px ;
40+ margin-left : -10px ;
41+ }
42+
43+ .ol-popup : before {
44+ border-top-color : # cccccc ;
45+ border-width : 11px ;
46+ left : 48px ;
47+ margin-left : -11px ;
48+ }
49+
50+ .ol-popup-closer {
51+ text-decoration : none;
52+ position : absolute;
53+ top : 2px ;
54+ right : 8px ;
55+ }
56+
57+ .ol-popup-closer : after {
58+ content : "✖" ;
59+ }
60+ </ style >
61+ </ head >
62+ < body style =" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0; ">
63+ < div id ="map " style ="margin:0 auto;width: 100%;height: 100% "> </ div >
64+ < div id ="popup " class ="ol-popup ">
65+ < a href ="# " id ="popup-closer " class ="ol-popup-closer "> </ a >
66+ < div id ="popup-content "> </ div >
67+ </ div >
68+ < script >
69+ var container = document . getElementById ( 'popup' ) ;
70+ var content = document . getElementById ( 'popup-content' ) ;
71+ var closer = document . getElementById ( 'popup-closer' ) ;
72+ var overlay = new ol . Overlay ( ( {
73+ element : container ,
74+ autoPan : true ,
75+ autoPanAnimation : {
76+ duration : 250
77+ }
78+ } ) ) ;
79+
80+ var map = new ol . Map ( {
81+ target : 'map' ,
82+ view : new ol . View ( {
83+ center : ol . proj . transform ( [ 116.450 , 39.916 ] , 'EPSG:4326' , 'EPSG:3857' ) ,
84+ zoom : 16 ,
85+ } ) ,
86+ overlays : [ overlay ]
87+ } ) ;
88+ map . addLayer ( new ol . layer . Tile ( {
89+ source : new ol . source . XYZ ( {
90+ url : "https://a.tiles.mapbox.com/v3/osmbuildings.kbpalbpk/{z}/{x}/{y}.png" ,
91+ attributions : [
92+ new ol . Attribution ( {
93+ html : ' Imagery © <a href="http://mapbox.com">Mapbox</a>'
94+ } ) ]
95+ } )
96+ } ) ) ;
97+
98+ loadData ( ) ;
99+
100+ function loadData ( ) {
101+ var data ;
102+ $ . get ( '../data/buildings.json' , function ( geojson ) {
103+ data = geojson ;
104+ new OSMBuildings ( map )
105+ . date ( new Date ( 2017 , 5 , 15 , 17 , 30 ) )
106+ . set ( geojson )
107+ . click ( bindPopup ) ;
108+ } ) ;
109+
110+ function bindPopup ( evt ) {
111+ var name = getFeatureNameById ( evt . feature ) ;
112+ if ( name ) {
113+ content . innerHTML = name ;
114+ overlay . setPosition ( [ evt . lat , evt . lon ] ) ;
115+ }
116+ closer . onclick = function ( ) {
117+ overlay . setPosition ( undefined ) ;
118+ closer . blur ( ) ;
119+ return false ;
120+ } ;
121+ }
122+
123+ function getFeatureNameById ( id ) {
124+ if ( ! data || ! id ) {
125+ return null ;
126+ }
127+ var features = data . features ;
128+ for ( var i = 0 ; i < features . length ; i ++ ) {
129+ if ( features [ i ] . properties . id === id ) {
130+ return features [ i ] . properties . name ;
131+ }
132+ }
133+ }
134+ }
135+
136+ </ script >
137+ </ body >
138+ </ html >
0 commit comments