百度地图

来源:互联网 发布:美工对电商的意义 编辑:程序博客网 时间:2024/05/22 03:26
public class MainActivity extends Activity {    MapView mMapView;    BaiduMap bMapManager;    Button mButton;    Button mButton1;    Button mButton2;    Button mButton3;    //定位相关的类             d    private LocationClient locationClient;    private  MyLocationListenter mLocationListenter;    private boolean isReceive = true;    private double mLatitude;   // 唯獨    private double mLongtitude;        //經度    //自定义图标3    private BitmapDescriptor mIconLocation;//定义图标    private  MyOrientationListenter myOrientationListenter;    private float mCurrentX;//记录当前的位置    // 定位模式    private MyLocationConfiguration.LocationMode locationMode;    //覆盖物相关的类    private  BitmapDescriptor mMarker;    private RelativeLayout mMarkerLy;    @Override    protected void onCreate(Bundle savedInstanceState) {        SDKInitializer.initialize(getApplicationContext());        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        initLaoction();        //覆盖物相关        initMarker();        bMapManager.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {            @Override            public boolean onMarkerClick(Marker marker) {                Bundle extreaInfo = marker.getExtraInfo();                Info info = (Info) extreaInfo.getSerializable("info");                if(mMarkerLy!=null){                    ImageView iv = (ImageView) mMarkerLy.findViewById(R.id.markerim);                    TextView name = (TextView) mMarkerLy.findViewById(R.id.tv_info_name);                    TextView distance = (TextView) mMarkerLy.findViewById(R.id.tv_info_dis);                    TextView zan = (TextView) mMarkerLy.findViewById(R.id.tv_info_zan);                    iv.setImageResource(info.getImgId());                    distance.setText(info.getDistance());                    name.setText(info.getName());                    zan.setText(info.getZan()+"");                }                mMarkerLy.setVisibility(View.VISIBLE);                return true;            }        });            bMapManager.setOnMapClickListener(new BaiduMap.OnMapClickListener() {                @Override                public void onMapClick(LatLng latLng) {                mMarkerLy.setVisibility(View.GONE);                }                @Override                public boolean onMapPoiClick(MapPoi mapPoi) {                    return false;                }            });    }    private void initMarker() {        mMarker = BitmapDescriptorFactory.fromResource(R.drawable.maker);        mMarkerLy = (RelativeLayout) findViewById(R.id.id_maker_ly1);    }    public void initView(){             mMapView = (MapView) findViewById(R.id.bmapView);             mButton = (Button) findViewById(R.id.satelliteBTN);             mButton1 = (Button) findViewById(R.id.trafficBTN1);             mButton2 = (Button) findViewById(R.id.trafficBTN2);             mButton3 = (Button) findViewById(R.id.trafficBTN3);             bMapManager= mMapView.getMap();             MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);             bMapManager.setMapStatus(msu);            mButton.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    LatLng ll = new LatLng(mLatitude, mLongtitude); //得到经纬度                    MapStatusUpdate msuf = MapStatusUpdateFactory.newLatLng(ll);     //设置这个位置                    bMapManager.animateMapStatus(msuf);                }            });        mButton1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                locationMode = MyLocationConfiguration.LocationMode.NORMAL;//默认模式            }        });        mButton2.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                locationMode = MyLocationConfiguration.LocationMode.COMPASS;//罗盘模式            }        });        mButton3.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {               /* locationMode = MyLocationConfiguration.LocationMode.FOLLOWING;//跟随*/                addOverlays(Info.infos);            }        });    }    /**     * 添加覆盖物 Maeker             OverlayOptions //定义图层     * @param infos     */    private void addOverlays(List<Info> infos) {        bMapManager.clear();//清楚图层        LatLng lat = null;        Marker marker1 = null;        OverlayOptions options;        for(Info info : infos){            //经纬度        lat = new LatLng(info.getLatitude(),info.getLongitude());            // 图标            options = new MarkerOptions().position(lat).icon(mMarker).zIndex(5);            marker1 = (Marker) bMapManager.addOverlay(options);            Bundle arg0 = new Bundle();            arg0.putSerializable("info",info);            marker1.setExtraInfo(arg0);    }        MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(lat);        bMapManager.setMapStatus(msu);//定位    }    public void initLaoction(){           locationMode = MyLocationConfiguration.LocationMode.NORMAL;//默认模式        locationClient = new LocationClient(this);        mLocationListenter = new MyLocationListenter();        locationClient.registerLocationListener(mLocationListenter);//对    locationClient进行注册        LocationClientOption option = new LocationClientOption();        option.setCoorType("bd09ll");   //坐标类型        option.setIsNeedAddress(true);   //得到地址        option.setOpenGps(true);//返回当前的地址        option.setScanSpan(1000);   //每隔多少秒進行請求 1秒         locationClient.setLocOption(option);  //不设置不会一直请求        mIconLocation = BitmapDescriptorFactory.fromResource(R.drawable.navi_map_gps_locked);//更换图标        // 自定义图标        myOrientationListenter = new MyOrientationListenter(getApplication());        myOrientationListenter.setmOnOrientationListenter(new MyOrientationListenter.OnOrientationListenter(){            @Override            public void OnOrientationChanged(float x) {            mCurrentX = x;            }        });    }    @Override    protected void onDestroy() {        super.onDestroy();        //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理        mMapView.onDestroy();    }    @Override    protected void onStart() {        super.onStart();        bMapManager.setMyLocationEnabled(true);//必须开启地图        if(!locationClient.isStarted()){            locationClient.start();  //定位的允许            //开启传感器            myOrientationListenter.start();        }    }    @Override    protected void onStop() {        super.onStop();          bMapManager.setMyLocationEnabled(false);//必          if(locationClient.isStarted()){                      locationClient.stop();  //定位的关闭l                      }        myOrientationListenter.stop();// 停止方向传感器    }    @Override    protected void onResume() {        super.onResume();        //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理        mMapView.onResume();    }    @Override    protected void onPause() {        super.onPause();        //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理        mMapView.onPause();    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.menu_main,menu);        return true;    }    @TargetApi(Build.VERSION_CODES.LOLLIPOP)    @Override    public boolean onOptionsItemSelected(MenuItem item) {        switch (item.getItemId()){            case R.id.id_map_common:                bMapManager.setMapType(BaiduMap.MAP_TYPE_NONE);                break;            case R.id.id_weixing:                 bMapManager.setMapType(BaiduMap.MAP_TYPE_SATELLITE);                break;            case R.id.shishijiaotong:                if(bMapManager.isTrafficEnabled()){                    bMapManager.setTrafficEnabled(false);               item.setTitle("时时交通图(off)");                }   else {                    bMapManager.setTrafficEnabled(true);                     item.setTitle("时时交通图(NO)");                }                break;        }        return super.onOptionsItemSelected(item);    }    private class MyLocationListenter implements BDLocationListener{        @Override        public void onReceiveLocation(BDLocation bdLocation) {            try {                //對   BDLocation j进行转换                MyLocationData data = new MyLocationData.Builder()                        .direction(mCurrentX)//方向和传感器的结合                        .accuracy(bdLocation.getRadius())  //读取经纬                        .latitude(bdLocation.getLatitude())    //维度                        .longitude(bdLocation.getLongitude())  //经度                        .build();      /// 处理返回的数据                        bMapManager.setMyLocationData(data);                //自定义图标                MyLocationConfiguration config= new MyLocationConfiguration(locationMode,true,mIconLocation);                bMapManager.setMyLocationConfigeration(config);                // 获取经纬度                mLatitude = bdLocation.getLatitude();                mLongtitude = bdLocation.getLongitude();                         if(isReceive){                             LatLng ll = new LatLng(bdLocation.getLatitude(),bdLocation.getLongitude()); //得到经纬度                                 MapStatusUpdate msuf = MapStatusUpdateFactory.newLatLng(ll);     //设置这个位置                             bMapManager.animateMapStatus(msuf);//定位到这个信息里面                             isReceive = false;                             Toast.makeText(getApplication(),"定位成功"+bdLocation.getAddrStr(),Toast.LENGTH_LONG).show();                         }          } catch (Exception e) {                e.printStackTrace();            }        }    }    private void satelliteBTNOnClick(View v){    }    private void trafficBTNOnClick(View v){      }    }
0 0