|
1. Search介绍 有时可能做一些搜索,如搜索配送中心周围的零售店。MapXtreme提供了丰富的搜索功能。搜索的结果可以转换为被选中状态,从而显示在地图上。Search是Layer对象的方法,有: • searchAll • searchWithinRadius • searchWithinRegion • searchWithinRectangle • searchAtPoint • searchByAttribute • searchByPrimaryKey
使用这些查询都需要给出要返回的字段集合和执行的查询动作。返回的字段集合需要放在Vector对象中。下面的例子返回的是表中所有的字段: //Assume myLayer is a Layer object. TableInfo myTableInfo = myLayer.getTableInfo( ); Vector columnNames = new Vector( ); int columnCount = myTableInfo.getColumnCount( ); String col; for (int j=0; j<columnCount; j++) { col = myTableInfo.getColumnName(j); columnNames.addElement(col); } 在默认情况下,执行查询时后得到的FeatureSet包含Geometry,Rendition,LabelRendition,PrimaryKey,raster Data等,所以需要使用QueryParams 类来过滤这些信息,以提高程序的性能。 进行查询时可以通过QueryParams指定查询类型SearchType。Mbr表示搜索最小外接矩形和搜索范围相交的图元;patial表示搜索和搜索范围相交的图元;entire表示搜索完全在搜索范围的的图元。 下面代码示例如何使用QueryParams: QueryParams qp = new QueryParams(bGeometry,bRendition,bPrimarykey,bLabelPoint,bRasterInfo,bLabelRendition, SearchType.entire); 下面示例了如何过滤信息: // find all Features entirely within a given search region, return a single Attribute column //and noRendition information. Vector cols = new Vector(); cols.addElement(“County”); Feature searchFeature =mapj.getFeatureFactory().createRegion(points, rend,attribs,null); QueryParams queryParams = new QueryParams(true, false,true,true, true, true, SearchType.entire); FeatureSet fs = layer.searchWithinRegion(cols,searchFeature.getGeometry(), queryParams); 2. Search方法 : searchAll 示例: //Assume fs is a FeatureSet object.Assume columnNames is a vector of the columns //to be returned.Assume qp is the QueryParams object.Assume myLayer is a Layer //object. try { fs = myLayer.searchAll(columnNames,qp); } catch(exception e) { e.printStackTrace(); } :==>searchWithinRadius 示例: //Assume fs is a FeatureSet object. Assume columnNames is a vector of the desired //columns to be returned. Assume qp is the QueryParams object.Assume myLayer //is a Layer object. DoublePoint dblPt = new DoublePoint(-73.889444,42.765555); double dRadius = 10.03; try { fs = myLayer.searchWithinRadius(columnNames,dblPt,dRadius,LinearUnit.mile,qp); } catch(exception e) { e.printStackTrace(); } ==> searchWithinRegion 示例: private boolean layerSearchWithinRegion() { //Assume fs is a FeatureSet object.Assume columnNames is a vector of the columns //to be returned.Assume qp is the QueryParams object.Assume myLayer is a Layer //object.Assume vGeom is a VectorGeometry of TYPE_REGION try { fs =myLayer.searchWithinRegion(columnNames,vGeom,qp); } catch(Exception e) { e.printStackTrace(); return false; } return true; } ==> searchWithinRectangle 示例: //Assume fs is a FeatureSet object.Assume columnNames is a vector of the columns //to be returned.Assume qp is the QueryParams object.Assume myLayer is a Layer //object. DoubleRect dRect = new DoubleRect(-74.092662,42.765555,-73.668898,42.856420); try { fs =myLayer.searchWithinRectangle(columnNames,dRect,qp); } catch(exception e) { e.printStackTrace(); } ==>searchAtPoint 示例: //Assume fs is a FeatureSet object.Assume columnNames is a vector of the columns //to be returned.Assume qp is the QueryParams object.Assume myLayer is a Layer //object. DoublePoint dp = new DoublePoint(12.3456,-67.890); try { fs = myLayer.searchAtPoint(columnNames,dp,qp); } catch(exception e) { e.printStackTrace(); } ==> searchByAttribute 示例: //Assume fs is a FeatureSet object.Assume columnNames is a vector of the columns to //be returned.Assume qp is the QueryParams object.Assume myLayer is a Layer //object.Assume attr is the attribute to search against.Assume colName is the column to //search against. try { fs =myLayer.searchByAttribute(columnNames,colName,attr,qp); } catch(Exception e) { e.printStackTrace(); return false; } ==>searchByPrimaryKey 示例: 可以使用searchByPrimaryKey返回特定的FeatureSet。 private boolean layerSearchByPrimaryKey() { // Assume fs is a FeatureSet object. Assume columnNames is a vector of the //columns to be returned. Assume qp is the QueryParams object. Assume myLayer //is a Layer object. Assume colName is the column to search against. Assume pk is a //PrimaryKey from a previous search. PrimaryKey[] arraypk = new PrimaryKey[1]; arraypk[0] = pk; try { fs = myLayer.searchByPrimaryKey(columnNames,arraypk,qp); } catch(Exception e) { e.printStackTrace(); return false; } return true; } }
|