arcgis for android 空间查询

来源:互联网 发布:转移矩阵怎么求 编辑:程序博客网 时间:2024/06/12 04:20

arcgis for android 参数设置:


//查询点击地图上任意点判断是否在一个范围内(地图坐标用的大地坐标)

Point point = mapView.toMapPoint(x, y);//投影坐标转换成经纬度Point wgsPoint = (Point) GeometryEngine.project(point, mapView.getSpatialReference(), SpatialReference.create(4326));

QueryParameters qParameters = new QueryParameters();    //创建查询参数对象SpatialReference sr = SpatialReference.create(4326);//设置空间参考坐标系Envelope envelope = new Envelope(wgsPoint);//后面2个参数指的是点的高和低qParameters.setGeometry(envelope);//设置识别位置qParameters.setOutSpatialReference(sr);//设置输出坐标系qParameters.setReturnGeometry(true);//指定是否返回几何对象qParameters.setSpatialRelationship(SpatialRelationship.WITHIN);  //需要指定关系



//显示一个面的中心

Polygon polygon = (Polygon) feature.getGeometry();Polygon poly = new Polygon();//创建多边形对象//获取面的中心Point pyPoint = GeometryEngine.getLabelPointForPolygon(polygon, mapView.getSpatialReference());//经纬度转换成投影坐标pyPoint = (Point) GeometryEngine.project(pyPoint, SpatialReference.create(4326), mapView.getSpatialReference());for (int i = 0; i < polygon.getPointCount(); i++) {    Point wgsPoint = polygon.getPoint(i);    Point point = (Point) GeometryEngine.project(wgsPoint, SpatialReference.create(4326), mapView.getSpatialReference());    if (i == 0) {        poly.startPath(point);    } else if (i == polygon.getPointCount() - 1) {        poly.lineTo(poly.getPoint(0));//多边形是闭合的因此最后我们还要添加初始点的位置    } else {        poly.lineTo(point);    }}SimpleFillSymbol symbol = new SimpleFillSymbol(Color.RED);Graphic graphic = new Graphic(poly, symbol, feature.getAttributes());mPoythLayer.addGraphic(graphic);


//********************************************************根据地名搜索******************************************************************* 

/**

     * 根据地名查询经纬度
     */
    private void searchPlace() {
        String address = etSearch.getText().toString().trim();
//
        LocatorFindParameters findParams = new LocatorFindParameters(address);
        // Use the centre of the current map extent as the find location point
        findParams.setLocation(mapView.getCenter(), mapView.getSpatialReference());
        // Calculate distance for find operation
        Envelope mapExtent = new Envelope();
        mapView.getExtent().queryEnvelope(mapExtent); // assume map is in metres, other units wont work, double current envelope double distance = (mapExtent != null && mapExtent.getWidth() > 0) ? mapExtent.getWidth() * 2 : 10000; findParams.setDistance(distance); findParams.setMaxLocations(2);
        // Set address spatial reference to match map
        findParams.setOutSR(mapView.getSpatialReference());
//                    executeLocatorTask(findParams);
        if (task != null && AsyncTask.Status.RUNNING == task.getStatus()) {
            task.cancel(true);
        }
        new LocatorAsyncTask().execute(findParams);

    }


/**
     * 根据地名搜索市政府的地理位置
     */
    private class LocatorAsyncTask extends AsyncTask<LocatorFindParameters, Void, List<LocatorGeocodeResult>> {
        private Exception mException = null;




        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog.show();
        }


        @Override
        protected List<LocatorGeocodeResult> doInBackground(LocatorFindParameters... params) {
            Locator locator = Locator.createOnlineLocator();
            List<LocatorGeocodeResult> results = null;
            try {
                results = locator.find(params[0]);
            } catch (Exception e) {
//              e.printStackTrace();
                mException = e;
            }


            return results;
        }


        @Override
        protected void onPostExecute(List<LocatorGeocodeResult> result) {
            dialog.dismiss();
            if (mException != null) {
                Log.e("PlaceSearch", "LocatorSyncTask failed with:");
                mException.printStackTrace();
                ToastUtils.show(MapQueryActivity.this, "地址搜索失败.");
                return;
            }
            if (result.size() == 0) {
                ToastUtils.show(MapQueryActivity.this, "没有搜索出结果.");
            } else {
                // Use first result in the list
                LocatorGeocodeResult geocodeResult = result.get(0);
                // get return geometry from geocode result
                Point resultPoint = geocodeResult.getLocation();
                // create marker symbol to represent location
                SimpleMarkerSymbol resultSymbol = new SimpleMarkerSymbol(Color.RED, width, SimpleMarkerSymbol.STYLE.DIAMOND);
                // create graphic object for resulting location
                Graphic resultLocGraphic = new Graphic(resultPoint, resultSymbol);
                // add graphic to location layer
                mLocationLayer.addGraphic(resultLocGraphic);




                // create text symbol for return address
//                String address = geocodeResult.getAddress();
//                TextSymbol resultAddress = new TextSymbol(20, address, Color.BLACK);
//                resultAddress.setOffsetX(-4 * address.length());
//                resultAddress.setOffsetY(10);
//                Graphic resultText = new Graphic(resultPoint, resultAddress);
//                // add address text graphic to location graphics layer
//                mLocationLayer.addGraphic(resultText);
//                mLocationLayerPoint = resultPoint;




                // Zoom map to geocode result location
                mapView.zoomToScale(geocodeResult.getLocation(), 5);
                //投影坐标转换经纬度
                //  Point wgsPoint = (Point) GeometryEngine.project(resultPoint ,mapView.getSpatialReference(),SpatialReference.create(4326));
                screenPoint = mapView.toScreenPoint(resultPoint);//投影坐标换屏幕坐标
//                Log.e("Log.e","================X========"+wgsPoint.getX()+"===YYY==="+wgsPoint.getY()+"==="+mapView.getSpatialReference());
            }
        }
    }

//*********************************************************************end**************************************************************************


