GeoJson+Openlayers展現后台服務提供的單點、多點的元素信息,並依據條件變化元素如元素色彩、圖標


【目標1】通過Openlayers展現后台服務提供的一個點的元素信息。

 
 
【步驟】a.地圖服務、b.后台查詢點經緯度、c.后台拼寫JSON串服務、d.Ajax異步獲取JSON串、e.頁面展現
 
 a.地圖服務:依據個人地圖服務而定,這里不累述。
 
 b.后台查詢點經緯度:后台查詢數據庫,獲取點的經緯度[125.37673830988,43.858870032345],這里不累述。
 
 c.后台拼寫JSON串服務:通過http://download.csdn.net/source/2991502下載拼寫JSON的Jar包,拼寫JSON串方法如下:
  1.          /** 
  2.  * 添加一個點 
  3.  * @return 
  4.  */  
  5. public String getPoints() {  
  6.     Point g1 = new Point();  
  7.     g1.setPoint("[125.37673830988,43.858870032345]");  
  8.     Feature feature = new Feature(g1);  
  9.     FeatureCollection c = new FeatureCollection(feature);  
  10.     System.out.println(c.draw());  
  11.     return c.draw();  
  12. }  

輸出c.draw()如下:
  1. {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[125.37673830988,43.858870032345]}}]}  

d.Ajax異步獲取JSON串:頁面展現中,通過ajax異步獲取后台提供的JSON串,這里不累述。
 
e.頁面展現
[javascript] view plain copy print?
  1. <script type="text/javascript">  
  2. var map,vectors_map,vector_point,geojson;  
  3. function init(){  
  4.  //創建地圖  
  5.  map = ......;  
  6.  vectors_map = new OpenLayers.Layer.WMS(  
  7.   ......  
  8.  );  
  9.  //創建一個點圖層,用來展現我們從后台獲取的點信息  
  10.  vector_point = new OpenLayers.Layer.Vector();  
  11.  //將地圖圖層和點圖層加載到Map  
  12.  map.addLayers([vectors_map,vector_point]);  
  13.  //創建GeoJSON類對象,用於解析JSON串  
  14.  geojson = new OpenLayers.Format.GeoJSON();  
  15.  map.setCenter(new OpenLayers.LonLat(125.30593872070312, 43.87017822557581),5);  
  16. }  
  17. //1.這里定義的jsons應該是通過ajax從后台獲取的  
  18. var jsons = {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[124.82786, 45.1371017]}}]};  
  19. //2.通過后台獲取json串后,調用addJson()方法后,就可以將點展現到頁面。  
  20. function addJson(){  
  21.    vector_point.addFeatures(geojson.read(jsons));  
  22. }  

【效果】
 
 
 
【目標2】通過Openlayers展現后台服務提供的多個點的元素信息。
 
【步驟】基本和單點一樣,不同之處在{拼寫JSON串}
 
c.拼寫JSON串。通過http://download.csdn.net/source/2991502下載拼寫JSON的Jar包,拼寫JSON串方法如下:  
  1.         /** 
  2.  * 添加兩個點 
  3.  *  
  4.  * @return 
  5.  */  
  6. public String getPoints() {  
  7.     Point g1 = new Point();  
  8.     g1.setPoint("[124.70169067383,43.859191897451]");  
  9.     Feature feature = new Feature(g1);  
  10.     Point g2 = new Point();  
  11.     g2.setPoint("[125.37673830988,43.858870032345]");  
  12.     Feature feature2 = new Feature(g2);  
  13.   
  14.     List<Component> components = new ArrayList<Component>(2);  
  15.     components.add(feature2);  
  16.     components.add(feature);  
  17.   
  18.     FeatureCollection c = new FeatureCollection(components);  
  19.     System.out.println(c.draw());  
  20.     return c.draw();  
  21. }  
   
輸出c.draw()如下:
  1. {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[125.37673830988,43.858870032345]}},{"type":"Feature","geometry":{"type":"Point","coordinates":[124.70169067383,43.859191897451]}}]}  
 
【效果】 
 
 
 
【目標】:通過Openlayers展現后台服務提供的多個點的元素信息,並且根據狀態改變成不同的圖片信息。
 
【步驟】基本和多點一樣,不同之處在{拼寫JSON串}和{頁面展現}
 
