欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android百度地图实现搜索和定位及自定义图标绘制并点击时弹出泡泡

程序员文章站 2024-02-19 21:50:16
一、问题描述   上一次我们使用百度地图实现基本的定位功能,接下来我们继续实现搜索和定位,并使用locationoverlay绘制定位位置,同时展示如何使用自定义图标...

一、问题描述

  上一次我们使用百度地图实现基本的定位功能,接下来我们继续实现搜索和定位,并使用locationoverlay绘制定位位置,同时展示如何使用自定义图标绘制并点击时弹出泡泡

  如图所示:

Android百度地图实现搜索和定位及自定义图标绘制并点击时弹出泡泡

二、编写myapplication类

public class myapplication extends application {
private static myapplication minstance = null;
public boolean m_bkeyright = true;
public bmapmanager mbmapmanager = null;
public static final string strkey = "申请的应用key";
@override
public void oncreate() {
super.oncreate();
minstance = this;
initenginemanager(this);
}
public void initenginemanager(context context) {
if (mbmapmanager == null) {
mbmapmanager = new bmapmanager(context);
}
if (!mbmapmanager.init(strkey,new mygenerallistener())) {
toast.maketext(myapplication.getinstance().getapplicationcontext(), "bmapmanager 初始化错误!", toast.length_long).show();
}
}
public static myapplication getinstance() {
return minstance;
}
// 常用事件监听,用来处理通常的网络错误,授权验证错误等
public static class mygenerallistener implements mkgenerallistener {
@override
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();
}
}
@override
public void ongetpermissionstate(int ierror) {
//非零值表示key验证未通过
if (ierror != 0) {
//授权key错误:
toast.maketext(myapplication.getinstance().getapplicationcontext(), 
"请在 demoapplication.java文件输入正确的授权key,并检查您的网络连接是否正常!error: "+ierror, toast.length_long).show();
myapplication.getinstance().m_bkeyright = true;
}
else{
myapplication.getinstance().m_bkeyright = true;
//toast.maketext(demoapplication.getinstance().getapplicationcontext(), "key认证成功", toast.length_long).show();
}
}
}
}

三、编写mylocationmapview,继承mapview重写ontouchevent实现泡泡处理操作

public class mylocationmapview extends mapview {


public static popupoverlay pop = null;// 弹出泡泡图层,点击图标使用
public mylocationmapview(context context) {
super(context);
}
public mylocationmapview(context context, attributeset attrs) {
super(context, attrs);
}
public mylocationmapview(context context, attributeset attrs, int defstyle) {
super(context, attrs, defstyle);
}
@override
public boolean ontouchevent(motionevent event) {
if (!super.ontouchevent(event)) {
// 消隐泡泡
if (pop != null && event.getaction() == motionevent.action_up)
pop.hidepop();
}
return true;
}
}

三、编写主程序mainactivity

  编写主程序mainactivity,用来展示如何结合定位sdk实现定位,并使用mylocation overlay绘制定位位置 同时展示如何使用自定义图标绘制并点击时弹出泡泡。

public class mainactivity extends activity {
private edittext txtaddr;
// 定位相关
locationclient mlocclient;
locationdata locdata = null;
public mylocationlistenner mylistener = new mylocationlistenner();
public myapplication app;
//定位图层
locationoverlay mylocationoverlay = null;
//弹出泡泡图层
private popupoverlay pop = null;//弹出泡泡图层,浏览节点时使用
private textview popuptext = null;//泡泡view
private view viewcache = null;
//地图相关,使用继承mapview的mylocationmapview目的是重写touch事件实现泡泡处理
//如果不处理touch事件,则无需继承,直接使用mapview即可
public mylocationmapview mmapview = null; // 地图view
private mapcontroller mmapcontroller = null;
private mksearch mmksearch = null;//用于信息检索服务 
//ui相关
oncheckedchangelistener radiobuttonlistener = null;
textview requestlocbutton ,btserach;
boolean isrequest = false;//是否手动触发请求定位
boolean isfirstloc = true;//是否首次定位
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
/**
* 使用地图sdk前需先初始化bmapmanager.
* bmapmanager是全局的,可为多个mapview共用,它需要地图模块创建前创建,
* 并在地图地图模块销毁后销毁,只要还有地图模块在使用,bmapmanager就不应该销毁
*/
app = (myapplication)this.getapplication();
if (app.mbmapmanager == null) {
app.mbmapmanager = new bmapmanager(getapplicationcontext());
/**
* 如果bmapmanager没有初始化则初始化bmapmanager
*/
app.mbmapmanager.init(myapplication.strkey,new myapplication.mygenerallistener());
}
setcontentview(r.layout.activity_main);
txtaddr=(edittext)findviewbyid(r.id.txtaddr);//关键字输入框
//监听搜索单击事件
btserach= (textview)findviewbyid(r.id.btok);
btserach.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
mmksearch.poisearchincity("", txtaddr.gettext().tostring()); 
}
});
//定位按钮
requestlocbutton = (textview)findviewbyid(r.id.btget);
requestlocbutton.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
requestlocclick();
}
});
//地图初始化
mmapview = (mylocationmapview)findviewbyid(r.id.bmapview);
mmapview.settraffic(true);//设置地图模式为交通视图(也可为卫星视图) 
mmapcontroller = mmapview.getcontroller();
mmapview.getcontroller().setzoom(15);
mmapview.getcontroller().enableclick(true);
mmapview.setbuiltinzoomcontrols(true);
//信息检索初始化
mmksearch = new mksearch(); 
mmksearch.init(app.mbmapmanager, new mksearchlistener() {
@override
public void ongetaddrresult(mkaddrinfo arg0, int arg1) {
}
@override
public void ongetbusdetailresult(mkbuslineresult arg0, int arg1) {
}
@override
public void ongetdrivingrouteresult(mkdrivingrouteresult arg0,
int arg1) {
}
@override
public void ongetpoidetailsearchresult(int arg0, int arg1) {
}
@override
public void ongetpoiresult(mkpoiresult res, int type, int error) {
if (error == mkevent.error_result_not_found) {
toast.maketext(mainactivity.this, "抱歉,未找到结果",
toast.length_long).show();
return;
} else if (error != 0 || res == null) {
toast.maketext(mainactivity.this, "搜索出错啦.."+error,
toast.length_long).show();
return;
}
poioverlay poioverlay = new poioverlay(mainactivity.this,
mmapview);
poioverlay.setdata(res.getallpoi());
mmapview.getoverlays().clear();
mmapview.getoverlays().add(poioverlay);
mmapview.refresh();
for (mkpoiinfo info : res.getallpoi()) {
if (info.pt != null) {
mmapview.getcontroller().animateto(info.pt);
break;
}
}
}
@override
public void ongetshareurlresult(mkshareurlresult arg0, int arg1,
int arg2) {
}
@override
public void ongetsuggestionresult(mksuggestionresult arg0, int arg1){
}
@override
public void ongettransitrouteresult(mktransitrouteresult arg0,
int arg1) {
}
@override
public void ongetwalkingrouteresult(mkwalkingrouteresult arg0,
int arg1) {
}
});
createpaopao();
//定位初始化
mlocclient = new locationclient( this );
locdata = new locationdata();
mlocclient.registerlocationlistener( mylistener );
locationclientoption option = new locationclientoption();
option.setopengps(true);//打开gps
option.setaddrtype("all");//返回的定位结果包含地址信息
option.disablecache(false);//禁止启用缓存定位
option.setcoortype("bd09ll"); //设置坐标类型
option.setscanspan(1000);
mlocclient.setlocoption(option);
mlocclient.start();
//定位图层初始化
mylocationoverlay = new locationoverlay(mmapview);
//设置定位数据
mylocationoverlay.setdata(locdata);
//添加定位图层
mmapview.getoverlays().add(mylocationoverlay);
mylocationoverlay.enablecompass();
//修改定位数据后刷新图层生效
mmapview.refresh();
}
/**
* 手动触发一次定位请求
*/
public void requestlocclick(){
isrequest = true;
mlocclient.requestlocation();
toast.maketext(mainactivity.this, "正在定位……", toast.length_short).show();
}
/**
* 创建弹出泡泡图层
*/
public void createpaopao(){
viewcache = getlayoutinflater().inflate(r.layout.custom_text_view, null);
popuptext =(textview) viewcache.findviewbyid(r.id.textcache);
//泡泡点击响应回调
popupclicklistener poplistener = new popupclicklistener(){
@override
public void onclickedpopup(int index) {
}
};
pop = new popupoverlay(mmapview,poplistener);
mylocationmapview.pop = pop;
}
/**
* 定位sdk监听函数
*/
public class mylocationlistenner implements bdlocationlistener {
@override
public void onreceivelocation(bdlocation location) {
if (location == null)
return ;
locdata.latitude = location.getlatitude();
locdata.longitude = location.getlongitude();
//如果不显示定位精度圈,将accuracy赋值为0即可
locdata.accuracy = location.getradius();
// 此处可以设置 locdata的方向信息, 如果定位 sdk 未返回方向信息,用户可以自己实现罗盘功能添加方向信息。
locdata.direction = location.getderect();
//更新定位数据
mylocationoverlay.setdata(locdata);
//更新图层数据执行刷新后生效
mmapview.refresh();
//是手动触发请求或首次定位时,移动到定位点
if (isrequest || isfirstloc){
//移动地图到定位点
//log.d("locationoverlay", "receive location, animate to it");
mmapcontroller.animateto(new geopoint((int)(locdata.latitude* 1e6), (int)(locdata.longitude * 1e6)));
isrequest = false;
mylocationoverlay.setlocationmode(locationmode.following);
}
//首次定位完成
isfirstloc = false;
}
public void onreceivepoi(bdlocation poilocation) {
if (poilocation == null){
return ;
}
}
}
//继承mylocationoverlay重写dispatchtap实现点击处理
public class locationoverlay extends mylocationoverlay{
public locationoverlay(mapview mapview) {
super(mapview);
}
@override
protected boolean dispatchtap() {
//处理点击事件,弹出泡泡
popuptext.setbackgroundresource(r.drawable.popup);
popuptext.settext(mlocclient.getlastknownlocation().getaddrstr());
pop.showpopup(bmaputil.getbitmapfromview(popuptext),new geopoint((int)(locdata.latitude*1e6), (int)(locdata.longitude*1e6)),8);
return true;
}
}
@override
protected void onpause() {
mmapview.onpause();
if(app.mbmapmanager!=null){ 
app.mbmapmanager.stop(); 
} 
super.onpause();
}
@override
protected void onresume() {
mmapview.onresume();
if(app.mbmapmanager!=null){ 
app.mbmapmanager.start(); 
}
super.onresume();
}
@override
protected void ondestroy() {
//退出时销毁定位
if (mlocclient != null)
mlocclient.stop();
mmapview.destroy();
if(app.mbmapmanager!=null){ 
app.mbmapmanager.destroy(); 
app.mbmapmanager=null; 
} 
super.ondestroy();
}
@override
protected void onsaveinstancestate(bundle outstate) {
super.onsaveinstancestate(outstate);
mmapview.onsaveinstancestate(outstate); 
}
@override
protected void onrestoreinstancestate(bundle savedinstancestate) {
super.onrestoreinstancestate(savedinstancestate);
mmapview.onrestoreinstancestate(savedinstancestate);
}
@override
public boolean oncreateoptionsmenu(menu menu) {
return true;
}
}

