百度地图手动定位 |
您所在的位置:网站首页 › 百度地图如何定位当前位置 › 百度地图手动定位 |
public BMapManager mapManager = null; static MapApplication app; public String mStrKey = "你申请的key值"; @Override public void onCreate() { mapManager = new BMapManager(this); mapManager.init(mStrKey, new MyGeneralListener()); } @Override //建议在您app的退出之前调用mapadpi的destroy()函数,避免重复初始化带来的时间消耗 public void onTerminate() { // TODO Auto-generated method stub if(mapManager != null) { mapManager.destroy(); mapManager = null; } super.onTerminate(); } static class MyGeneralListener implements MKGeneralListener{ @Override public void onGetNetworkState(int arg0) { Toast.makeText(MapApplication.app.getApplicationContext(), "您的网络出错啦!", Toast.LENGTH_LONG).show(); } @Override public void onGetPermissionState(int iError) { if (iError == MKEvent.ERROR_PERMISSION_DENIED) { // 授权Key错误: Toast.makeText(MapApplication.app.getApplicationContext(),"您的授权Key不正确!", Toast.LENGTH_LONG).show(); } } } 5、接下来就是按照百度api写定位代码了,使用handler机制去添加定位图层,需要说明的都在注释上了。 private BMapManager mBMapMan = null; private MapView mMapView = null; private MapController bMapController; private MKLocationManager mkLocationManager; private MKSearch mkSearch; private TextView address_view; //定位到的位置信息 private ProgressDialog dialog; private List hotelList; private int distance = 1000; //查询的范围(单位:m) Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { double lat = msg.getData().getDouble("lat"); double lon = msg.getData().getDouble("lon"); if(lat!=0&&lon!=0){ GeoPoint point = new GeoPoint( (int) (lat * 1E6), (int) (lon * 1E6)); bMapController.animateTo(point); //设置地图中心点 bMapController.setZoom(15); mkSearch.reverseGeocode(point); //解析地址(异步方法) MyLocationOverlay myLoc = new MyLocationOverlayFromMap(ShowMapAct.this,mMapView); myLoc.enableMyLocation(); // 启用定位 myLoc.enableCompass(); // 启用指南针 mMapView.getOverlays().add(myLoc); }else{ Toast.makeText(ShowMapAct.this, "没有加载到您的位置", Toast.LENGTH_LONG).show(); } if(hotelList!=null){ Drawable marker = getResources().getDrawable(R.drawable.iconmarka); //设置marker marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight()); //为maker定义位置和边界 mMapView.getOverlays().add(new OverItemList(marker,hotelList,ShowMapAct.this,bMapController)); }else if(hotelList==null&&lat!=0&&lon!=0){ Toast.makeText(ShowMapAct.this, "网络异常,没有获取到酒店信息。", Toast.LENGTH_LONG).show(); } if(dialog!=null) dialog.dismiss(); } }; @Override protected void onCreate(Bundle savedInstanceState) { distance = getIntent().getExtras().getInt("distance"); //获取查询范围 super.onCreate(savedInstanceState); setContentView(R.layout.location); mMapView = (MapView)findViewById(R.id.bmapsView); //初始化一个mapView 存放Map init(); //初始化地图管理器 super.initMapActivity(mBMapMan); address_view = (TextView)findViewById(R.id.myLocation_id); SpannableStringBuilder style = new SpannableStringBuilder(String.format(getResources().getString(R.string.location_text),"位置不详")); style.setSpan(new ForegroundColorSpan(Color.RED), 5, style.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); address_view.setText(style); Button location_button = (Button)findViewById(R.id.location_button_id); location_button.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { dialog = ProgressDialog.show(ShowMapAct.this, "", "数据加载中,请稍后....."); new Thread(new MyThread()).start(); } }); mkSearch = new MKSearch(); //初始化一个MKSearch,根据location解析详细地址 mkSearch.init(mBMapMan, this); mMapView.setBuiltInZoomControls(true); //启用内置的缩放控件 bMapController = mMapView.getController(); GeoPoint defaultPoint = new GeoPoint((int) (39.920934 * 1E6),(int) (116.412817 * 1E6)); //用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6) bMapController.setCenter(defaultPoint); //设置地图中心点 bMapController.setZoom(12); //设置地图zoom级别 mkLocationManager = mBMapMan.getLocationManager(); } /** * 初始化地图管理器BMapManager */ public void init(){ MapApplication app = (MapApplication)getApplication(); if (app.mapManager == null) { app.mapManager = new BMapManager(getApplication()); app.mapManager.init(app.mStrKey, new MapApplication.MyGeneralListener()); } mBMapMan = app.mapManager; } @Override protected void onDestroy() { MapApplication app = (MapApplication)getApplication(); if (mBMapMan != null) { mBMapMan.destroy(); app.mapManager.destroy(); app.mapManager = null; mBMapMan = null; } super.onDestroy(); } @Override protected void onPause() { if (mBMapMan != null) { // 终止百度地图API mBMapMan.stop(); } super.onPause(); } @Override protected void onResume() { if (mBMapMan != null) { // 开启百度地图API mBMapMan.start(); } super.onResume(); } @Override protected boolean isRouteDisplayed() { return false; } @Override public void onGetAddrResult(MKAddrInfo result, int iError) { if(result==null) return; SpannableStringBuilder style = new SpannableStringBuilder(String.format(getResources().getString(R.string.location_text),result.strAddr)); style.setSpan(new ForegroundColorSpan(Color.RED), 5, style.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); address_view.setText(style); if(dialog!=null) dialog.dismiss(); } @Override public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {} @Override public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {} @Override public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {} @Override public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {} /** * 重新定位,加载数据 * @author Administrator * */ class MyThread implements Runnable{ @Override public void run() { /** * 最重要的就是这个玩意 * 由于LocationListener获取第一个位置修正的时间会很长,为了避免用户等待, * 在LocationListener获取第一个更精确的位置之前,应当使用getLocationInfo() 获取一个缓存的位置 */ Location location = mkLocationManager.getLocationInfo(); double lat = 0d,lon = 0d; if(location!=null){ //定位到位置 String coordinate = location.getLatitude()+","+location.getLongitude(); HotelRemoteData hotelData = new HotelRemoteData(); /** * 远程获取酒店列表数据 */ hotelList = hotelData.getHotelToMap(coordinate,distance); lat = location.getLatitude(); lon = location.getLongitude(); } Message msg = new Message(); Bundle data = new Bundle(); data.putDouble("lat", lat); data.putDouble("lon", lon); msg.setData(data); handler.sendMessage(msg); } } |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |