当前位置:编程学习 > wap >>

android百度地图搜索功能实现

程序现在存在的问题:执行时程序十分不稳定,总共分三种情况 不知道为什么会这样,求解
1 程序成功运行,代码146行的walkingSearch函数执行的返回值为0,并且回调函数onGetWalkingRouteResult(125行)成功,程序正确执行。
2 程序运行失败 ,代码146行的walkingSearch函数执行的返回值为0,但是并没有执行回调函数onGetWalkingRouteResult(125行)
3 程序运行失败,代码146行的walkingSearch函数执行的返回值为-1。

主要程序代码

package com.realpathsdkdemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.RouteOverlay;
import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPlanNode;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKShareUrlResult;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult;
import com.baidu.platform.comapi.basestruct.GeoPoint;

public class MainActivity extends Activity {
BMapManager mBMapMan = null;   
MapView mMapView = null;
MKSearch mMKSearch = null;
private Handler mHandler = null;
MKPlanNode start = null;
MKPlanNode end = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBMapMan=new BMapManager(getApplication()); 
mBMapMan.init("LZVlc4DD9hoSjBxp7W243thR", null);  //前面的一个String对象为要替换的key   
//注意:请在试用setContentView前初始化BMapManager对象
setContentView(R.layout.activity_main);
mMapView=(MapView)findViewById(R.id.bmapsView);   
mMapView.setBuiltInZoomControls(true);   
//设置启用内置的缩放控件   
MapController mMapController=mMapView.getController();   

 // 上海市的GPS纬度经度值:31.243319,121.509075 
      GeoPoint geoPoint = new GeoPoint((int) (31.243319 * 1E6),(int) (121.509075 * 1E6)); 
      mMapController.setCenter(geoPoint); 
      mMapController.setZoom(12); 


        start = new MKPlanNode(); 
       start.pt = new GeoPoint((int) (31.205889 * 1E6), (int) (121.637942 * 1E6)); 

// 上海市陆家嘴的GPS纬度经度值: 121.509075,31.243319  
 end = new MKPlanNode(); 
end.pt = new GeoPoint( (int) (31.243319 * 1E6), (int) (121.509075 * 1E6));

mMKSearch = new MKSearch(); 
Log.d("Tag","MKSearchw");
mMKSearch.init(mBMapMan, new MKSearchListener(){

@Override
public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
// TODO Auto-generated method stub

}

@Override
public void onGetBusDetailResult(MKBusLineResult res, int error) {
// TODO Auto-generated method stub
}

@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult res,
int arg1) {
// TODO Auto-generated method stub
  if (res == null) {   
                return;   
        }   
            RouteOverlay routeOverlay = new RouteOverlay(MainActivity.this, mMapView);
            // 两点的驾车路线会有多条,采用第一种方案
            routeOverlay.setData(res.getPlan(0).getRoute(0));
            mMapView.getOverlays().add(routeOverlay);
            mMapView.invalidate();
}

@Override
public void onGetPoiDetailSearchResult(int 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 onGetShareUrlResult(MKShareUrlResult arg0, int arg1,
int arg2) {
// TODO Auto-generated method stub

}

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

}

@Override
public void onGetTransitRouteResult(MKTransitRouteResult res,
int arg1) {
// TODO Auto-generated method stub
 RouteOverlay routeOverlay = new RouteOverlay( MainActivity.this, mMapView); 
           routeOverlay.setData(res.getPlan(0).getRoute(0)); 
           mMapView.getOverlays().add(routeOverlay); 
           mMapView.refresh();// 刷新地图 
}

@Override
//回调函数
public void onGetWalkingRouteResult(MKWalkingRouteResult res,
int arg1) {
// TODO Auto-generated method stub
            RouteOverlay routeOverlay = new RouteOverlay(MainActivity.this, mMapView);
            routeOverlay.setData(res.getPlan(0).getRoute(0));
            Log.d("Tag","walkingSearch   onGetWalkingRouteResult");
            mMapView.getOverlays().add(routeOverlay);
            Log.d("Tag","walkingSearch   addOverlays");
            mMapView.refresh();
            Log.d("Tag","walkingSearch   mapViewRefresh");
}

});
mHandler = new Handler(){

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
//注意,MKSearchListener只支持一个,以最后一次设置为准 
//走路代码的实现返回值-1表示不成功,0表示成功,并执行回调函数 onGetWalkingRouteResult
int i=mMKSearch.walkingSearch(null, start, null, end);
Log.d("Tag","walkingSearch    "+i);
}

};

mHandler.sendEmptyMessageDelayed(0, 1000);//等待1s

// 设置驾车路线搜索策略,时间优先、费用最少或距离最短 
// 驾乘检索策略常量:时间优先 
//mMKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST); 
    //mMKSearch.drivingSearch(null, start, null, end); 
    // 驾乘检索策略常量:较少费用 
  //  mMKSearch.setDrivingPolicy(MKSearch.ECAR_FEE_FIRST); 
    //mMKSearch.drivingSearch(null, start, null, end); 
    // 驾乘检索策略常量:最短距离 
    //mMKSearch.setDrivingPolicy(MKSearch.ECAR_DIS_FIRST); 
   // mMKSearch.drivingSearch(null, start, null, end); 
    

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

 @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   
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(); 
       Log.d("Tag","walkingSearch  ----->  ");

}
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,