前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >解决使用百度地图默认定位是北京的问题

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

作者头像
wust小吴
发布2019-07-08 16:27:53
2.1K0
发布2019-07-08 16:27:53
举报
文章被收录于专栏:风吹杨柳风吹杨柳

这个大家应该是经常想要解决的问题,因为我肯定是加载当前的位置啊,特别是在网突然断,或者查找失败的时候,他就是北京位置,这个很烦,后来发现百度官方给的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; } }

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年11月09日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档