最近跟着百度地图API学地图开发,先是学了路径搜索,对于已知坐标的两点进行驾车、公交、步行三种路径的搜索(公交路径运行没效果,待学习中),后来又 学了定位功能,能够获取到自己所在位置的经纬度,但当将两者合起来先自动获取自己所在位置的经纬度然后与固定地点进行路径搜索时却弄不出来了,因为刚开始 写的两者在两个类中总是取不到经纬度值,后来将两者写到同一个类中去了,终于取到经纬度值了,也运行出来了。需要 在 BDLocationListener的onReceiveLocation里获取到经纬度值,因为已经能直接拿经纬度值了,所以就可以在里面进行路 径搜索了。萝莉啰嗦说了一些估计只有我自己能看懂。。。。
总结一下自己实现的过程(只说主要的):
1.初始化变量
2.在setContentView的前面初始化BMapManager.
3.得到MapController,确定一个点将mapview的中心设在那,(此步不可少),若缺少mapview会显示到默认的地址(貌似是北京那块地)影响后面起始位置路径的搜索。
4.生成LoctionClient,使用LocationClientOption对其进行简单的设置。调用locationClient.start();
locationClient.requestLocation();进行定位获取位置,使用回调给LocatiopnClient注册监听器,在回调方 法onReceiveLocation获得经纬度,使用MKPlanNode设置起始位置点(已经获取到的经纬度)与终点,就可以初始化 MKSearch,复写其参数监听器中的回调方法onGetWalkingRouteResult、onGetTransitRouteResult、 onGetDrivingRouteResult,最后mkSearch.walkingSearch(null, satrt, null, end);
//mkSearch.drivingSearch(null, satrt, null, end);
//mkSearch.transitSearch("上海", satrt, end);(运行不出,待学习)
就可以进行路线搜索了。
代码如下
package com.example.lockposi;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MKEvent;
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.MKPoiInfo;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKSearchListener;
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;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView textView;
LocationClient locationClient;
double latitude,longitude;
private MapView mapview;
BMapManager bMapManager;
static final String API_KEY="07418AEC69BAAB7104C6230A6120C580DFFAEEBB";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bMapManager=new BMapManager(getApplicationContext());
bMapManager.init(API_KEY,new MKGeneralListener() {
@Override
public void onGetPermissionState(int arg0) {
// TODO Auto-generated method stub
if(arg0==MKEvent.ERROR_PERMISSION_DENIED){
Toast.makeText(getApplicationContext(), "请输入正确的key",1000).show();
}
}
@Override
public void onGetNetworkState(int arg0) {
// TODO Auto-generated method stub
if (arg0==MKEvent.ERROR_NETWORK_CONNECT) {
Toast.makeText(getApplicationContext(), "您的网络出错了",1000).show();
}
}
});
setContentView(R.layout.activity_main);
mapview = (MapView) this.findViewById(R.id.bmapView);
mapview.setBuiltInZoomControls(true);
MapController mMapController = mapview.getController();
GeoPoint geoPoint = new GeoPoint((int) (31.243319 * 1E6),
(int) (121.509075 * 1E6));
mMapController.setCenter(geoPoint);
mMapController.setZoom(12);
textView=(TextView) findViewById(R.id.textView1);
locationClient=new LocationClient(getApplicationContext());
LocationClientOption locationClientOption=new LocationClientOption();
locationClientOption.setOpenGps(true);
locationClientOption.setCoorType("bd09ll");
locationClientOption.setPriority(LocationClientOption.NetWorkFirst);
locationClientOption.setScanSpan(5000);
locationClient.setLocOption(locationClientOption);
locationClient.start();
locationClient.requestLocation();
Toast.makeText(getApplicationContext(), String.valueOf(latitude),1000).show();
locationClient.registerLocationListener(new BDLocationListener() {
@Override
public void onReceivePoi(BDLocation arg0) {
// TODO Auto-generated method stub
if (arg0==null) {
return;
}
latitude=arg0.getLatitude();
longitude=arg0.getLongitude();
StringBuilder sBuilder=new StringBuilder();
sBuilder.append("latitude:"+String.valueOf(arg0.getLatitude())).append("longitude:"+String.valueOf(arg0.getLongitude()));
textView.setText(sBuilder.toString());
}
@Override
public void onReceiveLocation(BDLocation arg0) {
// TODO Auto-generated method stub
if (arg0==null) {
return;
}
latitude=arg0.getLatitude();
longitude=arg0.getLongitude();
StringBuilder sBuilder=new StringBuilder();
sBuilder.append("latitude:"+arg0.getLatitude()).append("longitude:"+arg0.getLongitude());
textView.setText(sBuilder.toString());
MKPlanNode satrt=new MKPlanNode();
satrt.pt=new GeoPoint((int) (31.205889 * 1E6),
(int) (121.637942 * 1E6));
MKPlanNode end=new MKPlanNode();
end.pt=new GeoPoint((int) (31.243319 * 1E6), (int) (121.509075 * 1E6));
final MKSearch mkSearch=new MKSearch();
mkSearch.init(bMapManager, new MKSearchListener() {
private int intidx;
@Override
public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
if (arg0==null) {
return;
}
RouteOverlay routeOverlay=new RouteOverlay(MainActivity.this, mapview);
routeOverlay.setData(arg0.getPlan(0).getRoute(0));
mapview.getOverlays().add(routeOverlay);
mapview.refresh();
}
@Override
public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
if (arg0==null) {
return;
}
RouteOverlay routeOverlay=new RouteOverlay(MainActivity.this, mapview);
routeOverlay.setData(arg0.getPlan(0).getRoute(0));
mapview.getOverlays().add(routeOverlay);
mapview.refresh();
}
@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
if (arg2 != 0 || arg0 == null) {
Toast.makeText(MainActivity.this, "抱歉,未找到结果", Toast.LENGTH_LONG).show();
return;
}
// 找到公交路线poi node
MKPoiInfo curPoi = null;
int totalPoiNum = arg0.getNumPois();
for(intidx = 0; intidx<totalPoiNum; intidx++ ) {
curPoi = arg0.getPoi(intidx);
if ( 2 == curPoi.ePoiType ) {
break;
}
}
mkSearch.busLineSearch("上海", curPoi.uid);
}
@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
if (arg0==null) {
return;
}
RouteOverlay routeOverlay=new RouteOverlay(MainActivity.this, mapview);
routeOverlay.setData(arg0.getPlan(0).getRoute(0));
mapview.getOverlays().add(routeOverlay);
mapview.refresh();
}
@Override
public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
// TODO Auto-generated method stub
if (arg1 != 0 || arg0 == null) {
Toast.makeText(MainActivity.this, "抱歉,未找到结果", Toast.LENGTH_LONG).show();
return;
}
RouteOverlay routeOverlay = new RouteOverlay(MainActivity.this, mapview); // 此处仅展示一个方案作为示例
routeOverlay.setData(arg0.getBusRoute());
mapview.getOverlays().clear();
mapview.getOverlays().add(routeOverlay);
mapview.refresh();
mapview.getController().animateTo(arg0.getBusRoute().getStart());
}
@Override
public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
// TODO Auto-generated method stub
}
});
mkSearch.walkingSearch(null, satrt, null, end);
//mkSearch.drivingSearch(null, satrt, null, end);
//mkSearch.transitSearch("上海", satrt, end);
}
});
}
}