百度地图定位与检索

百度地图开放平台   http://lbsyun.baidu.com/

最近闲来突然心血来潮想写一些关于地图的小dome

首先做出来的就是定位与检索。

初始化的步骤:

  1. (1)在application中添加开发密钥
  2. <application>
  3. <meta-data
  4. android:name="com.baidu.lbsapi.API_KEY"
  5. android:value="开发者 key" />
  6. </application>
  7. 2)添加所需权限
  8. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  9. <uses-permission android:name="android.permission.INTERNET"/>
  10. <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
  11. <uses-permission android:name="android.permission.WAKE_LOCK"/>
  12. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
  13. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  14. <uses-permission android:name="android.permission.GET_TASKS" />
  15. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  16. <uses-permission android:name="android.permission.WRITE_SETTINGS" />
  17. 第三步,在布局xml文件中添加地图控件;
  18. <com.baidu.mapapi.map.MapView
  19. android:id="@+id/bmapView"
  20. android:layout_width="fill_parent"
  21. android:layout_height="fill_parent"
  22. android:clickable="true" />
  23. 第四步,在应用程序创建时初始化 SDK引用的Context 全局变量:
  24. public class MainActivity extends Activity {
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. //在使用SDK各组件之前初始化context信息,传入ApplicationContext
  29. //注意该方法要再setContentView方法之前实现
  30. SDKInitializer.initialize(getApplicationContext());
  31. setContentView(R.layout.activity_main);
  32. }

第五步,创建地图Activity,管理地图生命周期;

public class MainActivity extends Activity {  
    MapView mMapView = null;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);   
        //在使用SDK各组件之前初始化context信息,传入ApplicationContext  
        //注意该方法要再setContentView方法之前实现  
        SDKInitializer.initialize(getApplicationContext());  
        setContentView(R.layout.activity_main);  
        //获取地图控件引用  
        mMapView = (MapView) findViewById(R.id.bmapView);  
    }  
    @Override  
    protected void onDestroy() {  
        super.onDestroy();  
        //在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();  
        }  
    }

做完这些你就可以拥有一个简单的百度地图了然后就是定位,定位这一块一定一定不要忘了在你的清单文件注册服务不然你是获取不到任何位置信息的。

<service
  android:name="com.baidu.location.f"
  android:enabled="true"
   android:process=":remote" >
然后就是定位的一些逻辑代码了1,首先你得实现这个接口 BDLocationListener 然后从这个接口并重写 onReceiveLocation方法然后你就可以一
直点出来你想要的位置信息。
private class MyLocationListener implements BDLocationListener {


        @Override
        public void onReceiveLocation(BDLocation location) {
            mLocationBean = new LocationBean();
            //获取定位结果
            StringBuffer sb = new StringBuffer(256);


            sb.append("time : ");
            sb.append(location.getTime());    //获取定位时间
            mLocationBean.setLocationTime(location.getTime());


            sb.append("\nerror code : ");
            sb.append(location.getLocType());    //获取类型类型




            sb.append("\nlatitude : ");
            sb.append(location.getLatitude());    //获取纬度信息
            mLocationBean.setLocationLatitude(location.getLatitude());


            sb.append("\nlontitude : ");
            sb.append(location.getLongitude());    //获取经度信息
            mLocationBean.setLocationLongitude(location.getLongitude());


            sb.append("\nradius : ");
            sb.append(location.getRadius());    //获取定位精准度


            if (location.getLocType() == BDLocation.TypeGpsLocation){


                // GPS定位结果
                sb.append("\nspeed : ");
                sb.append(location.getSpeed());    // 单位:公里每小时


                sb.append("\nsatellite : ");
                sb.append(location.getSatelliteNumber());    //获取卫星数


                sb.append("\nheight : ");
                sb.append(location.getAltitude());    //获取海拔高度信息,单位米


                sb.append("\ndirection : ");
                sb.append(location.getDirection());    //获取方向信息,单位度


                sb.append("\naddr : ");
                sb.append(location.getAddrStr());    //获取地址信息
                mLocationBean.setLocationAddress(location.getAddrStr());


                sb.append("\ndescribe : ");
                sb.append("gps定位成功");




            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){


                // 网络定位结果
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());    //获取地址信息
                mLocationBean.setLocationAddress(location.getAddrStr());


                sb.append("\noperationers : ");
                sb.append(location.getOperators());    //获取运营商信息


                sb.append("\ndescribe : ");
                sb.append("网络定位成功");


            } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {


                // 离线定位结果
                sb.append("\ndescribe : ");
                sb.append("离线定位成功,离线定位结果也是有效的");


            } else if (location.getLocType() == BDLocation.TypeServerError) {


                sb.append("\ndescribe : ");
                sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到[email protected],会有人追查原因");


            } else if (location.getLocType() == BDLocation.TypeNetWorkException) {


                sb.append("\ndescribe : ");
                sb.append("网络不同导致定位失败,请检查网络是否通畅");


            } else if (location.getLocType() == BDLocation.TypeCriteriaException) {


                sb.append("\ndescribe : ");
                sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");


            }


