百度地图安卓搜索服务开发

 百度api android SDK版的检索服务这一节的(检索SDK),个人认为如果按照它源码可能会出一下小问题,不知道大家遇到过没有,他主要的问题是在新建MySearchListener类的时候,方法中引用MyMapActivity这个类的时候会报错,而源码中并没有经过处理;这是因为内部类和外部类的相互间不能直接引用需要经过处理。修改后的源码如下:

一.MainActivity类

package com.example.maptobaidu;
import android.app.Activity;   
import android.os.Bundle;       
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.baidu.mapapi.BMapManager;   
import com.baidu.mapapi.map.MapController;   
import com.baidu.mapapi.map.MapView;  
import com.baidu.mapapi.search.MKSearch;
import com.baidu.platform.comapi.basestruct.GeoPoint;  
public  class MainActivity extends Activity {
 
	BMapManager mBMapMan = null;  
	MapView mMapView = null;  
	MKSearch mMKSearch = null;//用于信息检索服务
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);  
    	mBMapMan=new BMapManager(getApplication());  
    	mBMapMan.init("093FE924BA2C0875B81B88CDA1B67CE30D5C9E31", null);    
    	//注意:请在试用setContentView前初始化BMapManager对象,否则会报错  
    	setContentView(R.layout.activity_main);  
    	mMapView=(MapView)findViewById(R.id.bmapsView);  
    	mMapView.setBuiltInZoomControls(true);  
    	//设置启用内置的缩放控件  
    	MapController mMapController=mMapView.getController();  
    	mMapView.setTraffic(true); 
    	mMapView.setSatellite(true);  
    	// 得到mMapView的控制权,可以用它控制和驱动平移和缩放  
    	GeoPoint point =new GeoPoint((int)(39.915* 1E6),(int)(116.404* 1E6));  
    	//用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6)  
    	mMapController.setCenter(point);//设置地图中心点  
    	mMapController.setZoom(12);//设置地图zoom级别  
    	//信息检索初始化
    	mMKSearch = new MKSearch();  
    	mMKSearch.init(mBMapMan, new MySearchListener(MainActivity.this));//注意,MKSearchListener只支持一个,以最后一次设置为准 
    	final EditText txtCity=(EditText)findViewById(R.id.txtCity);
    	final EditText txtAddr=(EditText)findViewById(R.id.txtAddr);
    	Button btOk=(Button)findViewById(R.id.btOk);
    	btOk.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				mMKSearch.poiSearchInCity(txtCity.getText().toString(), txtAddr.getText().toString());  	
			}
		});
    	
    }
    //传递方法
    public void  mySearchListener()
    {
    	 MySearchListener activity = new  MySearchListener(MainActivity.this);
    }
    
    @Override  
    protected void onDestroy(){  
            mMapView.destroy();  
            if(mBMapMan!=null){  
                    mBMapMan.destroy();  
                    mBMapMan=null;  
            }  
            super.onDestroy();  
    }  
    @Override  
    protected void onPause(){  
            mMapView.onPause();  
            if(mBMapMan!=null){  
                   mBMapMan.stop();  
            }  
            super.onPause();  
    }  
    @Override  
    protected void onResume(){  
            mMapView.onResume();  
            if(mBMapMan!=null){  
                    mBMapMan.start();  
            }  
           super.onResume();  
    }  
}



MySearchListener类

package com.example.maptobaidu;

import android.widget.Toast;

import com.baidu.mapapi.map.MKEvent;
import com.baidu.mapapi.map.PoiOverlay;
import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPoiInfo;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult; 
public class MySearchListener implements MKSearchListener{
	
	//由于是内部类需要通过函数进行传递MainActivity,才能够进行引用
    private MainActivity activity=null;
    public MySearchListener(MainActivity activity)
    {
       this.activity = activity;
    }
	public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	public void onGetPoiDetailSearchResult(int arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	public void onGetPoiResult(MKPoiResult res, int type, int error) {
		if(error==MKEvent.ERROR_RESULT_NOT_FOUND){
			Toast.makeText(activity, "抱歉,未找到结果",Toast.LENGTH_LONG).show();
			return;
		}
		else 
			if(error != 0 || res == null){
			Toast.makeText(activity, "搜索出错啦..", Toast.LENGTH_LONG).show(); 
			return;
		}
	   PoiOverlay poiOverlay = new PoiOverlay(activity, activity.mMapView); 
	   poiOverlay.setData(res.getAllPoi());  
activity.mMapView.getOverlays().clear();
	   activity.mMapView.getOverlays().add(poiOverlay);
	   activity.mMapView.refresh();
	   for(MKPoiInfo info: res.getAllPoi()){
		   if(info.pt!=null){
			   activity.mMapView.getController().animateTo(info.pt);
			   break;
		   }
	   }
	}


	public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}


	public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}


	public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}


}

其效果图如下:






                                    
发布了16 篇原创文章 · 获赞 23 · 访问量 4万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览