mainactivity的布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<framelayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.jerehedu.ljb.mylocationmapview
android:id="@+id/bmapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
<relativelayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginleft="10dp"
android:layout_marginright="10dp"
android:layout_margintop="10dp"
android:background="@drawable/edit_bg_all" >
<edittext
android:id="@+id/txtaddr"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_centervertical="true"
android:layout_marginright="2dp"
android:layout_toleftof="@+id/btok"
android:background="@drawable/edit_bg_all"
android:completionthreshold="2"
android:drawableleft="@drawable/qz_icon_seabar_search"
android:hint="请输入搜索关键字" />
<textview
android:id="@+id/btok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centervertical="true"
android:layout_marginright="2dp"
android:layout_toleftof="@+id/s2"
android:text="搜索"
android:textsize="18sp" />
<imageview
android:id="@+id/s2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centervertical="true"
android:layout_marginright="2dp"
android:layout_toleftof="@+id/btget"
android:src="@drawable/slide_center" />
<textview
android:id="@+id/btget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignparentright="true"
android:layout_centervertical="true"
android:text="定位"
android:textsize="18sp" />
</relativelayout>
</framelayout>
</relativelayout>

通过以上内容给大家分享了android百度地图实现搜索和定位及自定义图标绘制并点击时弹出泡泡的相关知识,希望对大家有所帮助。