解决使用百度地图默认定位是北京的问题

来源:互联网 发布:苏宁易购抢卷软件 编辑:程序博客网 时间:2024/04/28 21:13

这个大家应该是经常想要解决的问题,因为我肯定是加载当前的位置啊,特别是在网突然断,或者查找失败的时候,他就是北京位置,这个很烦,后来发现百度官方给的demo里面LocationDemo 那里面说的很清楚   直接拿过来用就行了,这里我用官方给的  做公交来说明我用了哪些代码解决加载当前的位置问题,其他的依法行事即可




/**
 * 此demo用来展示如何进行公交线路详情检索,并使用RouteOverlay在地图上绘制 同时展示如何浏览路线节点并弹出泡泡
 */
public class BusLineSearchDemo extends FragmentActivity implements
OnGetPoiSearchResultListener, OnGetBusLineSearchResultListener,
BaiduMap.OnMapClickListener {
private Button mBtnPre = null;// 上一个节点
private Button mBtnNext = null;// 下一个节点
private int nodeIndex = -2;// 节点索引,供浏览节点时使用
private BusLineResult route = null;// 保存驾车/步行路线数据的变量,供浏览节点时使用
private List<String> busLineIDList = null;
private int busLineIndex = 0;
// 搜索相关
private PoiSearch mSearch = null; // 搜索模块,也可去掉地图模块独立使用
private BusLineSearch mBusLineSearch = null;
private BaiduMap mBaiduMap = null;
BusLineOverlay overlay;//公交路线绘制对象


LocationClient mLocClient;//定位当前的位置
public MyLocationListenner myListener = new MyLocationListenner();
boolean isFirstLoc = true;// 是否首次定位



protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_busline);
MyApplication.getInstance().addActivity(this);
CharSequence titleLable = "公交线路查询功能";
setTitle(titleLable);
mBtnPre = (Button) findViewById(R.id.pre);
mBtnNext = (Button) findViewById(R.id.next);
mBtnPre.setVisibility(View.INVISIBLE);
mBtnNext.setVisibility(View.INVISIBLE);


mBaiduMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.bmapView)).getBaiduMap();
//mBaiduMap.setOnMapClickListener(this);
//这里需要定位用户当前的地址  开启定位图层
mBaiduMap.setMyLocationEnabled(true);
// 定位初始化
mLocClient  =new LocationClient(this);
mLocClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);
option.setAddrType("all");
mLocClient.setLocOption(option);
mLocClient.start();


mSearch = PoiSearch.newInstance();
mSearch.setOnGetPoiSearchResultListener(this);
mBusLineSearch = BusLineSearch.newInstance();
mBusLineSearch.setOnGetBusLineSearchResultListener(this);
busLineIDList = new ArrayList<String>();
overlay = new BusLineOverlay(mBaiduMap);
mBaiduMap.setOnMarkerClickListener(overlay);
}
/**
* 定位SDK监听函数
*/
public class MyLocationListenner implements BDLocationListener {


@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null)
return;
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(100).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
if (isFirstLoc) {
isFirstLoc = false;
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(u);
}
}


public void onReceivePoi(BDLocation poiLocation) {
}
}




/**
* 发起检索

* @param v
*/
public void searchButtonProcess(View v) {
busLineIDList.clear();
busLineIndex = 0;
mBtnPre.setVisibility(View.INVISIBLE);
mBtnNext.setVisibility(View.INVISIBLE);
EditText editCity = (EditText) findViewById(R.id.city);
EditText editSearchKey = (EditText) findViewById(R.id.searchkey);
// 发起poi检索,从得到所有poi中找到公交线路类型的poi,再使用该poi的uid进行公交详情搜索
mSearch.searchInCity((new PoiCitySearchOption()).city(
editCity.getText().toString()).keyword(
editSearchKey.getText().toString()));
}


public void SearchNextBusline(View v) {
if (busLineIndex >= busLineIDList.size()) {
busLineIndex = 0;
}
if (busLineIndex >= 0 && busLineIndex < busLineIDList.size()
&& busLineIDList.size() > 0) {
mBusLineSearch.searchBusLine((new BusLineSearchOption()
.city(((EditText) findViewById(R.id.city)).getText()
.toString()).uid(busLineIDList.get(busLineIndex))));


busLineIndex++;
}


}


/**
* 节点浏览示例

* @param v
*/
public void nodeClick(View v) {


if (nodeIndex < -1 || route == null
|| nodeIndex >= route.getStations().size())
return;
TextView popupText = new TextView(this);
popupText.setBackgroundResource(R.drawable.popup);
popupText.setTextColor(0xff000000);
// 上一个节点
if (mBtnPre.equals(v) && nodeIndex > 0) {
// 索引减
nodeIndex--;
}
// 下一个节点
if (mBtnNext.equals(v) && nodeIndex < (route.getStations().size() - 1)) {
// 索引加
nodeIndex++;
}
if(nodeIndex >= 0){
// 移动到指定索引的坐标
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(route
.getStations().get(nodeIndex).getLocation()));
// 弹出泡泡
popupText.setText(route.getStations().get(nodeIndex).getTitle());
mBaiduMap.showInfoWindow(new InfoWindow(popupText, route.getStations()
.get(nodeIndex).getLocation(), 0));
}
}


@Override
protected void onPause() {
super.onPause();
}


@Override
protected void onResume() {
super.onResume();
}


@Override
protected void onDestroy() {
// 退出时销毁定位
mLocClient.stop();

mSearch.destroy();
mBusLineSearch.destroy();
super.onDestroy();
}


@Override
public void onGetBusLineResult(BusLineResult result) {
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(BusLineSearchDemo.this, "抱歉,未找到结果",Toast.LENGTH_LONG).show();
return;
}
mBaiduMap.clear();
route = result;
nodeIndex = -1;
overlay.removeFromMap();
overlay.setData(result);
overlay.addToMap();
overlay.zoomToSpan();
mBtnPre.setVisibility(View.VISIBLE);
mBtnNext.setVisibility(View.VISIBLE);
Toast.makeText(BusLineSearchDemo.this, result.getBusLineName(),Toast.LENGTH_SHORT).show();
}


@Override
public void onGetPoiResult(PoiResult result) {


if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(BusLineSearchDemo.this, "抱歉,未找到结果",Toast.LENGTH_LONG).show();
return;
}
// 遍历所有poi,找到类型为公交线路的poi
busLineIDList.clear();
for (PoiInfo poi : result.getAllPoi()) {
if (poi.type == PoiInfo.POITYPE.BUS_LINE
|| poi.type == PoiInfo.POITYPE.SUBWAY_LINE) {
busLineIDList.add(poi.uid);
}
}
SearchNextBusline(null);
route = null;
}


@Override
public void onGetPoiDetailResult(PoiDetailResult result) {


}


@Override
public void onMapClick(LatLng point) {
mBaiduMap.hideInfoWindow();
}


@Override
public boolean onMapPoiClick(MapPoi poi) {
return false;
}
}


其中红色的部分代码是我添加的,绿色的部分代码是我隐藏掉的,就完了,解决问题了

1 0
原创粉丝点击