arcgis for android 空间查询

本文档详细介绍了如何使用ArcGIS for Android进行空间查询,包括查询地图上点是否在特定范围内,以及根据地名搜索地理位置。示例代码展示了如何执行地名搜索任务,获取地理位置并将其显示在地图上,同时提供了缩放和坐标转换功能。

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***************************************************



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值