//*******************************************************根据字段检索**********************************************************************


   /**     * 查询图层属性Task     */    private void QueryFereatureTask(String param) {        if (queryTask != null && AsyncTask.Status.RUNNING == queryTask.getStatus()) {            queryTask.cancel(true);        }        queryTask = new QueryFeatureLayer(param);//        CompatibleUtils.executeTask(queryTask);//        queryTask.execute(param);        CompatibleUtils.executeTask(queryTask);        if (dialog == null) {            dialog = CustomProgressDialog.createDialog(this);        }        dialog.show();    }
  * 要素查询(查询层中的字段)     */    private class QueryFeatureLayer extends AsyncTask<Object,Void, FeatureResult> {        private Exception mException;        private String whereClause;        public QueryFeatureLayer(String param){            whereClause=param;        }        @Override        protected FeatureResult doInBackground(Object... objects) {//            String whereClause = "d_shi_mc ='" + params[0] + "' or d_shi_mc ='"+params[0]+"' or x_qi_mc ='"+params[0]+"' or x_zheng_mc ='"+params[0]+"'";            // Define a new query and set parameters            QueryParameters mParams = new QueryParameters();            mParams.setWhere(whereClause);            mParams.setReturnGeometry(true);            mParams.setOutFields(new String[]{"ttcode","yjz","qd","yxl","dmlx","dxzzzd","fldj","dkmj","d_shi_mc","x_qi_mc","x_zheng_mc","cunmc,nhmc","dkmc"});            QueryTask queryTask = new QueryTask(URL_PLOT + "/0");            FeatureResult results;            try {                results = queryTask.execute(mParams);                return results;            } catch (Exception e) {                mException = e;//                e.printStackTrace();            }            return null;        }        @Override        protected void onPostExecute(FeatureResult results) {            if (dialog != null) {                dialog.dismiss();            }            if (mException != null || results == null || results.featureCount() <= 0) {                Toast.makeText(MapQueryActivity.this, "沒有搜索到该地块信息.", Toast.LENGTH_SHORT).show();                return;            }            loadQueryPoint(results);            //设置地图中心点//            mapView.centerAt(point, true);//            MapOptions mapOptions = new MapOptions(MapOptions.MapType.OSM);//            mapOptions.setZoom(13);//            mapView.setMapOptions(mapOptions);        }    }

//****************************************************android离线查询(.geoDatabase)**************************************************


 /**     * 加载geodatabase文件图层     *     * @param featureLayerPath     */    private void updateFeatureLayer(String featureLayerPath) {        try {            localGdb = new Geodatabase(featureLayerPath);        } catch (FileNotFoundException e) {            e.printStackTrace();        }        if (localGdb != null) {            gdbFeautureTable = localGdb.getGeodatabaseFeatureTableByLayerId(0);            //如果有多个图层这需要这样,否则不需要//            for(GeodatabaseFeatureTable gdbFeautureTable:gdb.getGeodatabaseTables()){//                if(gdbFeautureTable.hasGeometry()){//                    mapView.addLayer(new FeatureLayer(gdbFeautureTable)); //地块//                }//            }            FeatureLayer feautreLayer = new FeatureLayer(gdbFeautureTable);            mapView.addLayer(feautreLayer); //地块//            localGdb.dispose();//释放资源如果放開ze 不能查詢        }    }

private void inputSearch() {    InputUtil.getInstance(MapQueryActivity.this).hide();    if (etSearch.getText().toString().equals("")) {        Toast.makeText(MapQueryActivity.this, "请输入要查询的地名.", Toast.LENGTH_SHORT).show();        return;    }    String param = etSearch.getText().toString().trim();    String whereClause = "s_shi_mc like '%" + param + "%' or d_shi_mc like '%" + param + "%' or d_shi_mc like '%" + param + "%' or x_qi_mc like '%" + param + "%' or x_zheng_mc like '%" + param + "%'";    if (this.gdbFeautureTable != null) {//离线查询        QueryParameters mParams = new QueryParameters();        mParams.setWhere(whereClause);        mParams.setReturnGeometry(true);        //要返回的字段        mParams.setOutFields(new String[]{"s_shi_mc", "ttcode", "yjz", "qd", "yxl", "dmlx", "dxzzzd", "fldj", "Area", "d_shi_mc", "x_qi_mc", "x_zheng_mc", "cunmc", "nhmc", "dkmc", "area_code"});        gdbFeautureTable.queryFeatures(mParams, callbackPointListener);        if (dialog == null) {            dialog = CustomProgressDialog.createDialog(this);        }        dialog.show();        // feautreLayer.selectFeatures(mParams, FeatureLayer.SelectionMode.NEW,callbackPointListener);    } }
/** * 根据字段查询回调 */CallbackListener<FeatureResult> callbackPointListener = new CallbackListener<FeatureResult>() {    @Override    public void onCallback(final FeatureResult result) {        runOnUiThread(new Runnable() {            @Override            public void run() {                if (dialog != null) {                    dialog.dismiss();                }                if (result != null && result.featureCount() > 0) {                    loadQueryPoint(result);                } else {                    Toast.makeText(MapQueryActivity.this, "沒有搜索到该地块信息.", Toast.LENGTH_SHORT).show();                }            }        });    }    @Override    public void onError(Throwable throwable) {        runOnUiThread(new Runnable() {            @Override            public void run() {                if (dialog != null) {                    dialog.dismiss();                }                Toast.makeText(MapQueryActivity.this, "沒有搜索到该地块信息.", Toast.LENGTH_SHORT).show();            }        });    }};
//*******************************************end***************************************************



0 0
原创粉丝点击