數(shù)據(jù)準(zhǔn)備
1、航線的相關(guān)數(shù)據(jù),google了一部分南航的數(shù)據(jù),只要有幾十條線路做演示就足夠用了。將它們保存在數(shù)據(jù)庫表中,主要有出發(fā)城市和到達(dá)城市兩個字段。
2、用作地圖顯示的行政區(qū)劃圖以及城市的點要素圖。為了簡化,只選擇了涉及上述航線的若干城市。城市數(shù)據(jù)是一個point的shapefile文件,其中有一個屬性列為城市名稱“name”,為了方便檢索,name屬性中保存的城市名稱與數(shù)據(jù)庫中航線信息所保存的城市名稱是匹配的。
實現(xiàn)方法
ADF提供了很好用的task framework,因此這個例子以自定義task的形式實現(xiàn)。先來簡單了解一下整個操作的流程:
用戶選擇了自定義工具欄提供的一個工具,與地圖進(jìn)行一個點查詢的交互,選擇一個起點城市。在服務(wù)器端根據(jù)用戶的操作將得到一個點的坐標(biāo),根據(jù)這個坐標(biāo)進(jìn)行查詢,得到這個坐標(biāo)點所表示的點要素的相關(guān)信息,這里我們所關(guān)心的只是這個城市的名稱。知道了城市名稱以后,以它作為起點城市,到數(shù)據(jù)庫中檢索所有從該城市出發(fā)的航線的目的地城市。這是一個字符串類型的數(shù)組,接下來可以根據(jù)這些城市名到地圖中查找它們對應(yīng)的點要素,并獲得這些點要素的坐標(biāo)。這樣,我們就有了一個起點和若干個終點的坐標(biāo),可以繪制航線了。好了,基本思路就是這樣,來看看怎么實現(xiàn)
先創(chuàng)建一個自定義task類,它有一個方法:
聲明了這個類以后,task framework就會根據(jù)getAirlines(MapEvent event)方法的參數(shù)來判斷這個工具的類型。由于MapEvent涉及的地圖交互操作有很多種,而這里我們需要的是點查詢操作,所以接下來我們需要再構(gòu)造一個類來給自定義task加一些說明:
然后將這個taskInfo類添加到我們的自定義task中:
完成了上面的準(zhǔn)備步驟以后,我們深入到getAirlines(MapEvent event)方法的內(nèi)部,看看它是怎么運行的:
所有的查詢操作都是通過WebQuery來進(jìn)行的,查詢的結(jié)果將通過WebGraphics繪制到地圖上。查詢的時候需要提供兩個信息,點的坐標(biāo)以及目標(biāo)圖層:
ok,用戶與地圖交互操作的查詢就完成了,接下來我們需要從查詢結(jié)果中得到這個城市的城市名。
這里需要解釋一下的是最后一句,也許有人會問,剛才我們不是已經(jīng)獲取了一個point坐標(biāo)了嗎,那個不就是起點城市么?事實上,我們在地圖上點擊查詢的時候不會恰好就點在表示城市的那個點的中心,WebQuery查詢的時候是有一個距離容錯的。而繪制航線的時候要求更精確,所以我們以查詢得到的點要素的坐標(biāo)為準(zhǔn)。
費了半天勁總算把城市名給找出來了,擦一把汗,接著查,我們還需要終點城市的坐標(biāo)呢!剛才我們用的是WebGeometry來查詢,接下來我們將使用文本進(jìn)行查詢。
到這里,航線的繪制功能就算完成了。后續(xù)的文章中將介紹如何將航線的其他信息添加到WebResults中,以及高亮顯示某一條航線的功能
1、航線的相關(guān)數(shù)據(jù),google了一部分南航的數(shù)據(jù),只要有幾十條線路做演示就足夠用了。將它們保存在數(shù)據(jù)庫表中,主要有出發(fā)城市和到達(dá)城市兩個字段。
2、用作地圖顯示的行政區(qū)劃圖以及城市的點要素圖。為了簡化,只選擇了涉及上述航線的若干城市。城市數(shù)據(jù)是一個point的shapefile文件,其中有一個屬性列為城市名稱“name”,為了方便檢索,name屬性中保存的城市名稱與數(shù)據(jù)庫中航線信息所保存的城市名稱是匹配的。
實現(xiàn)方法
ADF提供了很好用的task framework,因此這個例子以自定義task的形式實現(xiàn)。先來簡單了解一下整個操作的流程:
用戶選擇了自定義工具欄提供的一個工具,與地圖進(jìn)行一個點查詢的交互,選擇一個起點城市。在服務(wù)器端根據(jù)用戶的操作將得到一個點的坐標(biāo),根據(jù)這個坐標(biāo)進(jìn)行查詢,得到這個坐標(biāo)點所表示的點要素的相關(guān)信息,這里我們所關(guān)心的只是這個城市的名稱。知道了城市名稱以后,以它作為起點城市,到數(shù)據(jù)庫中檢索所有從該城市出發(fā)的航線的目的地城市。這是一個字符串類型的數(shù)組,接下來可以根據(jù)這些城市名到地圖中查找它們對應(yīng)的點要素,并獲得這些點要素的坐標(biāo)。這樣,我們就有了一個起點和若干個終點的坐標(biāo),可以繪制航線了。好了,基本思路就是這樣,來看看怎么實現(xiàn)
先創(chuàng)建一個自定義task類,它有一個方法:
CODE:
public class SearchAirlinesTask{
public void getAirlines(MapEvent event){
}
}
public void getAirlines(MapEvent event){
}
}
聲明了這個類以后,task framework就會根據(jù)getAirlines(MapEvent event)方法的參數(shù)來判斷這個工具的類型。由于MapEvent涉及的地圖交互操作有很多種,而這里我們需要的是點查詢操作,所以接下來我們需要再構(gòu)造一個類來給自定義task加一些說明:
CODE:
public class SearchAirlinesTaskInfo extends SimpleTaskInfo{
private TaskToolDescriptor[] taskTool = new TaskToolDescriptor[1];
public SearchAirlinesTaskInfo(){
taskTool[0] =
new TaskToolDescriptor(SearchAirlines.class,"getAirlines","選擇起點",ClientActions.MAP_POINT);
taskTool[0].setToolTip(“從地圖上選擇一個起點”);
}
public TaskToolDescriptorModel[] getToolDescriptors(){
return taskTool;
}
}
private TaskToolDescriptor[] taskTool = new TaskToolDescriptor[1];
public SearchAirlinesTaskInfo(){
taskTool[0] =
new TaskToolDescriptor(SearchAirlines.class,"getAirlines","選擇起點",ClientActions.MAP_POINT);
taskTool[0].setToolTip(“從地圖上選擇一個起點”);
}
public TaskToolDescriptorModel[] getToolDescriptors(){
return taskTool;
}
}
然后將這個taskInfo類添加到我們的自定義task中:
CODE:
public class SearchAirlinesTask{
private SearchAirlinesTaskInfo taskInfo = new SearchAirlinesTaskInfo();
public void getAirlines(MapEvent event){
}
public SimpleTaskInfo getTaskInfo(){
return taskInfo;
}
}
private SearchAirlinesTaskInfo taskInfo = new SearchAirlinesTaskInfo();
public void getAirlines(MapEvent event){
}
public SimpleTaskInfo getTaskInfo(){
return taskInfo;
}
}
完成了上面的準(zhǔn)備步驟以后,我們深入到getAirlines(MapEvent event)方法的內(nèi)部,看看它是怎么運行的:
CODE:
public getAirlines(MapEvent event){
WebContext ctx = event.getWebContext();
WebGraphics graphics = ctx.getWebGraphics();
WebQuery query = ctx.getWebQuery();
}
WebContext ctx = event.getWebContext();
WebGraphics graphics = ctx.getWebGraphics();
WebQuery query = ctx.getWebQuery();
}
所有的查詢操作都是通過WebQuery來進(jìn)行的,查詢的結(jié)果將通過WebGraphics繪制到地圖上。查詢的時候需要提供兩個信息,點的坐標(biāo)以及目標(biāo)圖層:
CODE:
WebPoint point =
(WebPoint)event.getWebGeometry().toMapGeometry(ctx.getWebMap());
IdentifyCriteria ic = new IdentifyCriteria(point);
ic.setMaxRecordCount(1);
//只需要查詢city圖層,該圖層保存的是城市的相關(guān)信息
List layers = query.getQueryLayers();
List<WebLayerInfo> queryLayer = new ArrayList<WebLayerInfo>();
for(Iterator iter=layers.iterator(); iter.hasNext();){
Object item = iter.next();
if(item instanceof WebLayerInfo){
WebLayerInfo layerinfo = (WebLayerInfo)item;
if(layerinfo.getName().equals(“city”)){
queryLayer.add(layerinfo);
}
}
}
List rs = query.query(ic, queryLayer);
(WebPoint)event.getWebGeometry().toMapGeometry(ctx.getWebMap());
IdentifyCriteria ic = new IdentifyCriteria(point);
ic.setMaxRecordCount(1);
//只需要查詢city圖層,該圖層保存的是城市的相關(guān)信息
List layers = query.getQueryLayers();
List<WebLayerInfo> queryLayer = new ArrayList<WebLayerInfo>();
for(Iterator iter=layers.iterator(); iter.hasNext();){
Object item = iter.next();
if(item instanceof WebLayerInfo){
WebLayerInfo layerinfo = (WebLayerInfo)item;
if(layerinfo.getName().equals(“city”)){
queryLayer.add(layerinfo);
}
}
}
List rs = query.query(ic, queryLayer);
ok,用戶與地圖交互操作的查詢就完成了,接下來我們需要從查詢結(jié)果中得到這個城市的城市名。
CODE:
if(rs.size() > 0){
Iterator iter = rs.iterator();
QueryResult item = (QueryResult)iter.next();
Object obj = item.getDetails().get(“name”);
String startCity = obj.toString();
WebPoint start = (WebPoint)item.getHighlightGeometry();
}
Iterator iter = rs.iterator();
QueryResult item = (QueryResult)iter.next();
Object obj = item.getDetails().get(“name”);
String startCity = obj.toString();
WebPoint start = (WebPoint)item.getHighlightGeometry();
}
這里需要解釋一下的是最后一句,也許有人會問,剛才我們不是已經(jīng)獲取了一個point坐標(biāo)了嗎,那個不就是起點城市么?事實上,我們在地圖上點擊查詢的時候不會恰好就點在表示城市的那個點的中心,WebQuery查詢的時候是有一個距離容錯的。而繪制航線的時候要求更精確,所以我們以查詢得到的點要素的坐標(biāo)為準(zhǔn)。
費了半天勁總算把城市名給找出來了,擦一把汗,接著查,我們還需要終點城市的坐標(biāo)呢!剛才我們用的是WebGeometry來查詢,接下來我們將使用文本進(jìn)行查詢。
CODE:
DbAirlineSearch dboper = new DbAirlineSearch();
String[] destinations = dboper.getDestinations(startCity);
//終點城市的坐標(biāo)
WebPoint[] ends = new WebPoint[destinations.length];
//航線
WebPath[] airlines = new WebPath[destinations.length];
//在WebGraphics中繪制航線所需的要素
GraphicElement[] linesElements =
new GraphicElement[destinations.length];
for(int i=0;i<destinations.length;i++){
//設(shè)置文本查詢的條件
TextCriteria tc = new TextCriteria();
List<String> searchFields = new ArrayList<String>();
searchFields.add(“name”);
tc.setSearchFields(searchFields);
tc.setSearchText(destinations[i]);
List rs2 = query.query(tc,queryLayer);
//處理文本查詢的結(jié)果
if(rs2.size() > 0){
QueryResult destination = (QueryResult)rs2.iterator.next();
ends[i] = (WebPoint)destination.getHighlightGeometry();
//得到一個終點坐標(biāo)以后就可以繪制一條航線了
List<WebPoint> pointList = new ArrayList<WebPoint>();
pointList.add(start);
pointList.add(ends[i]);
airlines[i] = new WebPath(pointList);
WebPolyline polyline = new WebPolyline();
polyline.addPath(airlines[i]);
linesElement[i] = new GraphicElement();
linesElement[i].setGeometry(polyline);//這里需要說明一下,如果這里用的是WebPath,將不會繪制出航線,換成WebPolyline以后就可以了。
linesElement[i].setSymbol(lineSymbol);
graphics.addGraphics(linesElement[i]);
}
}
String[] destinations = dboper.getDestinations(startCity);
//終點城市的坐標(biāo)
WebPoint[] ends = new WebPoint[destinations.length];
//航線
WebPath[] airlines = new WebPath[destinations.length];
//在WebGraphics中繪制航線所需的要素
GraphicElement[] linesElements =
new GraphicElement[destinations.length];
for(int i=0;i<destinations.length;i++){
//設(shè)置文本查詢的條件
TextCriteria tc = new TextCriteria();
List<String> searchFields = new ArrayList<String>();
searchFields.add(“name”);
tc.setSearchFields(searchFields);
tc.setSearchText(destinations[i]);
List rs2 = query.query(tc,queryLayer);
//處理文本查詢的結(jié)果
if(rs2.size() > 0){
QueryResult destination = (QueryResult)rs2.iterator.next();
ends[i] = (WebPoint)destination.getHighlightGeometry();
//得到一個終點坐標(biāo)以后就可以繪制一條航線了
List<WebPoint> pointList = new ArrayList<WebPoint>();
pointList.add(start);
pointList.add(ends[i]);
airlines[i] = new WebPath(pointList);
WebPolyline polyline = new WebPolyline();
polyline.addPath(airlines[i]);
linesElement[i] = new GraphicElement();
linesElement[i].setGeometry(polyline);//這里需要說明一下,如果這里用的是WebPath,將不會繪制出航線,換成WebPolyline以后就可以了。
linesElement[i].setSymbol(lineSymbol);
graphics.addGraphics(linesElement[i]);
}
}
到這里,航線的繪制功能就算完成了。后續(xù)的文章中將介紹如何將航線的其他信息添加到WebResults中,以及高亮顯示某一條航線的功能
