ArcGIS for Android示例解析之地理编码服务-----AGS_Locator

 

AGS_Locator

上面我们介绍了GP服务的示例,下面我们介绍一下地理编码服务是如何调用的,在示例中总共涉及到了两种查询方式的运用,一个是地理编码查询,一个是反地理编码查询,首先我们来看地理编码查询,代码如下:

/*

       * 执行地理编码查询.

       */

      class AgsLocator

                 extends

                 AsyncTask<java.util.Map<java.lang.String, java.lang.String>, Void, java.util.List<LocatorGeocodeResult>> {

 

           @Override

           protected void onPostExecute(java.util.List<LocatorGeocodeResult> result) {

 

                 if (result.size() == 0) {

                      Toast toast = Toast.makeText(AGS_Locator.this,

                                  "No result found.", Toast.LENGTH_SHORT);

                      toast.show();

 

                 } else {

                      dialog = ProgressDialog.show(AGS_Locator.this, "",

                                  "Loading. Please wait...", true, true);

                      cancelLocate = new Timer();

                      cancelLocate.schedule(new TimerTask() {

 

                            @Override

                            public void run() {

                                  uiHandler.sendEmptyMessage(CANCEL_LOADING_WINDOW);

                            }

 

                      }, 60000);

 

                      // Create graphic to add locator result to map

                     

                      Geometry geomS = result.get(0).getLocation();

                      SimpleMarkerSymbol smsS = new SimpleMarkerSymbol(Color.RED, 10, SimpleMarkerSymbol.STYLE.DIAMOND);

                      Graphic graphicS = new Graphic( geomS, smsS );

                     

                      Geometry geomT = result.get(0).getLocation();

                      TextSymbol tsT = new TextSymbol(16, result.get(0).getAddress(), Color.BLACK);

                      tsT.setOffsetX(15);

                      tsT.setOffsetY(15);

                      Graphic graphicT = new Graphic( geomT, tsT );

 

                      /*

                       * add the updated graphic to graphics layer and display the

                       * result on the map

                       */

                      graphicsLayer.addGraphic(graphicS);

                      graphicsLayer.addGraphic(graphicT);

 

                      // zoom to the locator result

                      map.zoomTo(result.get(0).getLocation(), 2);

 

                      uiHandler.sendEmptyMessage(CLOSE_LOADING_WINDOW);

 

 

                 }

           }

 

           @Override

           protected List<LocatorGeocodeResult> doInBackground(

                      Map<String, String>... params) {

 

                 int wkid = map.getSpatialReference().getID();

                 List<LocatorGeocodeResult> results = null;

 

                 // 初始化地理编码服务

                 al = new Locator(

                            "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Locators/ESRI_Geocode_USA/GeocodeServer");

                 try {

 

                      // 地理编码查询

                      results = al.geocode(params[0], null, wkid);

                 } catch (Exception e) {

                      e.getMessage();

                 }

                 return results;

           }

 

      }

下面来看一下反地理编码的代码:

map.setOnSingleTapListener(new OnSingleTapListener() {

 

                 private static final long serialVersionUID = 1L;

 

                 public void onSingleTap(final float x, final float y) {

 

 

                      graphicsLayer.removeAll();

 

                      // retrieve the user clicked location

                      final Point loc = map.toMapPoint(x, y);

 

                      // i初始化地理编码服务

                      al = new Locator(

                                  "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Locators/ESRI_Geocode_USA/GeocodeServer");

                      try {

 

                            int wkid = map.getSpatialReference().getID();

                            // 使其变成反地理编码并进行查询

                            LocatorReverseGeocodeResult result = al.reverseGeocode(loc,

                                       1000.00, wkid, wkid);

通过上面两部分的代码,我们可以看出地理编码和反地理编码就是两个相反的操作过程,简单点阐述一下他们之间的关系就是,地理编码是我们输入一个地址进行查询返回相对应的空间信息,而反地理编码就是输入一个空间信息而返回对应的地址数据。

你可能感兴趣的:(ArcGIS for Android示例解析之地理编码服务-----AGS_Locator)