leaflet.Control.LeadorStreetView.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. L.Control.LeadorStreetView = L.Control.extend({
  2. //是否初始化
  3. _initialized:false,
  4. _isShow:false,
  5. _isLoad:false,
  6. streetLayerGuid:null,
  7. streetMarkerGuid:null,
  8. options:{
  9. position:'topleft',
  10. autoZIndex:true,
  11. state:false,
  12. onFunc:null,
  13. offFunc:null
  14. },
  15. initialize:function(options){
  16. L.setOptions(this, options);
  17. return this;
  18. },
  19. onAdd:function(map){
  20. var _this = this;
  21. _this._map = map;
  22. ONEMAP.C.publisher.subscribe(_this.updateMarker23D,'change23D');
  23. _this._createControl();
  24. return _this._container;
  25. },
  26. /**
  27. * 创建冒泡窗
  28. */
  29. _creatPopupHtml:function(options){
  30. var popupHtml = '<div class="popup_html">'+
  31. ' <div class="popup-lt"></div>'+
  32. ' <div class="popup-lb"></div>'+
  33. ' <div class="popup-rt"></div>'+
  34. ' <div class="popup-rb"></div>'+
  35. ' <div class="popup-ct" style="text-indent:15px;line-height:48px">'+options.name+
  36. ' </div>'+
  37. ' <div class="popup-cb" style="padding-bottom:20px;color:#666666 !important">'+options.content+'</div>'+
  38. '</div>'
  39. return popupHtml;
  40. },
  41. _vieModalHtml:'<iframe frameborder="0" class="cover-iframe"></iframe>'+
  42. '<div class="cover-content">'+
  43. '<div class="popup_html">'+
  44. ' <div class="popup-lt"></div>'+
  45. ' <div class="popup-lb"></div>'+
  46. ' <div class="popup-rt"></div>'+
  47. ' <div class="popup-rb"></div>'+
  48. ' <div class="popup-ct" style="text-indent:15px;line-height:48px">路网实景</div>'+
  49. ' <div class="popup-cb" style="padding-bottom:20px;color:#666666 !important">'+
  50. ' <div id="leadorStreetViewWrap">'+
  51. '<div id="leadorStreetViewSwf"></div>'+
  52. ' </div>'+
  53. ' </div>'+
  54. '</div>'+
  55. '</div>',
  56. _createControl:function(){
  57. var _this = this;
  58. _this._container = L.DomUtil.create('div','leaflet-bar leaflet-control-leador-street-view');
  59. L.DomEvent.on(_this._container, 'contextmenu', L.DomEvent.stopPropagation);
  60. var link = L.DomUtil.create('a','leaflet-control-leador-street-view-link', this._container);
  61. L.DomUtil.create('span','',link);
  62. link.title = '路网实景';
  63. _this._viewModal = L.DomUtil.create('div','modal');
  64. _this._viewModal.id = 'leadorStreetViewModal';
  65. _this._viewModal.innerHTML = _this._vieModalHtml;
  66. document.body.appendChild(_this._viewModal);
  67. L.DomEvent
  68. .on(link, 'click', L.DomEvent.stopPropagation)
  69. .on(link, 'click', L.DomEvent.preventDefault)
  70. .on(link,'click', function(){
  71. if(_this._isLoad){
  72. return false;
  73. }
  74. if(_this.options.state){
  75. _this.options.state = false;
  76. _this._map.fire('leadorStreetView:off');
  77. L.DomUtil.removeClass(_this._container,'active');
  78. _this._removeViewMarker();
  79. _this._removeViewRoadTile();
  80. }else{
  81. _this.options.state = true;
  82. _this._map.fire('leadorStreetView:on');
  83. _this._buildLeadorSwf();
  84. _this._isLoad = true;
  85. setTimeout(function(){
  86. _this._isLoad = false;
  87. },1000);
  88. L.DomUtil.addClass(_this._container,'active');
  89. //setTimeout(function(){
  90. _this._addViewMarker();
  91. _this._addViewRoadTile();
  92. //},1000);
  93. }
  94. });
  95. _this._map.on('leadorStreetViewPoint:on cleanMap',function(){
  96. if(_this.options.state){
  97. _this.options.state = false;
  98. _this._map.fire('leadorStreetView:off');
  99. L.DomUtil.removeClass(_this._container,'active');
  100. _this._removeViewMarker();
  101. _this._removeViewRoadTile();
  102. }
  103. });
  104. },
  105. addShijing:function(){
  106. var _this = this;
  107. _this._groupId = ONEMAP.M.heighterControl.modValue.shijingGroup;
  108. _this.add3DShijing = true;
  109. if(!_this.options.state){
  110. _this.options.state = true;
  111. _this._map.fire('leadorStreetView:on');
  112. _this._buildLeadorSwf();
  113. _this._isLoad = true;
  114. setTimeout(function(){
  115. _this._isLoad = false;
  116. },1000);
  117. L.DomUtil.addClass(_this._container,'active');
  118. //setTimeout(function(){
  119. _this._addViewMarker();
  120. _this._addViewRoadTile();
  121. //},1000);
  122. //ONEMAP.C.publisher.subscribe(_this.controlMyLayer,'shijikngLayer');
  123. }
  124. },
  125. controlShijing:function(){
  126. var _this = this;
  127. if(_this.options.state){
  128. _this.options.state = false;
  129. _this._map.fire('leadorStreetView:off');
  130. L.DomUtil.removeClass(_this._container,'active');
  131. _this._removeViewMarker();
  132. _this._removeViewRoadTile();
  133. //ONEMAP.C.publisher.unSubscribe(_this.controlMyLayer,'shijikngLayer');
  134. }
  135. },
  136. //设置显示和隐藏 show/hide
  137. changeStatus:function(type){
  138. var _this = this;
  139. switch(type){
  140. case 'show':
  141. _this._container.style.left = '60px';
  142. break;
  143. case 'hide':
  144. _this._container.style.left = '-10000px';
  145. if(_this.options.state){
  146. _this.options.state = false;
  147. L.DomUtil.removeClass(_this._container,'active');
  148. _this._removeViewMarker();
  149. _this._removeViewRoadTile();
  150. }
  151. break;
  152. case 'off':
  153. if(_this.options.state){
  154. _this.options.state = false;
  155. L.DomUtil.removeClass(_this._container,'active');
  156. _this._removeViewMarker();
  157. _this._removeViewRoadTile();
  158. }
  159. break;
  160. }
  161. },
  162. flashChecker:function(){
  163. var hasFlash = 0;
  164. var flashVersion =0;
  165. try{
  166. if(document.all){
  167. var swf = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
  168. if(swf){
  169. hasFlash = 1;
  170. VSwf = swf.GetVariable('$version');
  171. flashVersion = parseInt(VSwf.split(" ")[1].split(",")[0]);
  172. }
  173. }else {
  174. if(navigator.plugins && navigator.plugins.length > 0){
  175. var swf = navigator.plugins["Shockwave Flash"];
  176. }
  177. if(swf){
  178. hasFlash = 1;
  179. var words = swf.description.split(" ");
  180. for(var i=0;i<words.length; i++){
  181. if(isNaN(parseInt(words[i]))) continue;
  182. flashVersion = parseInt(words[i]);
  183. }
  184. }
  185. }
  186. }catch (e) {
  187. }
  188. return {
  189. f:hasFlash,
  190. v:flashVersion
  191. };
  192. },
  193. //创建路径swf
  194. _buildLeadorSwf:function(){
  195. var _this = this;
  196. var flashPlayerInfo = _this.flashChecker();
  197. if(flashPlayerInfo && flashPlayerInfo.v > 10){
  198. /** 配置 **/
  199. var more = {};
  200. more.flashvars = {};
  201. LeadorStreetView.useHash = false;
  202. /** 设置服务信息 **/
  203. more.flashvars.TrueVisionSeverUrl = onemapUrlConfig.leadorStreetBaseUrl;
  204. more.flashvars.Port = onemapUrlConfig.leadorStreetPort;
  205. more.flashvars.DataName = onemapUrlConfig.leadorStreetDataName;
  206. more.flashvars.TrueVisionImageUrl = onemapUrlConfig.leadorStreetImageUrl;
  207. more.flashvars.UIUrl = onemapUrlConfig.leadorStreetLibUrl+"/IShowChinaTVUIBJ.swf";
  208. more.flashvars.LogoUrl = onemapUrlConfig.leadorStreetLibUrl+'/ld-logo.png';
  209. /** 绑定DIV **/
  210. LeadorStreetView.bindingDIV("leadorStreetViewSwf",null,more);
  211. LeadorStreetView.showFullScreenUI(false);
  212. //位置改变
  213. LeadorStreetView.addCompleteEvent(function(info){
  214. _this._viewMarker.setLatLng([info.lat,info.lon]);
  215. _this.updateMarker23D('3d');
  216. if(info.address){
  217. var latlng = new L.latLng(info.lat,info.lon)
  218. latlng = L.Util.formatHMS(latlng)
  219. var options_popup = {
  220. name:info.address,
  221. content:'<p>'+latlng.lng+';'+latlng.lat+'</p>'
  222. }
  223. var popupHtml = _this._creatPopupHtml(options_popup);
  224. _this._viewMarker.bindPopup(popupHtml).openPopup();
  225. }
  226. map3DViewer.marker({
  227. action: 'update',
  228. guid:_this.streetMarkerGuid,
  229. geojson: {
  230. "geometry": {
  231. "coordinates": [info.lng, info.lat]
  232. }
  233. }
  234. })
  235. //图标偏移到 左侧中心
  236. // var holderWrap = _this._map.getContainer();
  237. // var aLatLng = new L.LatLng(info.lat,info.lon);
  238. // var bPoints = _this._map.project(aLatLng,_this._map.getZoom());
  239. // bPoints.x += (holderWrap.clientWidth/2 - document.getElementById('leadorStreetViewWrap').offsetWidth)/4;
  240. // aLatLng = _this._map.unproject(bPoints,_this._map.getZoom());
  241. // _this._map.setView(aLatLng,_this._map.getZoom());
  242. });
  243. //视角改变
  244. LeadorStreetView.addAngleChangeEvent(function(info){
  245. _this._updateViewMarker(parseInt(info.yaw));
  246. });
  247. }else {
  248. if(document.all){
  249. var flashPlayerLink = map23DConfig.map23DAssetsUrl+"/soft/flashplayer_winax.exe";
  250. }else {
  251. var flashPlayerLink = map23DConfig.map23DAssetsUrl+"/soft/flashplayer_win.exe";
  252. }
  253. var installFlashHtml = '<div class="install-flash-player">' +
  254. '<p><strong>请安装Adobe flash player 11以上版本的播放器。</strong></p>' +
  255. '<a href="'+flashPlayerLink+'"><img src="images/layout/get_flash.png"/><span>Adobe Flash Player 11</span></a>' +
  256. '</div>';
  257. document.getElementById('leadorStreetViewSwf').innerHTML = installFlashHtml;
  258. }
  259. },
  260. /**
  261. * 图标 添加
  262. * @private
  263. */
  264. _addViewMarker: function () {
  265. var _this = this;
  266. _this._viewMarkerBox = L.DomUtil.create('div','leador-street-view-marker-box');
  267. //创建标志
  268. var latlng = new L.latLng(39.91302,116.4166)
  269. latlng = L.Util.formatHMS(latlng)
  270. var options_popup = {
  271. name:'请把我拖动到蓝色路网上!',
  272. content:'<p>'+latlng.lng+';'+latlng.lat+'</p>'
  273. }
  274. var popupHtml = _this._creatPopupHtml(options_popup);
  275. _this._viewMarker = L.marker([39.91302,116.4166],{
  276. draggable:true,
  277. icon: L.divIcon({
  278. className:'leador-street-view-marker',
  279. iconSize: [89, 68],
  280. iconAnchor: [43, 38],
  281. popupAnchor: [-2, -35],
  282. html:'<div id="leadorStreetViewMarkerBox"></div>'
  283. })
  284. }).bindPopup(popupHtml).addTo(map2DViewer.groups[_this._groupId]).openPopup();
  285. //创建3D标记
  286. var center = map23DData.view.center;
  287. _this.streetMarkerGuid = map3DViewer.marker({
  288. action:'add',
  289. groupId:_this._groupId,
  290. geojson:{
  291. "properties": {
  292. title:'请点击蓝色网路',
  293. iconUrl: map23DConfig.map23DAssetsUrl+'/images/layout/icon-shijing4.png',
  294. altitude:0,
  295. altitudeMode:0,
  296. iconSize: [25, 25],
  297. iconAnchor: [12, 12],
  298. popupAnchor:[0,-14],
  299. popupContent:'请点击蓝色网路'
  300. },
  301. "geometry": {
  302. "coordinates": [center.lng,center.lat]
  303. }
  304. }
  305. })
  306. _this._showStreet3DView();
  307. _this._viewMarker.on('dragend',function(){
  308. _this._showStreet3DView();
  309. });
  310. },
  311. _updateViewMarker:function(angle){
  312. var mod=Math.floor(angle/30);
  313. var index=0;
  314. if(angle>(mod*30-15) && angle< (mod*30+15)){
  315. //index=mod*30;
  316. index=mod;
  317. }else if(angle>((mod+1)*30-15) && angle< ((mod+1)*30+15)){
  318. index=mod+1;
  319. }
  320. if(index==12){
  321. index=0;
  322. }
  323. var viewMarkerBox = document.getElementById("leadorStreetViewMarkerBox");
  324. viewMarkerBox.style.backgroundPosition = '0px -'+(index*68)+'px';
  325. },
  326. /**
  327. * 添加路网瓦片
  328. * @private
  329. */
  330. _addViewRoadTile:function(){
  331. var _this = this;
  332. /*_this.tileLayerOverAddress = new L.TileLayer(onemapUrlConfig.leadorStreetTileUrl,
  333. {
  334. minZoom: 1,
  335. maxZoom: 19,
  336. zIndex: 11
  337. });
  338. _this.tileLayerOverAddress.addTo(_this._map);*/
  339. _this.streetLayerGuid=map23DControl.tileLayer({
  340. action: 'add',
  341. layer: {
  342. url2D: onemapUrlConfig.leadorStreetTileUrl + '?l={z}&x={x}&y={y}',
  343. url3D: onemapUrlConfig.leadorStreetTileUrl + '?z=%d&x=%d&y=%d',
  344. minZoom: 1,
  345. maxZoom: 19,
  346. maxNativeZoom: 8,
  347. attribution: '',
  348. opacity: 1,
  349. imageType: 'png'
  350. }
  351. })
  352. },
  353. updateMarker23D:function(option,center){
  354. var _this = this;
  355. if(option === '2d'){
  356. if(_this._viewMarker){
  357. _this._viewMarker.setLatLng([center.y,center.x]);
  358. _this._showStreet3DView();
  359. }
  360. }else if(option === '3d'){
  361. if(_this._viewMarker){
  362. var latlng = _this._viewMarker.getLatLng();
  363. map3DViewer.marker({
  364. action: 'update',
  365. guid:_this.streetMarkerGuid,
  366. geojson: {
  367. "geometry": {
  368. "coordinates": [latlng.lng, latlng.lat]
  369. }
  370. }
  371. })
  372. }
  373. }
  374. },
  375. updateMarker3D:function(options){
  376. var _this = this;
  377. if(_this.streetMarkerGuid){
  378. map3DViewer.marker({
  379. action: 'update',
  380. guid:_this.streetMarkerGuid,
  381. geojson: {
  382. "geometry": {
  383. "coordinates": [options.x, options.y]
  384. }
  385. }
  386. })
  387. _this.updateMarker23D('2d',options)
  388. }
  389. },
  390. controlMyLayer:function(options){
  391. var _this = this;
  392. if(options == 'show'){
  393. map23DControl.tileLayer({
  394. action:'show',
  395. guid:_this.streetLayerGuid
  396. })
  397. }else if(options =='hide'){
  398. map23DControl.tileLayer({
  399. action:'hide',
  400. guid:_this.streetLayerGuid
  401. })
  402. }
  403. },
  404. /**
  405. * 移除路网瓦片
  406. * @private
  407. */
  408. _removeViewRoadTile:function(){
  409. var _this = this;
  410. //_this._map.removeLayer(_this.tileLayerOverAddress);
  411. map23DControl.tileLayer({
  412. action: 'remove',
  413. guid: _this.streetLayerGuid
  414. })
  415. _this.streetLayerGuid = null;
  416. },
  417. /**
  418. * 标志 移除
  419. * @private
  420. */
  421. _removeViewMarker: function () {
  422. var _this = this;
  423. map23DControl.group({
  424. action:'remove',
  425. guid:_this._groupId
  426. })
  427. _this._viewModal.style.left = '-10000px';
  428. /*map3DViewer.marker({
  429. action:'remove',
  430. guid:_this.streetMarkerGuid
  431. })*/
  432. _this.streetMarkerGuid = null;
  433. _this._isShow = false;
  434. LeadorStreetView.removeBinding();
  435. swfobject.removeSWF(document.getElementById('leadorStreetViewWrap').id);
  436. setTimeout(function(){
  437. document.getElementById('leadorStreetViewModal').innerHTML = _this._vieModalHtml;
  438. }, 300);
  439. },
  440. /**
  441. * 显示3d实景
  442. * @private
  443. */
  444. _showStreet3DView:function(){
  445. var _this = this;
  446. _this._viewLatlng = _this._viewMarker.getLatLng();
  447. if(!_this._isShow){
  448. _this._viewModal.style.left = '60px';
  449. _this._isShow = true;
  450. }
  451. LeadorStreetView.loadStreetViewByPosition(_this._viewLatlng.lng,_this._viewLatlng.lat);
  452. }
  453. });
  454. L.control.leadorStreetView = function(options){
  455. return new L.Control.LeadorStreetView(options);
  456. };