ArcGIS for Android 之Geocode查询的实现

还是一样,在进行一个新的基础功能的学习之前,肯定是需要去了解其API的解释和使用规则。

ArcGIS for Android 之Geocode查询的实现_第1张图片

package com.arcgis.android.samples.geocode;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.SimpleMarkerSymbol;
import com.esri.core.symbol.TextSymbol;
import com.esri.core.tasks.ags.geocode.Locator;
import com.esri.core.tasks.ags.geocode.LocatorFindParameters;
import com.esri.core.tasks.ags.geocode.LocatorGeocodeResult;

import java.util.List;

public class GeocodeActivity extends Activity {
	// create arcgis objects
	MapView mMapView;
	ArcGISTiledMapServiceLayer basemap;
	GraphicsLayer locationLayer;
	Locator locator;
	// create UI components
	static ProgressDialog dialog;
	static Handler handler;

	// Label instructing input for EditText
	TextView geocodeLabel;
	// Text box for entering address
	EditText addressText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// 创建handler来更新UI进程
		handler = new Handler();

		// Set the geocodeLabel with instructions
		geocodeLabel = (TextView) findViewById(R.id.geocodeLabel);
		geocodeLabel.setText(getString(R.string.geocode_label));

		// Get the addressText component
		addressText = (EditText) findViewById(R.id.addressText);

		// Retrieve the map and initial extent from XML layout
		mMapView = (MapView) findViewById(R.id.map);
		/* create a @ArcGISTiledMapServiceLayer */
		basemap = new ArcGISTiledMapServiceLayer(this.getResources().getString(
				R.string.basemap_url));
		// Add tiled layer to MapView
		mMapView.addLayer(basemap);
		// Add location layer
		locationLayer = new GraphicsLayer();
		mMapView.addLayer(locationLayer);
		// 左下角显示ESRI的logo
		mMapView.setEsriLogoVisible(true);

		// button的单击事件监听
		Button go = (Button) findViewById(R.id.go);
		go.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
//				locationLayer.removeAll();
				// 从输入框获得地址
				String address = addressText.getText().toString();
				// 将地址发送给地址转换method
				address2Pnt(address);
			}
		});
	}

	/*
	 * Convert input address into geocoded point 地点转化成经纬度的点
	 */
	private void address2Pnt(String address) {
		try {
			// create Locator parameters from single line address string
			LocatorFindParameters findParams = new LocatorFindParameters(
					address);
			// 设置搜索的国家
			findParams.setSourceCountry("CHN");
			// 限制搜索结果为2
			findParams.setMaxLocations(2);
			// set address spatial reference to match map
			findParams.setOutSR(mMapView.getSpatialReference());
			// execute async task to geocode address
			new Geocoder().execute(findParams);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		super.onPause();
		mMapView.pause();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
		mMapView.unpause();
	}

	/*
	 * Dismiss dialog when geocode task completes
	 */
	static public class MyRunnable implements Runnable {
		public void run() {
			dialog.dismiss();
		}
	}

	/*
	 * AsyncTask to geocode an address to a point location Draw resulting point
	 * location on the map with matching address
	 */
	private class Geocoder extends
			AsyncTask<LocatorFindParameters, Void, List<LocatorGeocodeResult>> {
		// The result of geocode task is passed as a parameter to map the
		// results
		protected void onPostExecute(List<LocatorGeocodeResult> result) {
			if (result == null || result.size() == 0) {
				// 当搜索失败,没有找到结果的时候给UI进程吐丝
				Toast toast = Toast.makeText(GeocodeActivity.this,
						"No result found.", Toast.LENGTH_LONG);
				toast.show();
			} else {
				// 当定位地址的时候显示进度条
				dialog = ProgressDialog.show(mMapView.getContext(), "Geocoder",
						"Searching for address ...");
				// get return geometry from geocode result
				Geometry resultLocGeom = result.get(0).getLocation();
				// 创建渲染器
				SimpleMarkerSymbol resultSymbol = new SimpleMarkerSymbol(
						Color.BLUE, 20, SimpleMarkerSymbol.STYLE.CIRCLE);
				// create graphic object for resulting location
				Graphic resultLocation = new Graphic(resultLocGeom,
						resultSymbol);
				// add graphic to location layer
				locationLayer.addGraphic(resultLocation);
				// create text symbol for return address
				TextSymbol resultAddress = new TextSymbol(12, result.get(0)
						.getAddress(), Color.BLACK);
				// create offset for text
				resultAddress.setOffsetX(10);
				resultAddress.setOffsetY(50);
				// create a graphic object for address text
				Graphic resultText = new Graphic(resultLocGeom, resultAddress);
				// add address text graphic to location graphics layer
				locationLayer.addGraphic(resultText);
				// zoom to geocode result
//				mMapView.zoomToResolution(result.get(0).getLocation(), 2);
				// create a runnable to be added to message queue
				handler.post(new MyRunnable());
			}
		}

		// invoke background thread to perform geocode task
		@Override
		protected List<LocatorGeocodeResult> doInBackground(
				LocatorFindParameters... params) {
			// get spatial reference from map
			SpatialReference sr = mMapView.getSpatialReference();
			// create results object and set to null
			List<LocatorGeocodeResult> results = null;
			// set the geocode service
			locator = new Locator(getResources()
					.getString(R.string.geocode_url));
			try {
				// pass address to find method to return point representing
				// address
				results = locator.find(params[0]);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return results;
		}
	}

}

其中xml文件设置的初始化显示范围是:initExtent = "1.2757645697558502E7 2576233.422813467 1.3480434236922514E7 3307582.9093442294"

于是,实现的效果如图:


这是初始化的界面。单击go按钮后,实现geocode的搜索。

ArcGIS for Android 之Geocode查询的实现_第2张图片

工程资源下载:http://download.csdn.net/detail/vaecer/5287456

个人总结:

从中得更改其中的一些功能。得到一些问题。首先就是,sample中的源代码在对go按钮的事件触发上,使用的触发方法有些独特,和我平时使用监听器的方法有很大的差别,但是哪个更好更加便捷节省资源,还有待继续研究。其中,官方的使用方法是,现在xml中的button中,添加一个android:onclick=“geocodego”;然后在java中创建一个geocodego的方法。这样整合出来的就是一个button的监听器的实现方法。由于本人觉得书写上官方有些混乱。所以我将其改成上面代码所示。

geocode的查询其实通过观察代码,是很容易明白它的使用方法。和query等没有多大的大区别,也没有比他们难。其实就是:

1.获得输入框中用户需要查询的地址;

2.将string的地址转换成point;

3.通过异步查询的方法,查询到该点在地图上的位置;

4.然后利用handler更新UI进程,把点显示在地图上,一切就OK啦!



你可能感兴趣的:(ArcGIS for Android 之Geocode查询的实现)