package company.com.gaodedemo; import android.app.Activity; import android.app.ProgressDialog; import android.content.DialogInterface; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.GridView; import android.widget.LinearLayout; import android.widget.RadioGroup; import android.widget.Spinner; import android.widget.Switch; import android.widget.TextView; 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.AMapLocationListener; import com.amap.api.maps2d.AMap; import com.amap.api.maps2d.AMapOptions; import com.amap.api.maps2d.LocationSource; import com.amap.api.maps2d.MapView; import com.amap.api.maps2d.UiSettings; import com.amap.api.maps2d.model.BitmapDescriptorFactory; import com.amap.api.maps2d.model.LatLng; import com.amap.api.maps2d.model.Marker; import com.amap.api.maps2d.model.MarkerOptions; import com.amap.api.maps2d.model.MyLocationStyle; import com.amap.api.maps2d.overlay.PoiOverlay; 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.poisearch.PoiResult; import com.amap.api.services.poisearch.PoiSearch; import java.util.List; public class MainActivity extends Activity implements LocationSource, AMapLocationListener,AMap.OnMarkerClickListener, AMap.InfoWindowAdapter ,AdapterView.OnItemSelectedListener,PoiSearch.OnPoiSearchListener ,AMap.OnMapClickListener,AMap.OnInfoWindowClickListener ,View.OnClickListener { //基本地图 private AMap aMap; private MapView mapView; //定位服务 private OnLocationChangedListener mListener; private AMapLocationClient mlocationClient; private AMapLocationClientOption mLocationOption; private double current_Locationlatitude, current_Locationlongitude; private String current_city,current_Adress; //POI搜索 private ProgressDialog progDialog = null;// 搜索时进度条 private Spinner selectDeep;// 选择城市列表 private String[] itemDeep = {"酒店", "餐饮", "景区", "影院","中学","酒吧","KTV"}; private Spinner selectType;// 选择返回是否有团购,优惠 private String[] itemTypes = {"所有poi", "有团购", "有优惠", "有团购或者优惠"}; private String deepType = "";// poi搜索类型 private int searchType = 0;// 搜索类型 private int tsearchType = 0;// 当前选择搜索类型 private PoiResult poiResult; // poi返回的结果 private int currentPage = 0;// 当前页面,从0开始计数 private PoiSearch.Query query;// Poi查询条件类 private Marker locationMarker; // 选择的点 private PoiSearch poiSearch; private PoiOverlay poiOverlay;// poi图层 private List<PoiItem> poiItems;// poi数据 private Marker detailMarker;// 显示Marker的详情 private Button nextButton;// 下一页 private GridView gridView; private TextView tv_guid; private Button locationButton; private String Name; private LatLonPoint lp_current=new LatLonPoint(39.908127, 116.375257);// 默认西单广场; //选择地图显示模式 private RadioGroup radioGroup; private LinearLayout ll_btn; private UiSettings uiSettings;//设置地图自带按钮的位置 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.content_main); mapView = (MapView) findViewById(R.id.map_2d); mapView.onCreate(savedInstanceState);// 此方法必须重写 init(); SetData(); AddListener(); } private void AddListener() { gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Name = parent.getAdapter().getItem(position).toString(); doSearchQuery(Name, deepType, current_city);//POI搜索的方法 } }); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch(checkedId){ case R.id.map_type_normal_rb: aMap.setMapType(AMap.MAP_TYPE_NORMAL); break; case R.id.map_type_satellite_rb: aMap.setMapType(AMap.MAP_TYPE_SATELLITE); break; case R.id.map_type_night_rb: break; }; } }); } private void SetData() { ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.support_simple_spinner_dropdown_item, itemDeep); gridView.setAdapter(adapter); } /** * 初始化AMap对象 */ private void init() { if (aMap == null) { aMap = mapView.getMap(); uiSettings=aMap.getUiSettings(); setUpMap(); } locationButton = (Button) findViewById(R.id.Button); // locationButton.getBackground().setAlpha(100); locationButton.setOnClickListener(this); gridView = (GridView) findViewById(R.id.gridView_poi); tv_guid = (TextView) findViewById(R.id.Grid_layout); aMap.setOnMarkerClickListener(this);// 添加点击marker监听事件 aMap.setInfoWindowAdapter(this);// 添加显示infowindow监听事件 //选择地图模式 radioGroup= (RadioGroup) findViewById(R.id.radiogroup); ll_btn= (LinearLayout) findViewById(R.id.ll_btn); ll_btn.getBackground().setAlpha(150); } /** * 设置一些amap的属性 */ private void setUpMap() { // 自定义系统定位小蓝点 MyLocationStyle myLocationStyle = new MyLocationStyle(); myLocationStyle.myLocationIcon(BitmapDescriptorFactory .fromResource(R.drawable.location_marker));// 设置小蓝点的图标 myLocationStyle.strokeColor(Color.BLACK);// 设置圆形的边框颜色 myLocationStyle.radiusFillColor(Color.argb(100, 0, 0, 180));// 设置圆形的填充颜色 // myLocationStyle.anchor(int,int)//设置小蓝点的锚点 myLocationStyle.strokeWidth(1.0f);// 设置圆形的边框粗细 aMap.setMyLocationStyle(myLocationStyle); aMap.setLocationSource(this);// 设置定位监听 aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示 aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false uiSettings.setZoomPosition(AMapOptions.ZOOM_POSITION_RIGHT_CENTER);//设置缩放按钮去位置 uiSettings.setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_RIGHT);//设置高德地图Logo的位置 } /** * 方法必须重写 */ @Override protected void onResume() { super.onResume(); mapView.onResume(); } /** * 方法必须重写 */ @Override protected void onPause() { super.onPause(); mapView.onPause(); deactivate(); } /** * 方法必须重写 */ @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } /** * 方法必须重写 */ @Override protected void onDestroy() { super.onDestroy(); mapView.onDestroy(); } /** * 定位成功后回调函数 */ @Override public void onLocationChanged(AMapLocation amapLocation) { if (mListener != null && amapLocation != null) { if (amapLocation != null && amapLocation.getErrorCode() == 0) { mListener.onLocationChanged(amapLocation);// 显示系统小蓝点 } else { String errText = "定位失败," + amapLocation.getErrorCode() + ": " + amapLocation.getErrorInfo(); Log.e("AmapErr", errText); } current_Locationlatitude = amapLocation.getLatitude(); current_Locationlongitude = amapLocation.getLongitude(); current_city = amapLocation.getCity(); current_Adress=amapLocation.getAddress(); lp_current=new LatLonPoint(current_Locationlatitude,current_Locationlongitude); locationMarker = aMap.addMarker(new MarkerOptions() .anchor(0.5f, 1) .icon(BitmapDescriptorFactory .fromResource(R.drawable.point)) .position(new LatLng(lp_current.getLatitude(), lp_current.getLongitude())) .title(current_Adress)); Log.i("中心地址",current_Adress); } } /** * 激活定位 */ @Override public void activate(OnLocationChangedListener listener) { mListener = listener; if (mlocationClient == null) { mlocationClient = new AMapLocationClient(this); mLocationOption = new AMapLocationClientOption(); //设置定位监听 mlocationClient.setLocationListener(this); //设置为高精度定位模式 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //设置定位参数 mlocationClient.setLocationOption(mLocationOption); // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗, // 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求 // 在定位结束后,在合适的生命周期调用onDestroy()方法 // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除 mlocationClient.startLocation(); } } /** * 停止定位 */ @Override public void deactivate() { mListener = null; if (mlocationClient != null) { mlocationClient.stopLocation(); mlocationClient.onDestroy(); } mlocationClient = null; } //OnMarkerClickListener未实现的方法 @Override public boolean onMarkerClick(Marker marker) { return false; } //InfoWindowAdapter未实现的方法 @Override public View getInfoWindow(Marker marker) { return null; } @Override public View getInfoContents(Marker marker) { return null; } //OnItemSelectedListener未实现的方法 @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { } @Override public void onNothingSelected(AdapterView<?> parent) { } //OnPoiSearchListener未实现的方法(获得POI搜索结果) @Override public void onPoiSearched(PoiResult result, int rCode) { if (rCode == 0) { 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) { aMap.clear();// 清理之前的图标 poiOverlay = new PoiOverlay(aMap, poiItems); poiOverlay.removeFromMap(); poiOverlay.addToMap(); poiOverlay.zoomToSpan(); } else if (suggestionCities != null && suggestionCities.size() > 0) { showSuggestCity(suggestionCities); } else { Toast.makeText(MainActivity.this,"没有可显示的结果",Toast.LENGTH_SHORT).show(); } } } else { Toast.makeText(MainActivity.this,"没有可显示的结果",Toast.LENGTH_SHORT).show(); } } else if (rCode == 27) { Toast.makeText(MainActivity.this,"网络错误",Toast.LENGTH_SHORT).show(); } else if (rCode == 32) { Toast.makeText(MainActivity.this,"错误的Key值",Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this,"其他的错误",Toast.LENGTH_SHORT).show(); } } //OnMapClickListener未实现的方法 @Override public void onMapClick(LatLng latLng) { locationMarker=aMap.addMarker(new MarkerOptions().anchor(0.5f, 1) .icon(BitmapDescriptorFactory.fromResource(R.drawable.end)) .position(latLng).title("点击选取此点为中心")); locationMarker.showInfoWindow(); } //OnInfoWindowClickListener未实现的方法 @Override public void onInfoWindowClick(Marker marker) { locationMarker.hideInfoWindow(); lp_current=new LatLonPoint(locationMarker.getPosition().latitude, locationMarker.getPosition().longitude); Log.i("选点Lp",lp_current+""); locationMarker.destroy(); } //OnClickListener未实现的方法 @Override public void onClick(View v) { switch (v.getId()){ case R.id.Button: aMap.clear(); registerListener(); break; /*case R.id.nextButton: //点击获取下一页数据 nextSearch(); break;*/ } } 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"; } Toast.makeText(MainActivity.this,infomation,Toast.LENGTH_SHORT).show(); } protected void doSearchQuery(String name, String type, String city) { // showProgressDialog();// 显示进度框 aMap.setOnMapClickListener(null);// 进行poi搜索时清除掉地图点击事件 currentPage = 0; query = new PoiSearch.Query(name, type, city);// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国) query.setPageSize(20);// 设置每页最多返回多少条poiitem query.setPageNum(currentPage);// 设置查第一页 searchType = tsearchType; //选择是否团购 /** switch (searchType) { case 0: {// 所有poi query.setLimitDiscount(false); query.setLimitGroupbuy(false); } break; case 1: {// 有团购 query.setLimitGroupbuy(true); query.setLimitDiscount(false); } break; case 2: {// 有优惠 query.setLimitGroupbuy(false); query.setLimitDiscount(true); } break; case 3: {// 有团购或者优惠 query.setLimitGroupbuy(true); query.setLimitDiscount(true); } break; }*/ if (lp_current != null) { poiSearch = new PoiSearch(this, query); poiSearch.setOnPoiSearchListener(this); poiSearch.setBound(new PoiSearch.SearchBound(lp_current, 2000, true));// // 设置搜索区域为以lp点为圆心,其周围2000米范围 /* * List<LatLonPoint> list = new ArrayList<LatLonPoint>(); * list.add(lp); * list.add(AMapUtil.convertToLatLonPoint(Constants.BEIJING)); * poiSearch.setBound(new SearchBound(list));// 设置多边形poi搜索范围 */ poiSearch.searchPOIAsyn();// 异步搜索 } } /** * 注册监听 */ private void registerListener() { aMap.setOnMapClickListener(MainActivity.this); aMap.setOnMarkerClickListener(MainActivity.this); aMap.setOnInfoWindowClickListener(this); aMap.setInfoWindowAdapter(MainActivity.this); } /** * 点击下一页poi搜索(暂时未设置点击进入下一页的Button,如若需要请自行添加) */ public void nextSearch() { if (query != null && poiSearch != null && poiResult != null) { if (poiResult.getPageCount() - 1 > currentPage) { currentPage++; query.setPageNum(currentPage);// 设置查后一页 poiSearch.searchPOIAsyn(); } else { Toast.makeText(MainActivity.this,"没有可显示的结果",Toast.LENGTH_SHORT).show(); } } } }
原文:http://www.cnblogs.com/zhujiabin/p/5456949.html