天地圖專題一:加載天地圖

最近公司的項目到了尾聲。總結一下項目中的一些技術。其中有涉及到天地圖的部分。 相似百度地圖,google地圖。 咱們要用一些它的公共接口,實現一些咱們的效果。  好比在地圖上顯示咱們的設備的位置,範圍,運行軌跡等等。javascript

大多數的功能其實都只是參考天地圖官方api。http://api.tianditu.com/api-new/webIndex.html。css

基本上要實現什麼功能就去看一下api。就能解決多半的問題。可是實際運用中的話可能只看接口還沒法靈活地運用起來。因此這裏寫一個天地圖系列文章,但願給廣大程序員同胞們一點參考。html

 

加載天地圖首先要引入官方的js:java

[html]  view plain  copy
 
  1. <script type="text/javascript" src="http://api.tianditu.com/js/maps.js"></script>  

給它一個放地圖的div。設置好樣式,id。git

而後調用給它一個縮放級別,以及一箇中心點,調用TMap();生成地圖放到頁面。縮放級別有18級。代碼:程序員

 

[html]  view plain  copy
 
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <meta charset="UTF-8" />  
  5.     <title>加載天地圖</title>  
  6.     <script type="text/javascript" src="http://api.tianditu.com/js/maps.js"></script>  
  7. </head>  
  8. <body>  
  9.     <style type="text/css">  
  10.         #mapDiv{  
  11.             height:600px;  
  12.             border: 1px solid #ccc;  
  13.             margin: 10px;  
  14.   
  15.         }  
  16.     </style>  
  17.     <div id="mapDiv"></div>  
  18.     <script type="text/javascript">  
  19.     function loadMap(){//加載基本地圖和導航  
  20.         try {  
  21.             map = new TMap("mapDiv"); //初始化地圖對象  
  22.              map.centerAndZoom(new TLngLat(103.388611,35.563611), 4);//設置顯示地圖的中心點和級別-中國  
  23.             map.enableHandleMouseScroll(); //容許鼠標雙擊放大地圖     
  24.         } catch(err) {  
  25.             alert('圖加載不成功,請稍候再試!你能夠先使用其餘功能!');  
  26.         }  
  27.     }  
  28.     loadMap();  
  29. </script>  
  30. </body>  
  31. </html>  



 

 

效果圖:github

 

這樣咱們就加載了一張基本的地圖了。web

更多的內容請關注本系列後續文章。api

 

項目下載地址:https://github.com/liusaint/tiandituMapgoogle

演示地址:http://runningls.com/demos/tianditu/index.html?to=loadmap

相關文章
相關標籤/搜索