admin 管理员组文章数量: 887006
百度地图用城市名称切换城市更新地图
实例代码:
@ContentView(R.layout.activity_main)
public class MainActivity extends Activity {@ViewInject(R.id.bt1)
Button bt1;
@ViewInject(R.id.bt2)
Button bt2;
@ViewInject(R.id.listView)
private ListView listView;
@ViewInject(R.id.mapview)
private MapView mMapView;
private BaiduMap mBaiduMap;
private LatLng cenpt;
private GeoCoder mSearch = null; // 搜索模块,也可去掉地图模块独立使用
private PoiSearch mPoiSearch;
private LocationClient mLocClient;
MyLocationListener loctLis = new MyLocationListener();
ListAdapter adapter;
private PoiInfo currentInfo;
private AddressComponent addressDetail;
private List<PoiInfo> list;
// 纬度
private Double LATITUDE = 0.0;
// 经度
private Double LONGITUDE = 0.0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initMap();
}
@OnClick(R.id.bt1)
private void onclickBt1() {
search(bt1.getText().toString().trim(), "街道");
}
@OnClick(R.id.bt2)
private void onclickBt2() {
search(bt2.getText().toString().trim(), "街道");
}
private void search(String city, String keyword) {
//第一步 可以根据城市名称切换城市地图,必须.keyword()关键词,这里为”街道“
mPoiSearch.searchInCity((new PoiCitySearchOption()).city(city).keyword(keyword).pageNum(0).pageCapacity(30));
//调用完成后,会在getPoisLis 中回调,
}private void initMap() {
mBaiduMap = mMapView.getMap();
LocationClientOption option = new LocationClientOption();
// 设置定位模式
option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
// option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms
option.setOpenGps(true);// 打开gps
option.setIsNeedAddress(true);
// 定位初始化
mLocClient = new LocationClient(this);
mLocClient.registerLocationListener(loctLis);
mLocClient.setLocOption(option);
mLocClient.start();
mLocClient.requestLocation();
cenpt = new LatLng(LATITUDE, LONGITUDE);
mBaiduMap.setOnMapLoadedCallback(mapCallBack);
mBaiduMap.setOnMapStatusChangeListener(mapStatueLis);
// 初始化搜索模块,注册事件监听
mSearch = GeoCoder.newInstance();
mSearch.setOnGetGeoCodeResultListener(getGeocLis);
// 定义地图状态
MapStatus mMapStatus = new MapStatus.Builder().target(cenpt).zoom(14)
.build();
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory
.newMapStatus(mMapStatus);
// 改变地图状态̬
mBaiduMap.setMapStatus(mMapStatusUpdate);
// 初始化搜索模块,注册搜索事件监听
mPoiSearch = PoiSearch.newInstance();
mPoiSearch.setOnGetPoiSearchResultListener(getPoisLis);
adapter = new ListAdapter(this);
listView.setAdapter(adapter);
}
private void updateMapData(List<PoiInfo> allPoi) {
adapter.setList(allPoi);
adapter.notifyDataSetChanged();
}
// 第三步
private void cutCity() {
cenpt = new LatLng(LATITUDE, LONGITUDE);mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(cenpt));
}
OnGetPoiSearchResultListener getPoisLis = new OnGetPoiSearchResultListener() {
@Override
public void onGetPoiResult(PoiResult arg0) {
// TODO Auto-generated method stub
if (arg0 == null
|| arg0.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
Toast.makeText(MainActivity.this, "搜索不到当前地址", Toast.LENGTH_LONG)
.show();
return;
}
List<PoiInfo> allPoi = arg0.getAllPoi();
if (allPoi == null || allPoi.size() < 1) {
Toast.makeText(MainActivity.this, "搜索不到当前地址", Toast.LENGTH_LONG)
.show();
return;
}
list = allPoi;
currentInfo = allPoi.get(0);
// 第二步 获取经纬度后再经过经纬度更新地图
LATITUDE = currentInfo.location.latitude;LONGITUDE = currentInfo.location.longitude;
cutCity();
updateMapData(allPoi);
}
@Override
public void onGetPoiDetailResult(PoiDetailResult arg0) {
// TODO Auto-generated method stub
}
};
OnGetGeoCoderResultListener getGeocLis = new OnGetGeoCoderResultListener() {
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult arg0) {
// TODO Auto-generated method stub
mBaiduMap.clear();
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(arg0
.getLocation()));
addressDetail = arg0.getAddressDetail();
list = arg0.getPoiList();
currentInfo = null;
if (currentInfo != null) {
if (list != null) {
for (PoiInfo poiInfo : list) {
if (poiInfo.location.latitude == currentInfo.location.latitude
&& poiInfo.location.longitude == currentInfo.location.longitude) {
list.remove(poiInfo);
break;
}
}
list.add(0, currentInfo);
}
}
if (list != null && list.size() > 0) {
currentInfo = list.get(0);
}
updateMapData(list);
}
@Override
public void onGetGeoCodeResult(GeoCodeResult arg0) {
// TODO Auto-generated method stub
}
};
OnMapStatusChangeListener mapStatueLis = new OnMapStatusChangeListener() {
@Override
public void onMapStatusChangeStart(MapStatus arg0) {
// TODO Auto-generated method stub
}
@Override
public void onMapStatusChangeFinish(MapStatus arg0) {
// TODO Auto-generated method stub
LatLng latlng = arg0.target;
LATITUDE = latlng.latitude;
LONGITUDE = latlng.longitude;
cutCity();
}
@Override
public void onMapStatusChange(MapStatus arg0) {
// TODO Auto-generated method stub
}
};
OnMapLoadedCallback mapCallBack = new OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "完成加载", Toast.LENGTH_SHORT)
.show();
}
};
/**
* 实现实位回调监听
*/
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// Receive Location
LATITUDE = location.getLatitude();
LONGITUDE = location.getLongitude();
mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(cenpt));
}
public void onReceivePoi(BDLocation arg0) {
// TODO Auto-generated method stub
}
}
}
本文标签: 百度地图用城市名称切换城市更新地图
版权声明:本文标题:百度地图用城市名称切换城市更新地图 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1732353500h1533771.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论