maptalks,mapboxgl實現(xiàn)3D樓宇,攝像頭懸浮

<!DOCTYPE html>
<html>
<head>
    <title>maptalks.mapboxgl demo</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link type="text/css" rel="stylesheet" >
    <script type="text/javascript" src="https://unpkg.com/maptalks/dist/maptalks.min.js"></script>
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.9.1/mapbox-gl.js'></script>
    <link  rel='stylesheet' />
    <script type="text/javascript" src="maptalks.mapboxgl.js"></script>
    <script type="text/javascript" src="build.js"></script>
    <style>
        html,body{
            margin:0px;
            height:100%;
        }
        #map {
            width: 100%;
            height:100%;
        }
    </style>
</head>
<body>
<div id="map"></div>
<script type="text/javascript" >
    mapboxgl.accessToken = 'pk.eyJ1Ijoiemh1bGlwaWFvIiwiYSI6ImNqemtsbXZwdzBtNjAzZG9hOWt4Z21ocHUifQ.dsdPomNsO7w4Q-rRduoBcg';

    var baseLayer = new maptalks.MapboxglLayer('tile',{
        glOptions : {
            'style' : 'mapbox://styles/mapbox/dark-v10'
        }
    });

    var map = new maptalks.Map("map",{
        //limit max pitch to 60 as mapbox-gl-js required
        maxPitch : 60,
        center:   [121.57792019,31.26885962],
        zoom   :  16,
        pitch: 56,
        bearing: 0,
        baseLayer : baseLayer
    });
    var markers=[];//所有攝像頭
    let cameraWidth=15,cameraHeight=15,textSize=4;
     //根據(jù)層級來控制攝像頭大小
     map.on('zoomend',function(zoom){
        let temp=zoom.from-zoom.to;
        if(temp>0){
            temp=1.5*temp;
            cameraWidth=cameraWidth/temp;
            cameraHeight=cameraHeight/temp;
            textSize=textSize/temp;
        }else if(temp<0){
            temp=Math.abs(temp)*1.5;
            cameraWidth=cameraWidth*temp;
            cameraHeight=cameraHeight*temp;
            textSize=textSize*temp;
        }
        markers.forEach(function(g){
            g.updateSymbol({
            'markerWidth' : cameraWidth,
            'markerHeight' : cameraHeight,
            'textSize' : textSize
          });
        });
     });
     
     var layer=new maptalks.VectorLayer('vector',{
        enableAltitude : true,        // enable altitude
        altitudeProperty : 'altitude' // altitude property in properties, default by 'altitude'
      }).addTo(map);
      
      
      var marker1 = new maptalks.Marker(
        [121.57792019,31.26885962],
        {
          symbol: 
          {
            'markerFile'   : '1.png',//攝像頭圖片
            'markerWidth'  : cameraWidth,
            'markerHeight' : cameraHeight,
            'markerDx'     : 0,
            'markerDy'     : 0,
            'markerOpacity': 1,
            'textFaceName' : 'sans-serif',
            'textName' : '{name}',
            'textSize' : textSize,
            'textDy'   : 3
          },
          properties : {
            altitude : 50,
            name:'攝像頭1'
          }
        }
      );
      var marker2 = new maptalks.Marker(
        [121.57880035,31.26726501],
        {
          symbol: 
          {
            'markerFile'   : '2.png',//攝像頭圖片
            'markerWidth'  : cameraWidth,
            'markerHeight' : cameraHeight,
            'markerDx'     : 0,
            'markerDy'     : 0,
            'markerOpacity': 1,
            'textFaceName' : 'sans-serif',
            'textName' : '{name}',
            'textSize' : textSize,
            'textDy'   : 3
          },
          properties : {
            altitude : 50,
            name:'攝像頭2'
          }
        }
      );
      marker1.on('click',function(){
      });
      markers.push(marker1);
      markers.push(marker2);
      
      var multiPolygon = new maptalks.MultiPolygon([
                    [
                        [
                            [
                                121.57791602,
                                31.26885628
                            ],
                            [
                                121.57559574,
                                31.2672609
                            ],
                            [
                                121.57405076,
                                31.26650217
                            ],
                            [
                                121.57531057,
                                31.26453833
                            ],
                            [
                                121.57501869,
                                31.2643849
                            ],
                            [
                                121.57525944,
                                31.26404894
                            ],
                            [
                                121.57639994,
                                31.26319769
                            ],
                            [
                                121.57685333,
                                31.26344285
                            ],
                            [
                                121.57791642,
                                31.26454417
                            ],
                            [
                                121.57824748,
                                31.26493524
                            ],
                            [
                                121.57754657,
                                31.26540716
                            ],
                            [
                                121.57655069,
                                31.26546411
                            ],
                            [
                                121.57665163,
                                31.26625151
                            ],
                            [
                                121.57877251,
                                31.26606947
                            ],
                            [
                                121.57880035,
                                31.26726501
                            ],
                            [
                                121.57845355,
                                31.26765787
                            ],
                            [
                                121.57894712,
                                31.2680297
                            ],
                            [
                                121.57812851,
                                31.26882507
                            ],
                            [
                                121.57791602,
                                31.26885628
                            ]
                        ]
                    ],
                    [
                        [
                            [
                                121.57792019,
                                31.26885962
                            ],
                            [
                                121.57812435,
                                31.26882937
                            ],
                            [
                                121.5780214,
                                31.26892802
                            ],
                            [
                                121.57792019,
                                31.26885962
                            ]
                        ]
                    ]
                ], {
        visible : true,
        editable : true,
        cursor : null,
        shadowBlur : 0,
        shadowColor : 'black',
        draggable : false,
        dragShadow : false, // display a shadow during dragging
        drawOnAxis : null,  // force dragging stick on a axis, can be: x, y
        symbol: {
          'polygonFill' : 'rgb(135,196,240)',
          'polygonOpacity' : 0.1,
          'lineWidth' : 0
        },
        properties : {
          altitude : 60
        }
      });
      
      layer.addGeometry(markers);
      //二樓樓面呈現(xiàn)(凸顯層)
      layer.addGeometry(multiPolygon);
      var glMap=baseLayer.getGlMap();
      glMap.on('load',function(){
        glMap.addLayer({
                'id': 'buildings',
                'type': 'fill-extrusion',
                'minzoom': 1,
                'source': {
                    'type': 'geojson',
                    //樓宇坐標(biāo),層級 json文件
                    'data': build
                },
                'layout': {
                    'visibility': 'visible'
                },
                'paint': {
                    'fill-extrusion-color': ['get', 'color'],
                    'fill-extrusion-height': ['get', 'height'],
                    'fill-extrusion-base': ['get', 'baseHeight'],
                    'fill-extrusion-opacity': 0.6
                }
            });
      });
