經過兩天的折騰,終于把需求實作了,記錄下程序與遇到的坑(詳情鏈接)
先上效果圖
搜索結果

點擊串列中的去這里調取第三方地圖APP,效果圖

首先配置百度地圖
bBaiduMap = mMapView.getMap();
bBaiduMap.setMyLocationEnabled(true);
//定位初始化
mLocationClient = new LocationClient(getActivity());
MapStatus.Builder builder = new MapStatus.Builder();
builder.zoom(17.0f);
bBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
//通過LocationClientOption設定LocationClient相關引數
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); //打開gps
option.setCoorType("bd09ll"); //設定坐標型別
option.setScanSpan(10000);
option.setIsNeedAddress(true);
//設定locationClientOption
mLocationClient.setLocOption(option);
//注冊LocationListener監聽器
MyLocationListener myLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(myLocationListener);
//開啟地圖定位圖層
mLocationClient.start();
搜索結果資料
poiSearch = PoiSearch.newInstance();
poiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
@Override
public void onGetPoiResult(PoiResult poiResult) {
MyLog.e("PoiResult " + poiResult.error);
if (poiResult.error == SearchResult.ERRORNO.NO_ERROR) {
beans.clear();
List<PoiInfo> allPoi = poiResult.getAllPoi();
for (PoiInfo info : allPoi) {
MapBean bean = new MapBean();
bean.city = info.city;
// bean.distance = info.distance + "";
bean.name = info.name;
bean.addr = info.address;
bean.iv = getResources().getDrawable(R.mipmap.msg_img);
beans.add(bean);
}
adapter.setList(beans);
}
}
@Override
public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
MyLog.e("PoiDetailResult " + poiDetailResult.error);
}
@Override
public void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) {
MyLog.e("poiDetailSearchResult " + poiDetailSearchResult.error);
}
@Override
public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {
MyLog.e("poiIndoorResult " + poiIndoorResult.error);
}
});
搜索事件
poiSearch.searchInCity(new PoiCitySearchOption()
.city(city)
.keyword(v.getText().toString())//搜索的關鍵字
.pageCapacity(20)//一次搜索多少條資料
.pageNum(0));//0代表第1頁
地址監聽
public class MyLocationListener extends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
//mapView 銷毀后不在處理新接收的位置
if (location == null || mMapView == null) {
return;
}
city = location.getCity();
longitude = location.getLongitude();
latitude = location.getLatitude();
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此處設定開發者獲取到的方向資訊,順時針0-360
.direction(location.getDirection()).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
bBaiduMap.setMyLocationData(locData);
LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
if (isFirstLocate) {
isFirstLocate = false;
//給地圖設定狀態
bBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLng(ll));
}
}
}
點擊調取第三方地圖APP事件
mSearch.geocode(new GeoCodeOption()
.city(m.city)//城市
.address(m.addr));//詳細地址
mSearch = GeoCoder.newInstance();
mSearch.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
@Override
public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
LatLng location = geoCodeResult.getLocation();
String address = geoCodeResult.getAddress();
if (location != null) {
if (MapNaviUtils.isBaiduMapInstalled()) {//手機內是否安裝了百度地圖
MapNaviUtils.openBaiDuNavi(getActivity(), addr);
} else if (MapNaviUtils.isGdMapInstalled()) {//手機內是否安裝了高德地圖
MapNaviUtils.openGaoDeNavi(getActivity(), addr);
} else {//都沒安裝下載百度地圖
new AlertDialog.Builder(getActivity())
.setMessage("下載百度地圖?")
.setNegativeButton("取消", null)
.setPositiveButton("下載", (dialog12, which12) -> startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(MapNaviUtils.DOWNLOAD_BAIDU_MAP))))
.show();
}
} else {
MyLog.e("onGetGeoCodeResult location=null ");
}
}
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
}
});
打開百度地圖
public static void openBaiDuNavi(Context context, String dname) {
String uri = "baidumap://map/geocoder?src=com.construction5000.yun&address=";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setPackage(PN_BAIDU_MAP);
intent.setData(Uri.parse(uri + dname));
context.startActivity(intent);
}
打開高德地圖
public static void openGaoDeNavi(Activity context, String dname) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setPackage(PN_GAODE_MAP);
intent.setData(Uri.parse("androidamap://keywordNavi?sourceApplication=softname&keyword="+dname+"&style=2"));
context.startActivity(intent);
}
下篇遇到的坑(詳情鏈接)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/282140.html
標籤:其他
上一篇:iOS分配終端界面功能實作:1、拆分SN 2、計算SN個數( 號段用‘-’連接;每批號段請用‘,’|‘隔開或分行)
