public MyLocationListenner myListener = new MyLocationListenner(); private BaiduMap mBaiduMap; ***其他自己初始化 // 获取地图控件引用 mMapView = (MapView) findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); // 设置是否显示比例尺控件 mMapView.showScaleControl(false); // 设置是否显示缩放控件 mMapView.showZoomControls(true); // 删除百度地图LoGo mMapView.removeViewAt(1);
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
if(city_tv.equals("")){
// 定位初始化
mLocClient = new LocationClient(getApplicationContext());
mLocClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); // 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);
mLocClient.setLocOption(option);
mLocClient.start();
}
// 设置marker图标 bitmap = BitmapDescriptorFactory .fromResource(R.drawable.dtdw); mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
[@Override](https://my.oschina.net/u/1162528)
public boolean onMapPoiClick(MapPoi arg0) {
return false;
}
// 此方法就是点击地图监听
[@Override](https://my.oschina.net/u/1162528)
public void onMapClick(LatLng latLng) {
// 获取经纬度
mCurrentLat = latLng.latitude;
mCurrentLon = latLng.longitude;
Log.e("LiNing","具体地址======"+mCurrentLat+"----"+mCurrentLon);
// 先清除图层
mBaiduMap.clear();
// 定义Maker坐标点
LatLng point = new LatLng(mCurrentLat, mCurrentLon);
// 构建MarkerOption,用于在地图上添加Marker
MarkerOptions options = new MarkerOptions().position(point)
.icon(bitmap);
// 在地图上添加Marker,并显示
mBaiduMap.addOverlay(options);
// 实例化一个地理编码查询对象
GeoCoder geoCoder = GeoCoder.newInstance();
// 设置反地理编码位置坐标
ReverseGeoCodeOption op = new ReverseGeoCodeOption();
op.location(latLng);
// 发起反地理编码请求(经纬度->地址信息)
geoCoder.reverseGeoCode(op);
geoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
[@Override](https://my.oschina.net/u/1162528)
public void onGetReverseGeoCodeResult(
ReverseGeoCodeResult arg0) {
// 获取点击的坐标地址
address = arg0.getAddress();
// city_tv.setText(arg0.getAddressDetail().city); address_tv.setText(address); Log.e("LiNing","具体地址======"+address);
}
[@Override](https://my.oschina.net/u/1162528)
public void onGetGeoCodeResult(GeoCodeResult arg0) {
}
});
geoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(latLng));
}
});
mPoiSearch = PoiSearch.newInstance(); //创建POI检索监听者; mPoiSearch.setOnGetPoiSearchResultListener(poiListener); mPoiSearch.searchInCity((new PoiCitySearchOption()) .city(city_tv.getText().toString().trim()) .keyword(address_tv.getText().toString().trim()) .pageNum(0)); @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
mPoiSearch.searchInCity((new PoiCitySearchOption())
.city(city_tv.getText().toString().trim())
.keyword(address_tv.getText().toString().trim())
.pageNum(0));
}
public class MyLocationListenner extends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || mMapView == null) {
return;
}
// 获取经纬度
mCurrentLat = location.getLatitude();
mCurrentLon = location.getLongitude();
// 先清除图层
mBaiduMap.clear();
// 定义Maker坐标点
LatLng point = new LatLng(mCurrentLat, mCurrentLon);
Log.e("LiNing","具体地址======"+point);
// 构建MarkerOption,用于在地图上添加Marker
MarkerOptions options = new MarkerOptions().position(point).icon(
bitmap);
// 在地图上添加Marker,并显示
mBaiduMap.addOverlay(options);
ll = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatus.Builder builder = new MapStatus.Builder();
builder.target(ll).zoom(18.0f);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory
.newMapStatus(builder.build()));
// 实例化一个地理编码查询对象
GeoCoder geoCoder = GeoCoder.newInstance();
// 设置反地理编码位置坐标
ReverseGeoCodeOption op = new ReverseGeoCodeOption();
op.location(point);
// 发起反地理编码请求(经纬度->地址信息)
geoCoder.reverseGeoCode(op);
geoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult arg0) {
// 获取点击的坐标地址
address = arg0.getAddress();
city_tv.setText(arg0.getAddressDetail().city);
address_tv.setText(address);
Log.e("LiNing","具体地址======"+address);
// 定位成功后就停止
mBaiduMap.setMyLocationEnabled(false);
if (mLocClient.isStarted()) {
mLocClient.stop();
};
}
@Override
public void onGetGeoCodeResult(GeoCodeResult arg0) {
}
});
geoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(ll));
}
}
@Override protected void onDestroy() { super.onDestroy(); // mLocClient.stop(); //在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(); } //用于条件搜索 private PoiSearch mPoiSearch; List<PoiInfo> allPoi; LatLng location_ll,ll; private ListView searchPois; OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {
public void onGetPoiResult(PoiResult result) {
// 关闭定位图层
// mBaiduMap.clear(); // mLocClient.stop(); if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) { //详情检索失败 // result.error请参考SearchResult.ERRORNO Toast.makeText(MapInfosActivity.this, "未搜索到POI数据", Toast.LENGTH_SHORT).show(); } if (result.error == SearchResult.ERRORNO.NO_ERROR) { //获取POI检索结果 Toast.makeText(MapInfosActivity.this, "已搜索到相关数据数据", Toast.LENGTH_SHORT).show(); // mBaiduMap.clear();
allPoi = result.getAllPoi();
for (int i = 0; i < allPoi.size(); i++) {
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.csdw);
Bitmap bitmap1 = BitmapFactory.decodeResource(res, R.drawable.cxdw);
// Bitmap bitmap1 = ImageUtils.drawTextToCenter(MapInfosActivity.this, bitmap, "" + i, 20, Color.BLACK);
OverlayOptions options = new MarkerOptions()
.position(result.getAllPoi().get(i).location)
.title(result.getAllPoi().get(i).name + ":" + result.getAllPoi().get(i).address)
.icon(BitmapDescriptorFactory.fromBitmap(bitmap1));
mBaiduMap.addOverlay(options);
//实现位置跟踪
location_ll = result.getAllPoi().get(i).location;
// MapStatusUpdate msu = MapStatusUpdateFactory.newLatLngZoom(location_ll, 18); // mBaiduMap.animateMapStatus(msu); } if (allPoi != null) { PoiSearchAdapter poiSearchAdapter = new PoiSearchAdapter(MapInfosActivity.this, allPoi,ll); searchPois.setVisibility(View.VISIBLE); searchPois.setAdapter(poiSearchAdapter); poiSearchAdapter.notifyDataSetChanged(); }
mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Log.e("LiNing","具体地址======"+marker);
Intent localIntent = getIntent();
// localIntent.putExtra("ANDRESS", marker.getTitle().toString()); localIntent.putExtra("ANDRESS",address_tv.getText().toString()); setResult(1, localIntent); finish(); Toast.makeText(MapInfosActivity.this, marker.getTitle(), Toast.LENGTH_SHORT).show(); return true; } }); } if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {
// 当输入关键字在本市没有找到,但在其他城市找到时,返回包含该关键字信息的城市列表
String strInfo = "在";
for (CityInfo cityInfo : result.getSuggestCityList()) {
strInfo += cityInfo.city;
strInfo += ",";
}
strInfo += "找到结果";
Toast.makeText(MapInfosActivity.this, strInfo, Toast.LENGTH_LONG)
.show();
}
}
public void onGetPoiDetailResult(PoiDetailResult result) {
//获取Place详情页检索结果
if (result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(MapInfosActivity.this, "抱歉,未找到结果", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(MapInfosActivity.this, result.getName() + ": " + result.getAddress(), Toast.LENGTH_SHORT)
.show();
}
}
@Override
public void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) {
}
@Override
public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {
}
};