</script>
</body>
</html>

build.js

var build={
    "features":[
        {
            "type":"Feature",
            "properties":{
                "name":null,
                "type":"building",
                "key":"長征村|151號",
                "height":60,
                "color":"#00cec9",
                "baseHeight":0,
                "floor":"1"
            },
            "geometry":{
                "type":"MultiPolygon",
                "coordinates":[
                    [
                        [
                            [
                                121.57791602,
                                31.26885628
                            ],
                            [
                                121.57559574,
                                31.2672609
                            ],
                            [
                                121.57405076,
                                31.26650217
                            ],
                            [
                                121.57531057,
                                31.26453833
                            ],
                            [
                                121.57501869,
                                31.2643849
                            ],
                            [
                                121.57525944,
                                31.26404894
                            ],
                            [
                                121.57639994,
                                31.26319769
                            ],
                            [
                                121.57685333,
                                31.26344285
                            ],
                            [
                                121.57791642,
                                31.26454417
                            ],
                            [
                                121.57824748,
                                31.26493524
                            ],
                            [
                                121.57754657,
                                31.26540716
                            ],
                            [
                                121.57655069,
                                31.26546411
                            ],
                            [
                                121.57665163,
                                31.26625151
                            ],
                            [
                                121.57877251,
                                31.26606947
                            ],
                            [
                                121.57880035,
                                31.26726501
                            ],
                            [
                                121.57845355,
                                31.26765787
                            ],
                            [
                                121.57894712,
                                31.2680297
                            ],
                            [
                                121.57812851,
                                31.26882507
                            ],
                            [
                                121.57791602,
                                31.26885628
                            ]
                        ]
                    ],
                    [
                        [
                            [
                                121.57792019,
                                31.26885962
                            ],
                            [
                                121.57812435,
                                31.26882937
                            ],
                            [
                                121.5780214,
                                31.26892802
                            ],
                            [
                                121.57792019,
                                31.26885962
                            ]
                        ]
                    ]
                ]
            }
        },
        {
            "type":"Feature",
            "properties":{
                "name":null,
                "type":"building",
                "key":"長征村|151號",
                "height":120,
                "color":"#00cec9",
                "baseHeight":60,
                "floor":"2"
            },
            "geometry":{
                "type":"MultiPolygon",
                "coordinates":[
                    [
                        [
                            [
                                121.57791602,
                                31.26885628
                            ],
                            [
                                121.57559574,
                                31.2672609
                            ],
                            [
                                121.57405076,
                                31.26650217
                            ],
                            [
                                121.57531057,
                                31.26453833
                            ],
                            [
                                121.57501869,
                                31.2643849
                            ],
                            [
                                121.57525944,
                                31.26404894
                            ],
                            [
                                121.57639994,
                                31.26319769
                            ],
                            [
                                121.57685333,
                                31.26344285
                            ],
                            [
                                121.57791642,
                                31.26454417
                            ],
                            [
                                121.57824748,
                                31.26493524
                            ],
                            [
                                121.57754657,
                                31.26540716
                            ],
                            [
                                121.57655069,
                                31.26546411
                            ],
                            [
                                121.57665163,
                                31.26625151
                            ],
                            [
                                121.57877251,
                                31.26606947
                            ],
                            [
                                121.57880035,
                                31.26726501
                            ],
                            [
                                121.57845355,
                                31.26765787
                            ],
                            [
                                121.57894712,
                                31.2680297
                            ],
                            [
                                121.57812851,
                                31.26882507
                            ],
                            [
                                121.57791602,
                                31.26885628
                            ]
                        ]
                    ],
                    [
                        [
                            [
                                121.57792019,
                                31.26885962
                            ],
                            [
                                121.57812435,
                                31.26882937
                            ],
                            [
                                121.5780214,
                                31.26892802
                            ],
                            [
                                121.57792019,
                                31.26885962
                            ]
                        ]
                    ]
                ]
            }
        }
    ],
    "type":"FeatureCollection"
}
效果圖
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末爸邢,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子烈评,更是在濱河造成了極大的恐慌塘雳,老刑警劉巖张足,帶你破解...
    沈念sama閱讀 218,607評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡孝鹊,警方通過查閱死者的電腦和手機(jī)扭仁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評論 3 395
  • 文/潘曉璐 我一進(jìn)店門垮衷,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人乖坠,你說我怎么就攤上這事搀突。” “怎么了熊泵?”我有些...
    開封第一講書人閱讀 164,960評論 0 355
  • 文/不壞的土叔 我叫張陵仰迁,是天一觀的道長甸昏。 經(jīng)常有香客問我,道長徐许,這世上最難降的妖魔是什么施蜜? 我笑而不...
    開封第一講書人閱讀 58,750評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮雌隅,結(jié)果婚禮上翻默,老公的妹妹穿的比我還像新娘。我一直安慰自己恰起,他們只是感情好修械,可當(dāng)我...
    茶點故事閱讀 67,764評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著检盼,像睡著了一般肯污。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上梯皿,一...
    開封第一講書人閱讀 51,604評論 1 305
  • 那天仇箱,我揣著相機(jī)與錄音,去河邊找鬼东羹。 笑死剂桥,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的属提。 我是一名探鬼主播权逗,決...
    沈念sama閱讀 40,347評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼冤议!你這毒婦竟也來了斟薇?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,253評論 0 276
  • 序言:老撾萬榮一對情侶失蹤恕酸,失蹤者是張志新(化名)和其女友劉穎堪滨,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蕊温,經(jīng)...
    沈念sama閱讀 45,702評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡袱箱,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,893評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了义矛。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片发笔。...
    茶點故事閱讀 40,015評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖凉翻,靈堂內(nèi)的尸體忽然破棺而出了讨,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 35,734評論 5 346
  • 正文 年R本政府宣布前计,位于F島的核電站胞谭,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏男杈。R本人自食惡果不足惜韭赘,卻給世界環(huán)境...
    茶點故事閱讀 41,352評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望势就。 院中可真熱鬧泉瞻,春花似錦、人聲如沸苞冯。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽舅锄。三九已至鞭达,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間皇忿,已是汗流浹背畴蹭。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留鳍烁,地道東北人叨襟。 一個月前我還...
    沈念sama閱讀 48,216評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像幔荒,于是被迫代替她去往敵國和親糊闽。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,969評論 2 355