地圖選點(diǎn)
/**
* 地址-地圖選點(diǎn)
* 定位->經(jīng)緯度->轉(zhuǎn)地址信息PoiList
* 移動(dòng)地圖->中心點(diǎn)經(jīng)緯度->轉(zhuǎn)地址信息PoiList
* 關(guān)鍵字搜索->PoiList
*/
public class AddressMapActivity extends AppCompatActivity implements BaiduMap.OnMapStatusChangeListener, OnGetGeoCoderResultListener, OnGetPoiSearchResultListener {
@BindView(R.id.mAddressMapSearch)
EditText mSearchContent;
@BindView(R.id.mAddressMapView)
MapView mapView;
@BindView(R.id.mAddressMapRcy1)
RecyclerView rcy1;
@BindView(R.id.mAddressMapContentLayout1)
LinearLayout mContentLayout;
@BindView(R.id.mAddressMapRcy2)
RecyclerView rcy2;
private BaiduMap mBaiduMap;
private GeoCoder geoCoder;
private LocationClient mLocClient;
private MyLocationListener mLocationListener;
private PoiListAdapter mPoiListAdapter;
private List<PoiInfo> poiInfos = new ArrayList<>();
private List<PoiInfo> searchInfos = new ArrayList<>();
private PoiSearchAdapter mPoiSearchAdapter;
private String city;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_address_map);
ButterKnife.bind(this);
initContentView1();
initContentView2();
startLocation();
}
private void initContentView1() {
mBaiduMap = mapView.getMap();
MapStatus mapStatus = new MapStatus.Builder().zoom(15).build();
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);
mBaiduMap.setMapStatus(mMapStatusUpdate);
// 地圖狀態(tài)改變相關(guān)監(jiān)聽
mBaiduMap.setOnMapStatusChangeListener(this);//讓MainActivity實(shí)現(xiàn)OnMapStatusChangeListener
// 創(chuàng)建GeoCoder實(shí)例對象
geoCoder = GeoCoder.newInstance();
// 設(shè)置查詢結(jié)果監(jiān)聽者
geoCoder.setOnGetGeoCodeResultListener(this);//讓MainActivity實(shí)現(xiàn)OnGetGeoCoderResultListener
rcy1.setLayoutManager(new LinearLayoutManager(this));
rcy1.setHasFixedSize(true);
mPoiListAdapter = new PoiListAdapter(R.layout.item_poi, poiInfos);
mPoiListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
String name = poiInfos.get(position).name;
String province = poiInfos.get(position).province;
String city = poiInfos.get(position).city;
String area = poiInfos.get(position).area;
LatLng location = poiInfos.get(position).location;
L.show("選點(diǎn)列表click---" + poiInfos.get(position).toString());
if (location == null) {
T.show("未獲取到該地點(diǎn)經(jīng)緯度信息徒仓,請重新輸入");
return;
}
Intent intent = new Intent();
intent.putExtra("name", name);
intent.putExtra("province", province);
intent.putExtra("city", city);
intent.putExtra("area", area);
intent.putExtra("lat", location.latitude);
intent.putExtra("lng", location.longitude);
setResult(RESULT_OK, intent);
finish();
}
});
rcy1.setAdapter(mPoiListAdapter);
}
private void initContentView2() {
//----------------------------poi搜索模塊設(shè)置璃岳,注冊搜索事件監(jiān)聽---------------------------//
rcy2.setLayoutManager(new LinearLayoutManager(this));
rcy2.setHasFixedSize(true);
mPoiSearchAdapter = new PoiSearchAdapter(R.layout.item_poi_search, searchInfos);
mPoiSearchAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
String name = searchInfos.get(position).name;
String province = searchInfos.get(position).province;
String city = searchInfos.get(position).city;
String area = searchInfos.get(position).area;
LatLng location = searchInfos.get(position).location;
L.show("搜索列表click---" + searchInfos.get(position).toString());
if (location == null) {
T.show("未獲取到該地點(diǎn)經(jīng)緯度信息涡尘,請重新輸入");
return;
}
Intent intent = new Intent();
intent.putExtra("name", name);
intent.putExtra("province", province);
intent.putExtra("city", city);
intent.putExtra("area", area);
intent.putExtra("lat", location.latitude);
intent.putExtra("lng", location.longitude);
setResult(RESULT_OK, intent);
finish();
}
});
rcy2.setAdapter(mPoiSearchAdapter);
PoiSearch mPoiSearch = PoiSearch.newInstance();
//讓MainActivity實(shí)現(xiàn)OnGetPoiSearchResultListener趁冈,當(dāng)系統(tǒng)定位成功則調(diào)用onGetPoiResult()方法
mPoiSearch.setOnGetPoiSearchResultListener(this);
mSearchContent.addTextChangedListener(new TextWatcher() {
@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) {
if (s.toString().length() <= 0) {
return;
}
//根據(jù)關(guān)鍵字搜索poi信息
mPoiSearch.searchInCity((new PoiCitySearchOption())
.cityLimit(false)
.city(city)
.keyword(s.toString())
.pageCapacity(20));
}
});
}
// 輸入搜索回調(diào)
@Override
public void onGetPoiResult(PoiResult result) {
//獲取POI檢索結(jié)果
if (result == null || result.getAllPoi() == null || result.getAllPoi().size() == 0) {
T.show(R.string.empty_data);
return;
}
L.show("----------------------當(dāng)前搜索結(jié)果---------------------------");
for (PoiInfo info : result.getAllPoi()) {
L.show("當(dāng)前的搜索信息:" + info.name + " " + info.address);
}
searchInfos.clear();
searchInfos.addAll(result.getAllPoi());
mPoiSearchAdapter.notifyDataSetChanged();
}
@Override
public void onMapStatusChangeFinish(MapStatus mapStatus) {
// 獲取地圖最后狀態(tài)改變的中心點(diǎn)
LatLng cenpt = mapStatus.target;
L.show("最后停止點(diǎn):" + cenpt.latitude + "," + cenpt.longitude);
//將中心點(diǎn)坐標(biāo)轉(zhuǎn)化為具體位置信息歼争,當(dāng)轉(zhuǎn)化成功后調(diào)用onGetReverseGeoCodeResult()方法
geoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(cenpt));
}
//經(jīng)緯度轉(zhuǎn)化地址
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
L.show("中心點(diǎn)轉(zhuǎn)為地址-----" + reverseGeoCodeResult.toString());
ReverseGeoCodeResult.AddressComponent detail = reverseGeoCodeResult.getAddressDetail();
String province = detail.province;
String city = detail.city;
String area = detail.distance;
List<PoiInfo> infos = reverseGeoCodeResult.getPoiList();
for (int i = 0; i < poiInfos.size(); i++) {
L.show("附近位置:" + poiInfos.get(i).name);
}
if (infos != null && !"".equals(infos)) {
//創(chuàng)建poiAdapter 將獲取到的Poi數(shù)據(jù)傳入,更新UI
poiInfos.clear();
poiInfos.addAll(infos);
mPoiListAdapter.notifyDataSetChanged();
}
}
private void startLocation() {
mBaiduMap.setMyLocationEnabled(true);
// 定位圖層顯示方式
MyLocationConfiguration.LocationMode mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;
mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(mCurrentMode, true, null));
mLocClient = new LocationClient(this);
//注冊LocationListener監(jiān)聽器
mLocationListener = new MyLocationListener();
mLocClient.registerLocationListener(mLocationListener);
// 定位參數(shù)選項(xiàng)
LocationClientOption option = new LocationClientOption();
option.setCoorType("bd09ll");
// 設(shè)置是否需要地址信息渗勘,默認(rèn)為無地址
option.setIsNeedAddress(true);
// 設(shè)置是否需要返回位置語義化信息沐绒,可以在BDLocation.getLocationDescribe()中得到數(shù)據(jù),ex:"在天安門附近"旺坠,
// 可以用作地址信息的補(bǔ)充
option.setIsNeedLocationDescribe(true);
// 設(shè)置是否需要返回位置POI信息乔遮,可以在BDLocation.getPoiList()中得到數(shù)據(jù)
option.setIsNeedLocationPoiList(true);
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
// 設(shè)置是否打開gps進(jìn)行定位
option.setOpenGps(true);
// 設(shè)置 LocationClientOption
mLocClient.setLocOption(option);
// 開始定位
mLocClient.start();
}
class MyLocationListener extends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
//mapView 銷毀后不在處理新接收的位置
if (location == null || mapView == null) {
return;
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此處設(shè)置開發(fā)者獲取到的方向信息,順時(shí)針0-360
.direction(location.getDirection())
.latitude(location.getLatitude())
.longitude(location.getLongitude())
.build();
mBaiduMap.setMyLocationData(locData);
LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLngZoom(ll, 18);
mBaiduMap.animateMapStatus(msu);
LatLng locationLatLng = new LatLng(location.getLatitude(), location.getLongitude());
// 獲取城市取刃,待會(huì)用于POISearch
city = location.getCity();
// 發(fā)起反地理編碼請求(經(jīng)緯度->地址信息)
ReverseGeoCodeOption reverseGeoCodeOption = new ReverseGeoCodeOption();
// 設(shè)置反地理編碼位置坐標(biāo)
reverseGeoCodeOption.location(new LatLng(location.getLatitude(), location.getLongitude()));
geoCoder.reverseGeoCode(reverseGeoCodeOption);
//只需要打開該頁面時(shí)定位一次即可蹋肮,定位成功后關(guān)閉定位功能
mLocClient.stop();
mBaiduMap.setMyLocationEnabled(false);
}
}
@OnClick({R.id.mAddressMapBack, R.id.mAddressMapSearch})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.mAddressMapBack:
onBackPressed();
break;
case R.id.mAddressMapSearch:
mContentLayout.setVisibility(View.GONE);
rcy2.setVisibility(View.VISIBLE);
break;
}
}
@Override
public void onBackPressed() {
if (rcy2.getVisibility() == View.VISIBLE) {
if (KeyboardUtils.isSoftInputVisible(this))
KeyboardUtils.hideSoftInput(this);
mContentLayout.setVisibility(View.VISIBLE);
rcy2.setVisibility(View.GONE);
} else {
super.onBackPressed();
}
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onDestroy() {
mLocClient.unRegisterLocationListener(mLocationListener);
mBaiduMap.setMyLocationEnabled(false);
mBaiduMap.clear();
mBaiduMap = null;
mapView.onDestroy();
mapView = null;
super.onDestroy();
}
@Override
public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
}
@Override
public void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) {
}
@Override
public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {
}
@Override
public void onMapStatusChangeStart(MapStatus mapStatus) {
}
@Override
public void onMapStatusChangeStart(MapStatus mapStatus, int i) {
}
@Override
public void onMapStatusChange(MapStatus mapStatus) {
}
@Override
public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:clipToPadding="false"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar style="@style/AppToolbar">
<ImageView
android:id="@+id/mAddressMapBack"
style="@style/AppBackImg"
android:paddingRight="20dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="12dp"
android:background="@color/white"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:src="@mipmap/ic_search" />
<EditText
android:id="@+id/mAddressMapSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:hint="查找小區(qū)出刷,大廈,學(xué)校"
android:singleLine="true"
android:textColor="@color/text_color"
android:textSize="14sp" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:id="@+id/mAddressMapContentLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:fitsSystemWindows="true"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="vertical"
android:visibility="visible">
<com.baidu.mapapi.map.MapView
android:id="@+id/mAddressMapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@android:color/transparent"
android:src="@mipmap/icon_gcoding" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/mAddressMapRcy1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:cacheColorHint="#00000000"
android:descendantFocusability="beforeDescendants"
android:fastScrollEnabled="true"
android:scrollbars="none" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/mAddressMapRcy2"
android:layout_width="match_parent"
android:background="@color/pager"
android:layout_height="match_parent"
android:visibility="gone" />
</LinearLayout>
附錄: