Android百度定位导航之基于百度地图移动获取位置和自动定位
程序员文章站
2024-02-14 10:11:34
一、问题描述
使用百度地图实现如图所示应用,首先自动定位当前我起始位置(小圆点位置),并跟随移动不断自动定位我的当前位置
百度api不同版本使用会有些差...
一、问题描述
使用百度地图实现如图所示应用,首先自动定位当前我起始位置(小圆点位置),并跟随移动不断自动定位我的当前位置
百度api不同版本使用会有些差异,本例中加入lib如下:
二、编写myapplication类
public class myapplication extends application { static myapplication myapplication; bmapmanager mbmapmanager = null; string mstrkey = "7zfurcox1g3oz8tkutngm4so"; boolean m_bkeyright = true; // 授权key正确,验证通过 private list<activity> activitylist = new linkedlist<activity>(); @override public void oncreate() { myapplication = this; initenginemanager(this); super.oncreate(); } public void initenginemanager(context context) { if (mbmapmanager == null) { mbmapmanager = new bmapmanager(context); toast.maketext(myapplication.getinstance().getapplicationcontext(), "bmapmanager 初始化sucsuc!", toast.length_long).show(); } if (!mbmapmanager.init(mstrkey, new mygenerallistener())) { toast.maketext(myapplication.getinstance().getapplicationcontext(), "bmapmanager 初始化错误!", toast.length_long).show(); } } public static myapplication getinstance() { if (myapplication == null) { myapplication = new myapplication(); } return myapplication; } public void addactivity(activity activity) { activitylist.add(activity); } public void exit() { for (activity activity : activitylist) { activity.finish(); } system.exit(0); } // 常用事件监听,用来处理通常的网络错误,授权验证错误等 static class mygenerallistener implements mkgenerallistener { public void ongetnetworkstate(int ierror) { if (ierror == mkevent.error_network_connect) { toast.maketext( myapplication.getinstance().getapplicationcontext(), "您的网络出错啦!", toast.length_long).show(); } else if (ierror == mkevent.error_network_data) { toast.maketext( myapplication.getinstance().getapplicationcontext(), "输入正确的检索条件!", toast.length_long).show(); } // ... } public void ongetpermissionstate(int ierror) { if (ierror == mkevent.error_permission_denied) { // 授权key错误: toast.maketext( myapplication.getinstance().getapplicationcontext(), "请输入正确的授权key!", toast.length_long).show(); myapplication.getinstance().m_bkeyright = false; } } } }
三、编写主程序mainactivity,显示当前所处位置
主程序mainactity:
public class mainactivity extends activity { public myapplication app; static mapview mmapview = null; public mkmapviewlistener mmaplistener = null; mylocationoverlay mylocationoverlay = null; // 定位相关 locationclient mlocclient; public notifylister mnotifyer = null; public mylocationlistenner mylistener = new mylocationlistenner(); locationdata locdata = null; private mapcontroller mmapcontroller = null; static mksearch mkserach; handler mhandler = new handler() { public void handlemessage(android.os.message msg) { toast.maketext(mainactivity.this, "msg:" + msg.what, toast.length_short).show(); }; }; static textview showaddr; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); showaddr = (textview) findviewbyid(r.id.showaddr); myicon mi = new myicon(this); //在屏幕中心点添加接我图标 getwindow().addcontentview( mi, new layoutparams(layoutparams.fill_parent, layoutparams.fill_parent)); myicon2 m2 = new myicon2(this); getwindow().addcontentview( m2, new layoutparams(layoutparams.fill_parent, layoutparams.fill_parent)); mmapview = (mapview) findviewbyid(r.id.bmapsview); mmapcontroller = mmapview.getcontroller(); initmapview(); app = myapplication.getinstance(); mlocclient = new locationclient(this); mlocclient.registerlocationlistener(mylistener); //搜索初始化 mkserach = new mksearch(); mkserach.init(app.mbmapmanager, new mksearchlistener() { @override public void ongetwalkingrouteresult(mkwalkingrouteresult arg0, int arg1) { // todo auto-generated method stub } @override public void ongettransitrouteresult(mktransitrouteresult arg0, int arg1) { // todo auto-generated method stub } @override public void ongetsuggestionresult(mksuggestionresult arg0, int arg1) { // todo auto-generated method stub } @override public void ongetpoiresult(mkpoiresult arg0, int arg1, int arg2) { // todo auto-generated method stub } @override public void ongetpoidetailsearchresult(int arg0, int arg1) { // todo auto-generated method stub } @override public void ongetdrivingrouteresult(mkdrivingrouteresult arg0, int arg1) { // todo auto-generated method stub } @override public void ongetbusdetailresult(mkbuslineresult arg0, int arg1) { // todo auto-generated method stub } @override public void ongetaddrresult(mkaddrinfo info, int arg1) { showaddr.settext(info.straddr); } }); //设置地图相关 locationclientoption option = new locationclientoption(); option.setopengps(true); option.setcoortype("bd09ll"); option.setscanspan(300000); mlocclient.setlocoption(option); mlocclient.start(); mmapview.getcontroller().setzoom(16); mmapview.getcontroller().enableclick(true); mmapview.displayzoomcontrols(true); mmaplistener = new mkmapviewlistener() { public void onmapmovefinish() { } public void onclickmappoi(mappoi mappoiinfo) { // todo auto-generated method stub string title = ""; if (mappoiinfo != null) { title = mappoiinfo.strtext; toast.maketext(mainactivity.this, title, toast.length_short) .show(); } } }; mmapview.regmapviewlistener(myapplication.getinstance().mbmapmanager, mmaplistener); mylocationoverlay = new mylocationoverlay(mmapview); locdata = new locationdata(); mylocationoverlay.setdata(locdata); mmapview.getoverlays().add(mylocationoverlay); mylocationoverlay.enablecompass(); mmapview.refresh(); } private void initmapview() { mmapview.setlongclickable(true); } /** * 监听函数,又新位置的时候,格式化成字符串,输出到屏幕中 */ public class mylocationlistenner implements bdlocationlistener { public void onreceivelocation(bdlocation location) { if (location == null) return; locdata.latitude = location.getlatitude(); locdata.longitude = location.getlongitude(); locdata.direction = 2.0f; locdata.accuracy = location.getradius(); locdata.direction = location.getderect(); log.d("loctest", string.format("before: lat: %f lon: %f", location.getlatitude(), location.getlongitude())); mylocationoverlay.setdata(locdata); mmapview.refresh(); mmapcontroller .animateto(new geopoint((int) (locdata.latitude * 1e6), (int) (locdata.longitude * 1e6)), mhandler .obtainmessage(1)); } public void onreceivepoi(bdlocation poilocation) { if (poilocation == null) { return; } } } public class notifylister extends bdnotifylistener { public void onnotify(bdlocation mlocation, float distance) { } } @override protected void onpause() { mmapview.onpause(); super.onpause(); } @override protected void onresume() { mmapview.onresume(); super.onresume(); } @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); mmapview.onsaveinstancestate(outstate); } @override protected void onrestoreinstancestate(bundle savedinstancestate) { super.onrestoreinstancestate(savedinstancestate); mmapview.onrestoreinstancestate(savedinstancestate); } public static void getposition(geopoint g) { mkserach.reversegeocode(g); showaddr.settext("获取位置中..."); } }
xml布局:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <textview android:id="@+id/showaddr" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="选择地点" android:textsize="20dp" /> <com.baidu.mapapi.map.mapview android:id="@+id/bmapsview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" /> </linearlayout>
三、绘制图形
myicon 的ondraw实现绘制中心点的图标,myicon2绘制指示器图标,如图所示
public class myicon extends view { public static int w; public static int h; public static bitmap mbitmap; public myicon(context context) { super(context); mbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.me); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); w = this.getwidth() / 2 - mbitmap.getwidth() / 2; h = this.getheight() / 2 - mbitmap.getheight() / 2; canvas.drawbitmap(mbitmap, w, h, null); } } public class myicon2 extends view{ public static int w; public static int h; private bitmap mbitmap; public myicon2(context context) { super(context); mbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.jiewo); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); w = this.getwidth() / 2 - mbitmap.getwidth() / 2; h = (this.getheight() / 2 - mbitmap.getheight() / 2) - ( myicon.mbitmap.getheight()/2); canvas.drawbitmap(mbitmap, w, h, null); } }
以上所述是小编给大家分享android百度定位导航之基于百度地图移动获取位置和自动定位的相关知识,希望对大家有所帮助。