            sb.append("\nlocationdescribe : ");
            sb.append(location.getLocationDescribe());    //位置语义化信息
            mLocationBean.setLocationDescribe(location.getLocationDescribe());


            List<Poi> list = location.getPoiList();    // POI数据
            if (list != null) {
                sb.append("\npoilist size = : ");
                sb.append(list.size());
                for (Poi p : list) {
                    sb.append("\npoi= : ");
                    sb.append(p.getId() + " " + p.getName() + " " + p.getRank());
                }
                mLocationListener.onSuccess(mLocationBean);
            }else{
                mLocationListener.onFile();
            }


           // Log.i("BaiduLocationApiDem", sb.toString());


        }


        @Override
        public void onConnectHotSpotMessage(String s, int i) {


        }
    }

这块有一个工具类,我会在博客底部附上github地址又需要的朋友可以下载一下
另一个就是检索位置信息
//创建POI检索实例
mPoiSearch = PoiSearch.newInstance();
baiduMap = mMapView.getMap();
// 设置POI检索监听者
mPoiSearch.setOnGetPoiSearchResultListener(poiListener);
//city检索的城市,keystr关键字,num检索条数
mPoiSearch.searchInCity((new PoiCitySearchOption()).city(city).keyword(keystr).pageNum(num));
// 创建POI检索监听者
OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {
public void onGetPoiResult(PoiResult result) {
// 获取POI检索结果
if (result == null
|| result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {// 没有找到检索结果
toastShort("未找到结果");
return;
}
if (result.error == SearchResult.ERRORNO.NO_ERROR) {// 检索结果正常返回
// baiduMap.clear();
baiduMap.clear();
// 创建PoiOverlay
PoiOverlay overlay = new MyPoiOverlay(baiduMap);
// 设置overlay可以处理标注点击事件
baiduMap.setOnMarkerClickListener(overlay);
// 设置PoiOverlay数据
overlay.setData(result);
// 添加PoiOverlay到地图中
overlay.addToMap();
overlay.zoomToSpan();


}

}

检索怎么可以不用覆盖物呢,覆盖物这块被百度开源了PoiOverlay这个类可以自己去下载,现在还很多地方能下载到我就没上传。

还有就是覆盖物的点击监听直接上代码

/**
* 覆盖物
*/
private class MyPoiOverlay extends PoiOverlay {
private PoiResult mPoiResult = null;


public MyPoiOverlay(BaiduMap baiduMap) {
super(baiduMap);
}


@Override
public void setData(PoiResult poiResult) {
// TODO Auto-generated method stub
super.setData(poiResult);
this.mPoiResult = poiResult;
}


@Override
public boolean onPoiClick(int index) {

//覆盖物点击监听,index覆盖物在地图上标识地址集合的下标
super.onPoiClick(index);
Log.e("tag", mPoiResult.getAllPoi().get(index).address);
Log.e("tag", "维度"+ mPoiResult.getAllPoi().get(index).location.latitude +"经度"+mPoiResult.getAllPoi().get(index).location.longitude);
return true;
}
}

个人认为百度开放平台写的还是很详细的。也不是什么大牛就是编码路上一直笨拙起飞的小鸟,博客可能有些措辞不太准确,对看到这篇博客的人有哪些不懂的一定详细

的说明,可以下下面留言。我的路还有好长,请大家多多包涵与指教

附上定位的一个小工具类:https://github.com/115MRLI/baidumap

 
   
 
  

猜你喜欢

转载自blog.csdn.net/jia_you1/article/details/77757570