最近由于项目需要,研究了下百度地图定位,他们提供的实例基本都是用监听器实现自动定位的。我想实现一种效果:当用户进入UI时,不定位,用户需要定位的时候,自己手动点击按钮,再去定位当前位置。 经过2天研究和咨询,找到了解决方案,在此备忘一下。
注意:定位使用真机才能够真正定位;模拟器的话,在DDMS中的Emulator Control中,选择Manual,下面单选按钮选择Decimal,然后填写经纬度,send后,再点击定位我的位置按钮,就能定位了(这应该算是固定定位,哈哈。。。)、
1、第一步当然是获取一个针对自己项目的key值。http://dev.baidu.com/wiki/static/imap/key/
2、使用百度API是有前提的,摘自百度:首先将API包括的两个文件baidumapapi.jar和libBMapApiEngine.so拷贝到工程根目录及libs\armeabi目录下,并在工程属性->Java Build Path->Libraries中选择“Add JARs”,选定baidumapapi.jar,确定后返回,这样您就可以在您的程序中使用API了。(这两个文件见附件)。
3、按照自己的需求写一个layout,我的如下:
<?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/myLocation_id"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="15dp"
- android:gravity="center_horizontal"
- android:textColor="@drawable/black"
- android:background="@drawable/gary"
- />
- <com.baidu.mapapi.MapViewandroid:id="@+id/bmapsView"
- android:layout_width="fill_parent"android:layout_height="fill_parent"
- android:clickable="true"android:layout_weight="1"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/location_button_id"
- android:text="@string/location_button_text"
- />
- </LinearLayout>
需要特别注意的是:<com.baidu.mapapi.MapView /> 这玩意。
4、写一个MapApplication实现application,提供全局的BMapManager,以及其初始化。
- publicBMapManagermapManager=null;
- staticMapApplicationapp;
- publicStringmStrKey="你申请的key值";
- @Override
- publicvoidonCreate(){
- mapManager=newBMapManager(this);
- mapManager.init(mStrKey,newMyGeneralListener());
- }
- @Override
- publicvoidonTerminate(){
- if(mapManager!=null)
- {
- mapManager.destroy();
- mapManager=null;
- }
- super.onTerminate();
- }
- staticclassMyGeneralListenerimplementsMKGeneralListener{
- @Override
- publicvoidonGetNetworkState(intarg0){
- Toast.makeText(MapApplication.app.getApplicationContext(),"您的网络出错啦!",
- Toast.LENGTH_LONG).show();
- }
- @Override
- publicvoidonGetPermissionState(intiError){
- if(iError==MKEvent.ERROR_PERMISSION_DENIED){
- Toast.makeText(MapApplication.app.getApplicationContext(),"您的授权Key不正确!",
- Toast.LENGTH_LONG).show();
- }
- }
- }
- 5、接下来就是按照百度api写定位代码了,使用handler机制去添加定位图层,需要说明的都在注释上了。
- privateBMapManagermBMapMan=null;
- privateMapViewmMapView=null;
- privateMapControllerbMapController;
- privateMKLocationManagermkLocationManager;
- privateMKSearchmkSearch;
- privateTextViewaddress_view;
- privateProgressDialogdialog;
- privateList<HotelInfo>hotelList;
- privateintdistance=1000;
- Handlerhandler=newHandler(){
- @Override
- publicvoidhandleMessage(Messagemsg){
- doublelat=msg.getData().getDouble("lat");
- doublelon=msg.getData().getDouble("lon");
- if(lat!=0&&lon!=0){
- GeoPointpoint=newGeoPoint(
- (int)(lat*1E6),
- (int)(lon*1E6));
- bMapController.animateTo(point);
- bMapController.setZoom(15);
- mkSearch.reverseGeocode(point);
- MyLocationOverlaymyLoc=newMyLocationOverlayFromMap(ShowMapAct.this,mMapView);
- myLoc.enableMyLocation();
- myLoc.enableCompass();
- mMapView.getOverlays().add(myLoc);
- }else{
- Toast.makeText(ShowMapAct.this,"没有加载到您的位置",Toast.LENGTH_LONG).show();
- }
- if(hotelList!=null){
- Drawablemarker=getResources().getDrawable(R.drawable.iconmarka);
- marker.setBounds(0,0,marker.getIntrinsicWidth(),marker.getIntrinsicHeight());
- mMapView.getOverlays().add(newOverItemList(marker,hotelList,ShowMapAct.this,bMapController));
- }elseif(hotelList==null&&lat!=0&&lon!=0){
- Toast.makeText(ShowMapAct.this,"网络异常,没有获取到酒店信息。",Toast.LENGTH_LONG).show();
- }
- if(dialog!=null)dialog.dismiss();
- }
- };
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- distance=getIntent().getExtras().getInt("distance");
- super.onCreate(savedInstanceState);
- setContentView(R.layout.location);
- mMapView=(MapView)findViewById(R.id.bmapsView);
- init();
- super.initMapActivity(mBMapMan);
- address_view=(TextView)findViewById(R.id.myLocation_id);
- SpannableStringBuilderstyle=newSpannableStringBuilder(String.format(getResources().getString(R.string.location_text),"位置不详"));
- style.setSpan(newForegroundColorSpan(Color.RED),5,style.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- address_view.setText(style);
- Buttonlocation_button=(Button)findViewById(R.id.location_button_id);
- location_button.setOnClickListener(newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- dialog=ProgressDialog.show(ShowMapAct.this,"","数据加载中,请稍后.....");
- newThread(newMyThread()).start();
- }
- });
- mkSearch=newMKSearch();
- mkSearch.init(mBMapMan,this);
- mMapView.setBuiltInZoomControls(true);
- bMapController=mMapView.getController();
- GeoPointdefaultPoint=newGeoPoint((int)(39.920934*1E6),(int)(116.412817*1E6));
- bMapController.setCenter(defaultPoint);
- bMapController.setZoom(12);
- mkLocationManager=mBMapMan.getLocationManager();
- }
- publicvoidinit(){
- MapApplicationapp=(MapApplication)getApplication();
- if(app.mapManager==null){
- app.mapManager=newBMapManager(getApplication());
- app.mapManager.init(app.mStrKey,newMapApplication.MyGeneralListener());
- }
- mBMapMan=app.mapManager;
- }
- @Override
- protectedvoidonDestroy(){
- MapApplicationapp=(MapApplication)getApplication();
- if(mBMapMan!=null){
- mBMapMan.destroy();
- app.mapManager.destroy();
- app.mapManager=null;
- mBMapMan=null;
- }
- super.onDestroy();
- }
- @Override
- protectedvoidonPause(){
- if(mBMapMan!=null){
- mBMapMan.stop();
- }
- super.onPause();
- }
- @Override
- protectedvoidonResume(){
- if(mBMapMan!=null){
- mBMapMan.start();
- }
- super.onResume();
- }
- @Override
- protectedbooleanisRouteDisplayed(){
- returnfalse;
- }
- @Override
- publicvoidonGetAddrResult(MKAddrInforesult,intiError){
- if(result==null)return;
- SpannableStringBuilderstyle=newSpannableStringBuilder(String.format(getResources().getString(R.string.location_text),result.strAddr));
- style.setSpan(newForegroundColorSpan(Color.RED),5,style.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- address_view.setText(style);
- if(dialog!=null)dialog.dismiss();
- }
- @Override
- publicvoidonGetDrivingRouteResult(MKDrivingRouteResultarg0,intarg1){}
- @Override
- publicvoidonGetPoiResult(MKPoiResultarg0,intarg1,intarg2){}
- @Override
- publicvoidonGetTransitRouteResult(MKTransitRouteResultarg0,intarg1){}
- @Override
- publicvoidonGetWalkingRouteResult(MKWalkingRouteResultarg0,intarg1){}
- classMyThreadimplementsRunnable{
- @Override
- publicvoidrun(){
- Locationlocation=mkLocationManager.getLocationInfo();
- doublelat=0d,lon=0d;
- if(location!=null){
- Stringcoordinate=location.getLatitude()+","+location.getLongitude();
- HotelRemoteDatahotelData=newHotelRemoteData();
- hotelList=hotelData.getHotelToMap(coordinate,distance);
- lat=location.getLatitude();
- lon=location.getLongitude();
- }
- Messagemsg=newMessage();
- Bundledata=newBundle();
- data.putDouble("lat",lat);
- data.putDouble("lon",lon);
- msg.setData(data);
- handler.sendMessage(msg);
- }
- }
6、还有一种就是百度示例相当推荐的,也是加载定位位置速度比较快的,那就是通过定位监听器来定位信息。没啥难的,照着百度的示例写,都能搞定。
- LocationListenerlistener=newLocationListener(){
- @Override
- publicvoidonLocationChanged(Locationlocation){
- GeoPointgp=newGeoPoint((int)(location.getLatitude()*1E6),(int)(location.getLongitude()*1E6));
- bMapController.animateTo(gp);
- }
- };
分享到:
相关推荐
百度地图定位api示例 定位+路线规划 (存在小bug未解决)
百度地图定位规划+导航,想学习的就下载吧
android 百度地图定位系统 源码 动态获取目标位置,自动定位
百度地图定位自己的位置~ 有需要的下载,
基于Android的百度地图车辆定位系统设计与实现 基于Android的百度地图车辆定位系统设计与实现 基于Android的百度地图车辆定位系统设计与实现 基于Android的百度地图车辆定位系统设计与实现 基于Android的百度...
android 百度地图 定位模块资料+源码
Android版百度地图定位自己位置,可实现卫星图和普通地图两个图层的切换。
android百度地图定位当前位置 截取当前位置的小图片
Android开发之百度地图定位打卡
写的百度地图定位的demo,百度地图根据坐标实现定位
打开即可用!不要998,只要2分就可以啦 打开即可用!不要998,只要2分就可以啦
Android百度地图定位demo blog:http://blog.csdn.net/shenyuanqing/article/details/48056289
调用百度地图定位同时获取经纬度,应该要更改成自己的百度地图key值,还有手机别忘了开权限,打开GPS
android百度地图定位、路线查询、城市查询、poi查询。代码里面有详细的注释说明,是本人参考各种资料整理编写出来的,很适合新手学习。欢迎下载交流!
1积分就可下载 Android安卓百度地图定位实现APP 下载即可用,显示地图、定位导航等文章说的功能 文章链接https://blog.csdn.net/u011752195/article/details/87887410
android,实现百度地图定位功能 ,代码清晰,能够实现基础定位功能,连续定位,室内定位
本人做的一个简单的Android studio 百度地图定位,里面的Key需要自己申请更换
android studio 2.0写的 百度GPS定位+获取经纬度 DEMO,每隔30秒定一次位。只要申请了百度API+KEY,就可以直接用。是开发手机定位的好参考DEMO。该DEMO也是本人在开发一个项目时写的测试,所以参考价值还是比较高的...
使用android实现百度地图的定位功能及当前位置周边的兴趣点搜索