c.拼寫JSON串。
  1.          /** 
  2.  * 添加兩個點 
  3.  *  
  4.  * @return 
  5.  */  
  6. public String getPoints() {  
  7.     Point g1 = new Point();  
  8.     g1.setPoint("[124.70169067383,43.859191897451 ]");  
  9.     Feature feature = new Feature(g1);  
  10.     Map<String, String> properties = new HashMap<String, String>();  
  11.     properties.put("image", "red.png");// 這里提供了點將使用的圖片,自己可以隨便定義  
  12.     feature.setProperties(properties);  
  13.   
  14.     Point g2 = new Point();  
  15.     g2.setPoint("[125.37673830988,43.858870032345 ]");  
  16.     Feature feature2 = new Feature(g2);  
  17.     Map<String, String> properties2 = new HashMap<String, String>();  
  18.     properties2.put("image", "yell.png");// 這里提供了點將使用的圖片,自己可以隨便定義  
  19.     feature2.setProperties(properties2);  
  20.   
  21.     List<Component> components = new ArrayList<Component>(2);  
  22.     components.add(feature);  
  23.     components.add(feature2);  
  24.   
  25.     FeatureCollection c = new FeatureCollection(components);  
  26.     System.out.println(c.draw());  
  27.     return c.draw();  
  28. }  

輸出c.draw()如下:
  1. {"type":"FeatureCollection","features":[{"type":"Feature","properties":{"image":"red.png"},"geometry":{"type":"Point","coordinates":[124.70169067383,43.859191897451 ]}},{"type":"Feature","properties":{"image":"yell.png"},"geometry":{"type":"Point","coordinates":[125.37673830988,43.858870032345 ]}}]}  

e.頁面展現
[javascript] view plain copy print?
  1. <script type="text/javascript">  
  2. var map,vectors_map,vector_point,geojson;  
  3. function init(){  
  4.  //創建地圖  
  5.  map = ......;  
  6.  vectors_map = new OpenLayers.Layer.WMS(  
  7.   ......  
  8.  );  
  9.    
  10. //新增部分,將對vector_point這個圖層定義一個樣式,不使用默認樣式  
  11.  var styleMap = new OpenLayers.StyleMap({  
  12.       "default": {  
  13.             fillOpacity: 1,  
  14.             strokeOpacity:1,  
  15.             strokeColor: "#000000",  
  16.             graphicWidth:30,  
  17.             graphicHeight:50,  
  18.             externalGraphic: "${image}"  
  19.          }  
  20.  });  
  21.  vector_point = new OpenLayers.Layer.Vector("Simple Geometry", {  
  22.       styleMap: styleMap,  
  23.       rendererOptions: {zIndexing: true}  
  24.  });  
  25.    
  26.  //創建一個點圖層,用來展現我們從后台獲取的點信息  
  27.  //屏蔽這句話vector_point = new OpenLayers.Layer.Vector();  
  28.  //將地圖圖層和點圖層加載到Map  
  29.  map.addLayers([vectors_map,vector_point]);  
  30.  //創建GeoJSON類對象,用於解析JSON串  
  31.  geojson = new OpenLayers.Format.GeoJSON();  
  32.  map.setCenter(new OpenLayers.LonLat(125.30593872070312, 43.87017822557581),5);  
  33. }  
  34. //1.這里定義的jsons應該是通過ajax從后台獲取的  
  35. var jsons = {"type":"FeatureCollection","features":[{"type":"Feature","properties":{"image":"red.png"},"geometry":{"type":"Point","coordinates":[124.70169067383,43.859191897451 ]}},{"type":"Feature","properties":{"image":"yell.png"},"geometry":{"type":"Point","coordinates":[125.37673830988,43.858870032345 ]}}]};  
  36. //2.通過后台獲取json串后,調用addJson()方法后,就可以將點展現到頁面。  
  37. function addJson(){  
  38.    vector_point.addFeatures(geojson.read(jsons));  
  39. }  

 
【效果C】
 
 
【注意】如果你的程序沒有出來效果,檢查兩個地方
 
1.自定義圖片的位置是否正確;
 
2.externalGraphic: "${image}"是否起作用了;
先寫一個固定的圖片,看看效果。externalGraphic: "red.png"
如果可以正常顯示,這表明$符號被屏蔽了。我們在開發中,頁面可能會使用到其他框架,比如DWR,JQuery。這個時候,有的框架會屏蔽$符號的作用。解決辦法在頁面添加 <%@ page isELIgnored ="true" %>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM