在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → android 高德地图定位、周边、关键字搜索等

android 高德地图定位、周边、关键字搜索等

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:11.00M
  • 下载次数:79
  • 浏览次数:827
  • 发布时间:2016-07-04
  • 实例类别:Android平台开发
  • 发 布 人:kkkkzj
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 地图 定位 高德

实例介绍

【实例简介】集高德地图定位、周边、关键字搜索

【实例截图】

 

【核心代码】

package com.example.gaodemap;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.location.Criteria;
import android.location.LocationManager;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.AMap.InfoWindowAdapter;
import com.amap.api.maps.AMap.OnInfoWindowClickListener;
import com.amap.api.maps.AMap.OnMapClickListener;
import com.amap.api.maps.AMap.OnMarkerClickListener;
import com.amap.api.maps.AMapException;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CircleOptions;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.NaviPara;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.core.SuggestionCity;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.Inputtips.InputtipsListener;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.amap.api.services.poisearch.PoiSearch.OnPoiSearchListener;
import com.amap.api.services.poisearch.PoiSearch.SearchBound;


public class MainActivity extends Activity implements LocationSource,
		AMapLocationListener, OnClickListener, 
		OnMapClickListener, OnInfoWindowClickListener, InfoWindowAdapter, OnMarkerClickListener, 
		OnPoiSearchListener ,OnCheckedChangeListener,TextWatcher,InputtipsListener{
	MapView mMapView = null;
	private AMap aMap;
	private OnLocationChangedListener mListener;
	private AMapLocationClient mlocationClient;
	private AMapLocationClientOption mLocationOption;
	private RadioGroup mGPSModeGroup;
	private LocationManager locationManager;
	double x, y;
	private TextView mLocationErrText;
	public AMapLocationListener mLocationListener = new MyListener();

	// 周边
	private PoiResult poiResult; // poi返回的结果
	private int currentPage = 0;// 当前页面,从0开始计数
	private PoiSearch.Query query;// Poi查询条件类
	private LatLonPoint lp=new LatLonPoint(x, y);
	private Marker locationMarker; // 选择的点
	private Marker detailMarker;
	private Marker mlastMarker;
	private PoiSearch poiSearch;
	String cityaddress;
	private myPoiOverlay poiOverlay;// poi图层
	private List<PoiItem> poiItems;// poi数据
	private PoiItem mPoi;

	private RelativeLayout mPoiDetail;
	private TextView mPoiName, mPoiAddress;
	private String keyWord = "";//要输入的poi搜索关键字
	private EditText mSearchText;
	
	private AutoCompleteTextView searchText;// 输入搜索关键字
	private ProgressDialog progDialog = null;// 搜索时进度条
	private EditText editCity;// 要输入的城市名字或者城市区号

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 不显示程序的标题栏
		setContentView(R.layout.activity_main);
		mMapView = (MapView) findViewById(R.id.map);
		mMapView.onCreate(savedInstanceState);
		// aMap.setMapType(aMap.MAP_TYPE_NIGHT);

		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		init();
	}

	private void init() {
		mGPSModeGroup = (RadioGroup) findViewById(R.id.gps_radio_group);
		mGPSModeGroup.setOnCheckedChangeListener(this);
		mLocationErrText = (TextView) findViewById(R.id.location_errInfo_text);
		mLocationErrText.setVisibility(View.GONE);
		
		if (aMap == null) {
			aMap = mMapView.getMap();
			aMap.setOnMapClickListener(this);
			aMap.setOnMarkerClickListener(this);
			aMap.setOnInfoWindowClickListener(this);
			aMap.setInfoWindowAdapter(this);
			TextView searchButton = (TextView) findViewById(R.id.btn_search);
			searchButton.setOnClickListener(this);
			locationMarker = aMap
					.addMarker(new MarkerOptions()
							.anchor(0.5f, 0.5f)
							.icon(BitmapDescriptorFactory
									.fromBitmap(BitmapFactory.decodeResource(
											getResources(), R.drawable.point4)))
							.position(
									new LatLng(lp.getLatitude(), lp
											.getLongitude())));
			locationMarker.showInfoWindow();

		}
		setUpMap();
		aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
				new LatLng(lp.getLatitude(), lp.getLongitude()), 14));

	}

	private void setUpMap() {
		mPoiDetail = (RelativeLayout) findViewById(R.id.poi_detail);
		Button searButton = (Button) findViewById(R.id.searchButton);
		searButton.setOnClickListener(this);
		Button nextButton = (Button) findViewById(R.id.nextButton);
		nextButton.setOnClickListener(this);
		searchText = (AutoCompleteTextView) findViewById(R.id.keyWord);
		searchText.addTextChangedListener(this);// 添加文本输入框监听事件
		editCity = (EditText) findViewById(R.id.city);
		mPoiDetail.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// Intent intent = new Intent(PoiSearchActivity.this,
				// SearchDetailActivity.class);
				// intent.putExtra("poiitem", mPoi);
				// startActivity(intent);

			}
		});
		mPoiName = (TextView) findViewById(R.id.poi_name);
		mPoiAddress = (TextView) findViewById(R.id.poi_address);
		mSearchText = (EditText) findViewById(R.id.input_edittext);
		
		
		aMap.setLocationSource(this);// 设置定位监听
		aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
		aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
		// 设置定位的类型为定位模式 ,可以由定位、跟随或地图根据面向方向旋转几种
		aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
		
		aMap.moveCamera(CameraUpdateFactory.zoomTo(20));// 设置显示比例
		mlocationClient = new AMapLocationClient(getApplicationContext());
		mlocationClient.setLocationListener(mLocationListener);

		mLocationOption = new AMapLocationClientOption();
		mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
		mLocationOption.setNeedAddress(true);
		mLocationOption.setOnceLocation(false);
		mLocationOption.setWifiActiveScan(true);
		mLocationOption.setMockEnable(false);
		mLocationOption.setInterval(2000);
		mlocationClient.setLocationOption(mLocationOption);
		mlocationClient.startLocation();
	}
	
	protected void doSearchQuery() {
		
		keyWord = mSearchText.getText().toString().trim();
		currentPage = 0;
		query = new PoiSearch.Query(keyWord, "", "");// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)
		query.setPageSize(20);// 设置每页最多返回多少条poiitem
		query.setPageNum(currentPage);// 设置查第一页

		if (lp != null) {
			poiSearch = new PoiSearch(this, query);
			poiSearch.setOnPoiSearchListener(this);
			poiSearch.setBound(new SearchBound(lp, 5000, true));//
			// 设置搜索区域为以lp点为圆心,其周围5000米范围
			poiSearch.searchPOIAsyn();// 异步搜索
		}
	}
	/**
	 * 点击搜索按钮
	 */
	public void searchButton() {
		keyWord = AMapUtil.checkEditText(searchText);
		if ("".equals(keyWord)) {
			ToastUtil.show(MainActivity.this, "请输入搜索关键字");
			return;
		} else {
			doSearchQuery2();
		}
	}
	private void doSearchQuery2() {
		showProgressDialog();// 显示进度框
		currentPage = 0;
		query = new PoiSearch.Query(keyWord, "", cityaddress);// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)
		query.setPageSize(10);// 设置每页最多返回多少条poiitem
		query.setPageNum(currentPage);// 设置查第一页

		poiSearch = new PoiSearch(this, query);
		poiSearch.setOnPoiSearchListener(this);
		poiSearch.searchPOIAsyn();
		
	}

	/**
	 * 显示进度框
	 */
	private void showProgressDialog() {
		if (progDialog == null)
			progDialog = new ProgressDialog(this);
		progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progDialog.setIndeterminate(false);
		progDialog.setCancelable(false);
		progDialog.setMessage("正在搜索:\n"   keyWord);
		progDialog.show();
	}
	/**
	 * 隐藏进度框
	 */
	private void dissmissProgressDialog() {
		if (progDialog != null) {
			progDialog.dismiss();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
		mMapView.onDestroy();
	}

	@Override
	protected void onResume() {
		super.onResume();
		// 在activity执行onResume时执行mMapView.onResume (),实现地图生命周期管理
		mMapView.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		// 在activity执行onPause时执行mMapView.onPause (),实现地图生命周期管理
		mMapView.onPause();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		// 在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState
		// (outState),实现地图生命周期管理
		mMapView.onSaveInstanceState(outState);
	}

	/**
	 * 定位成功后回调函数
	 */
	public void onLocationChanged(AMapLocation amapLocation) {
		if (mListener != null && amapLocation != null) {
			if (amapLocation != null && amapLocation.getErrorCode() == 0) {
				mListener.onLocationChanged(amapLocation);// 显示系统小蓝点
				x = amapLocation.getLatitude();
				y = amapLocation.getLongitude();
				lp=new LatLonPoint(x, y);
//				Toast.makeText(MainActivity.this, String.valueOf(x y), Toast.LENGTH_LONG).show();
			} else {
				String errText = "定位失败,"   amapLocation.getErrorCode()   ": "
						  amapLocation.getErrorInfo();
				Log.e("AmapErr", errText);
			}
		}
	}

	/**
	 * 激活定位
	 */
	public void activate(OnLocationChangedListener listener) {
		mListener = listener;
		if (mlocationClient == null) {
			mlocationClient = new AMapLocationClient(this);
			mLocationOption = new AMapLocationClientOption();
			// 设置定位监听
			mlocationClient.setLocationListener(this);
			// 设置为高精度定位模式
			mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
			// 设置定位参数
			mlocationClient.setLocationOption(mLocationOption);
			// 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
			// 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求
			// 在定位结束后,在合适的生命周期调用onDestroy()方法
			// 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
			mlocationClient.startLocation();// 开始定位
		}
	}

	/**
	 * 停止定位
	 */
	public void deactivate() {
		mListener = null;
		if (mlocationClient != null) {
			mlocationClient.stopLocation();
			mlocationClient.onDestroy();
		}
		mlocationClient = null;
	}

	public class MyListener implements AMapLocationListener {

		@Override
		public void onLocationChanged(AMapLocation amapLocation) {
			if (amapLocation != null) {
				if (amapLocation.getErrorCode() == 0) {
					amapLocation.getLocationType();// 获取当前定位结果来源,如网络定位结果,详见定位类型
					amapLocation.getAccuracy();
					SimpleDateFormat df = new SimpleDateFormat(
							"yyyy-MM-ddHH:mm:ss");
					Date date = new Date(amapLocation.getTime());
					df.format(date);// 定为时间
					cityaddress=amapLocation.getAddress();
					editCity.setText(cityaddress);
					amapLocation.getProvince();
					amapLocation.getCity();
					amapLocation.getDistrict();
					amapLocation.getStreet();

				}

			}

		}

	}

	@Override
	public void onCheckedChanged(RadioGroup arg0, int checkedId) {
		switch (checkedId) {
		case R.id.gps_locate_button:
			// 设置定位的类型为定位模式
			aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
			break;
		case R.id.gps_follow_button:
			// 设置定位的类型为 跟随模式
			aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_FOLLOW);
			break;
		case R.id.gps_rotate_button:
			// 设置定位的类型为根据地图面向方向旋转
			aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_ROTATE);
			break;
		}

	}


	@Override
	public void onPoiItemSearched(PoiItem arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPoiSearched(PoiResult result, int rcode) {
		dissmissProgressDialog();// 隐藏对话框
		if (rcode == 1000) {
			if (result != null && result.getQuery() != null) {// 搜索poi的结果
				if (result.getQuery().equals(query)) {// 是否是同一条
					poiResult = result;
					poiItems = poiResult.getPois();// 取得第一页的poiitem数据,页数从数字0开始
					List<SuggestionCity> suggestionCities = poiResult
							.getSearchSuggestionCitys();// 当搜索不到poiitem数据时,会返回含有搜索关键字的城市信息
					if (poiItems != null && poiItems.size() > 0) {
						//清除POI信息显示
						whetherToShowDetailInfo(false);
						//并还原点击marker样式
						if (mlastMarker != null) {
							resetlastmarker();
						}				
						//清理之前搜索结果的marker
						if (poiOverlay !=null) {
							poiOverlay.removeFromMap();
						}
						aMap.clear();
						poiOverlay = new myPoiOverlay(aMap, poiItems);
						poiOverlay.addToMap();
						poiOverlay.zoomToSpan();
						
						aMap.addMarker(new MarkerOptions()
						.anchor(0.5f, 0.5f)
						.icon(BitmapDescriptorFactory
								.fromBitmap(BitmapFactory.decodeResource(
										getResources(), R.drawable.point4)))
						.position(new LatLng(lp.getLatitude(), lp.getLongitude())));
						
						aMap.addCircle(new CircleOptions()
						.center(new LatLng(lp.getLatitude(),
								lp.getLongitude())).radius(5000)
						.strokeColor(Color.BLUE)
						.fillColor(Color.argb(50, 1, 1, 1))
						.strokeWidth(2));

					} else if (suggestionCities != null
							&& suggestionCities.size() > 0) {
						showSuggestCity(suggestionCities);
					} else {
						ToastUtil.show(MainActivity.this,
								"对不起,没有搜索到相关数据");
					}
				}
			} else {
				ToastUtil
						.show(MainActivity.this, "对不起,没有搜索到相关数据");
			}
		}
		
	}
	
	
	
	private void showSuggestCity(List<SuggestionCity> cities) {
		String infomation = "推荐城市\n";
		for (int i = 0; i < cities.size(); i  ) {
			infomation  = "城市名称:"   cities.get(i).getCityName()   "城市区号:"
					  cities.get(i).getCityCode()   "城市编码:"
					  cities.get(i).getAdCode()   "\n";
		}
		ToastUtil.show(this, infomation);

		
	}
	
	

	// 将之前被点击的marker置为原来的状态
		private void resetlastmarker() {
			int index = poiOverlay.getPoiIndex(mlastMarker);
			if (index < 10) {
				mlastMarker.setIcon(BitmapDescriptorFactory
						.fromBitmap(BitmapFactory.decodeResource(
								getResources(),
								markers[index])));
			}else {
				mlastMarker.setIcon(BitmapDescriptorFactory.fromBitmap(
				BitmapFactory.decodeResource(getResources(), R.drawable.marker_other_highlight)));
			}
			mlastMarker = null;
			
		}

	    


	@Override
	public boolean onMarkerClick(Marker marker) {
		if (marker.getObject() != null) {
			whetherToShowDetailInfo(true);
			marker.showInfoWindow();
			try {
				PoiItem mCurrentPoi = (PoiItem) marker.getObject();
				if (mlastMarker == null) {
					mlastMarker = marker;
				} else {
					// 将之前被点击的marker置为原来的状态
					resetlastmarker();
					mlastMarker = marker;
					
				}
				detailMarker = marker;
				detailMarker.setIcon(BitmapDescriptorFactory
									.fromBitmap(BitmapFactory.decodeResource(
											getResources(),
											R.drawable.poi_marker_pressed)));

				setPoiItemDisplayContent(mCurrentPoi);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}else {
			whetherToShowDetailInfo(false);
			resetlastmarker();
		}


		return true;
	}

	private void setPoiItemDisplayContent(PoiItem mCurrentPoi) {
		mPoiName.setText(mCurrentPoi.getTitle());
		mPoiAddress.setText(mCurrentPoi.getSnippet());
		
	}

	@Override
	public View getInfoContents(Marker arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public View getInfoWindow(final Marker marker) {
		View view = getLayoutInflater().inflate(R.layout.poikeywordsearch_uri,
				null);
		TextView title = (TextView) view.findViewById(R.id.title);
		title.setText(marker.getTitle());

		TextView snippet = (TextView) view.findViewById(R.id.snippet);
		snippet.setText(marker.getSnippet());
		ImageButton button = (ImageButton) view
				.findViewById(R.id.start_amap_app);
		// 调起高德地图app
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				startAMapNavi(marker);
			}
		});
		return view;
	}
	/**
	 * 调起高德地图导航功能,如果没安装高德地图,会进入异常,可以在异常中处理,调起高德地图app的下载页面
	 */
	public void startAMapNavi(Marker marker) {
		// 构造导航参数
		NaviPara naviPara = new NaviPara();
		// 设置终点位置
		naviPara.setTargetPoint(marker.getPosition());
		// 设置导航策略,这里是避免拥堵
		naviPara.setNaviStyle(NaviPara.DRIVING_AVOID_CONGESTION);

		// 调起高德地图导航
		try {
			AMapUtils.openAMapNavi(naviPara, getApplicationContext());
		} catch (AMapException e) {

			// 如果没安装会进入异常,调起下载页面
			AMapUtils.getLatestAMapApp(getApplicationContext());

		}
	
}
	
	/**
	 * 判断高德地图app是否已经安装
	 */
	public boolean getAppIn() {
		PackageInfo packageInfo = null;
		try {
			packageInfo = this.getPackageManager().getPackageInfo(
					"com.autonavi.minimap", 0);
		} catch (NameNotFoundException e) {
			packageInfo = null;
			e.printStackTrace();
		}
		// 本手机没有安装高德地图app
		if (packageInfo != null) {
			return true;
		}
		// 本手机成功安装有高德地图app
		else {
			return false;
		}
	}
	
	/**
	 * 获取当前app的应用名字
	 */
	public String getApplicationName() {
		PackageManager packageManager = null;
		ApplicationInfo applicationInfo = null;
		try {
			packageManager = getApplicationContext().getPackageManager();
			applicationInfo = packageManager.getApplicationInfo(
					getPackageName(), 0);
		} catch (PackageManager.NameNotFoundException e) {
			applicationInfo = null;
		}
		String applicationName = (String) packageManager
				.getApplicationLabel(applicationInfo);
		return applicationName;
	}

	@Override
	public void onInfoWindowClick(Marker arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onMapClick(LatLng arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_search:
			doSearchQuery();
			break;
		case R.id.searchButton:
			searchButton();
			break;
		/**
		 * 点击下一页按钮
		 */
		case R.id.nextButton:
			nextButton();
			break;

		default:
			break;
		}
		
	}

		/**
		 * 点击下一页按钮
		 */
		public void nextButton() {
			if (query != null && poiSearch != null && poiResult != null) {
				if (poiResult.getPageCount() - 1 > currentPage) {
					currentPage  ;
					query.setPageNum(currentPage);// 设置查后一页
					poiSearch.searchPOIAsyn();
				} else {
					ToastUtil.show(MainActivity.this,
							"对不起,没有搜索到相关数据");
				}
		}
		
	}

	private int[] markers = {R.drawable.poi_marker_1,
			R.drawable.poi_marker_2,
			R.drawable.poi_marker_3,
			R.drawable.poi_marker_4,
			R.drawable.poi_marker_5,
			R.drawable.poi_marker_6,
			R.drawable.poi_marker_7,
			R.drawable.poi_marker_8,
			R.drawable.poi_marker_9,
			R.drawable.poi_marker_10
			};
	private void whetherToShowDetailInfo(boolean isToShow) {
		if (isToShow) {
			mPoiDetail.setVisibility(View.VISIBLE);

		} else {
			mPoiDetail.setVisibility(View.GONE);

		}
	}
	/**
	 * 自定义PoiOverlay
	 * 
	 */

	private class myPoiOverlay {
		private AMap mamap;
		private List<PoiItem> mPois;
		private ArrayList<Marker> mPoiMarks = new ArrayList<Marker>();

		public myPoiOverlay(AMap amap, List<PoiItem> pois) {
			mamap = amap;
			mPois = pois;
		}

		public void zoomToSpan() {
			if (mPois != null && mPois.size() > 0) {
	            if (mamap == null)
	                return;
	            LatLngBounds bounds = getLatLngBounds();
	            mamap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
	        }
			
		}

		private LatLngBounds getLatLngBounds() {
			LatLngBounds.Builder b = LatLngBounds.builder();
	        for (int i = 0; i < mPois.size(); i  ) {
	            b.include(new LatLng(mPois.get(i).getLatLonPoint().getLatitude(),
	                    mPois.get(i).getLatLonPoint().getLongitude()));
	        }
	        return b.build();
		}

		public void removeFromMap() {
			for (Marker mark : mPoiMarks) {
	            mark.remove();
	        }
			
		}

		public int getPoiIndex(Marker marker) {
			 for (int i = 0; i < mPoiMarks.size(); i  ) {
		            if (mPoiMarks.get(i).equals(marker)) {
		                return i;
		            }
		        }
		        return -1;
		}

		public void addToMap() {
		      for (int i = 0; i < mPois.size(); i  ) {
		            Marker marker = mamap.addMarker(getMarkerOptions(i));
		            PoiItem item = mPois.get(i);
					marker.setObject(item);
		            mPoiMarks.add(marker);
		        }
			
		}

		private MarkerOptions getMarkerOptions(int index) {
			 return new MarkerOptions()
             .position(
                     new LatLng(mPois.get(index).getLatLonPoint()
                             .getLatitude(), mPois.get(index)
                             .getLatLonPoint().getLongitude()))
             .title(getTitle(index)).snippet(getSnippet(index))
             .icon(getBitmapDescriptor(index));
		}
		 private BitmapDescriptor getBitmapDescriptor(int arg0) {
			 if (arg0 < 10) {
					BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(
							BitmapFactory.decodeResource(getResources(), markers[arg0]));
					return icon;
				}else {
					BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(
							BitmapFactory.decodeResource(getResources(), R.drawable.marker_other_highlight));
					return icon;
				}	
		}

		protected String getTitle(int index) {
		        return mPois.get(index).getTitle();
		    }

		    protected String getSnippet(int index) {
		        return mPois.get(index).getSnippet();
		    }
	}

	@Override
	public void afterTextChanged(Editable arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		String newText = s.toString().trim();
		if (!AMapUtil.IsEmptyOrNullString(newText)) {
		    InputtipsQuery inputquery = new InputtipsQuery(newText, cityaddress);
		    Inputtips inputTips = new Inputtips(MainActivity.this, inputquery);
		    inputTips.setInputtipsListener(this);
		    inputTips.requestInputtipsAsyn();
		}
		
	}
	

	@Override
	public void onGetInputtips(List<Tip> tipList, int rCode) {
		if (rCode == 1000) {// 正确返回
			List<String> listString = new ArrayList<String>();
			for (int i = 0; i < tipList.size(); i  ) {
				listString.add(tipList.get(i).getName());
			}
			ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(
					getApplicationContext(),
					R.layout.route_inputs, listString);
			searchText.setAdapter(aAdapter);
			aAdapter.notifyDataSetChanged();
		} else {
			ToastUtil.showerror(this, rCode);
		}
		
	}

}

标签: 地图 定位 高德

实例下载地址

android 高德地图定位、周边、关键字搜索等

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警