Android百度地图定位收索取周边在列表中展示并选择

具体内容如下效果图:

Android百度地图定位收索取周边在列表中展示并选择_第1张图片

1、布局文件,就是一个MapView和ListView,布局文件就是上面是一个百度地图的mapview,下面是一个显示周边位置的ListView



    

        

        

        
2、初始化控件

		dataList = new ArrayList();
		mMapView = (MapView) findViewById(R.id.bmapView);
		mCompleteButton = (Button) findViewById(R.id.chat_publish_complete_publish);
		mRequestLocation = (Button) findViewById(R.id.request);
		mListView = (ListView) findViewById(R.id.lv_location_nearby);
		checkPosition=0;
		adapter = new ListAdapter(0);
		mListView.setAdapter(adapter);

3、 定位

		//重新设置
		checkPosition = 0;
		adapter.setCheckposition(0);
		
		mBaiduMap = mMapView.getMap();
		mBaiduMap.clear();
		// 开启定位图层
		mBaiduMap.setMyLocationEnabled(true);
		mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(new MapStatus.Builder().zoom(17).build()));   // 设置级别
		
		// 定位初始化
		mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类
		mLocationClient.registerLocationListener(myListener);// 注册定位监听接口
		
		/**
		 * 设置定位参数
		 */
		LocationClientOption option = new LocationClientOption();
		option.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式
//		option.setScanSpan(5000);// 设置发起定位请求的间隔时间,ms
		option.setNeedDeviceDirect(true);// 设置返回结果包含手机的方向
		option.setOpenGps(true);
		option.setAddrType("all");// 返回的定位结果包含地址信息
		option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
		option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
		mLocationClient.setLocOption(option);
		mLocationClient.start(); // 调用此方法开始定位
4、定位SDK监听函数

public class MyLocationListener implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null || mMapView == null) {
				return;
			}
			
			locType = location.getLocType();
			Log.i("mybaidumap", "当前定位的返回值是:"+locType);
			
			longitude = location.getLongitude();
			latitude = location.getLatitude();
			if (location.hasRadius()) {// 判断是否有定位精度半径
				radius = location.getRadius();
			}
			
			if (locType == BDLocation.TypeNetWorkLocation) {
				addrStr = location.getAddrStr();// 获取反地理编码(文字描述的地址)
				Log.i("mybaidumap", "当前定位的地址是:"+addrStr);
			}
			
			direction = location.getDirection();// 获取手机方向,【0~360°】,手机上面正面朝北为0°
			province = location.getProvince();// 省份
			city = location.getCity();// 城市
			district = location.getDistrict();// 区县
			
			LatLng ll = new LatLng(location.getLatitude(),location.getLongitude());
			
			//将当前位置加入List里面
			PoiInfo info = new PoiInfo();
			info.address = location.getAddrStr();
			info.city = location.getCity();
			info.location = ll;
			info.name = location.getAddrStr();
			dataList.add(info);
			adapter.notifyDataSetChanged();
			Log.i("mybaidumap", "province是:"+province +" city是"+city +" 区县是: "+district);

			
			// 构造定位数据
			MyLocationData locData = new MyLocationData.Builder()
					.accuracy(location.getRadius())
					// 此处设置开发者获取到的方向信息,顺时针0-360
					.direction(100).latitude(location.getLatitude())
					.longitude(location.getLongitude()).build();
			mBaiduMap.setMyLocationData(locData);
			
			//画标志
			CoordinateConverter converter = new CoordinateConverter();
		    converter.coord(ll);
		    converter.from(CoordinateConverter.CoordType.COMMON);
		    LatLng convertLatLng = converter.convert();
		    
		    OverlayOptions ooA = new MarkerOptions().position(ll).icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_marka));
		    mCurrentMarker = (Marker) mBaiduMap.addOverlay(ooA);
		     
		    
		     MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(convertLatLng, 17.0f);
		     mBaiduMap.animateMapStatus(u);
		     
		     //画当前定位标志
		     MapStatusUpdate uc = MapStatusUpdateFactory.newLatLng(ll);
			 mBaiduMap.animateMapStatus(uc);
			
			 mMapView.showZoomControls(false);
			 //poi 搜索周边
			 new Thread(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					Looper.prepare(); 
					searchNeayBy();
					Looper.loop();
				}
			}).start();
			 

		}

5、搜索周边:

private void searchNeayBy(){
		// POI初始化搜索模块,注册搜索事件监听
		mPoiSearch = PoiSearch.newInstance();
		mPoiSearch.setOnGetPoiSearchResultListener(this);
		PoiNearbySearchOption poiNearbySearchOption = new PoiNearbySearchOption();

		poiNearbySearchOption.keyword("公司");
		poiNearbySearchOption.location(new LatLng(latitude, longitude));
		poiNearbySearchOption.radius(100);  // 检索半径,单位是米
		poiNearbySearchOption.pageCapacity(20);  // 默认每页10条
		mPoiSearch.searchNearby(poiNearbySearchOption);  // 发起附近检索请求
	}
7、周边地理位置结果返回

@Override
	public void onGetPoiResult(PoiResult result) {
		// 获取POI检索结果
		if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {// 没有找到检索结果
			Toast.makeText(MainActivity.this, "未找到结果",Toast.LENGTH_LONG).show();
			return;
		}

		if (result.error == SearchResult.ERRORNO.NO_ERROR) {// 检索结果正常返回
//			mBaiduMap.clear();
			if(result != null){
				if(result.getAllPoi()!= null && result.getAllPoi().size()>0){
					dataList.addAll(result.getAllPoi());
//					adapter.notifyDataSetChanged();
					Message msg = new Message();
			        msg.what = 0;
			        handler.sendMessage(msg);
				}
			}
		}
	}

8、返回结果result.getAllPoi() 设到ListView的Adapter里面去,刷新控件即可。


9、源代码


你可能感兴趣的:(Android)