百度地图

来源:互联网 发布:淘宝买同款违法吗 编辑:程序博客网 时间:2024/06/01 08:24

主界面=====================================================

public class MainActivity extends ActionBarActivity {

    private MapView mMapView;
    private BaiduMap mBaiduMap;
    public LocationClient mLocationClient = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        
        mMapView = (MapView) findViewById(R.id.bmapView);  
        mBaiduMap = mMapView.getMap();  
        
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);  
        
        
        BDLocationListener myListener = new MyLocationListener(mBaiduMap);
        //普通地图  
          //
          
          mLocationClient = new LocationClient(getApplicationContext());
          initLocation();
          //声明LocationClient类
          mLocationClient.registerLocationListener( myListener );    //注册监听函数
          
          mLocationClient.start();
    }

    private void initLocation() {
         LocationClientOption option = new LocationClientOption();
            option.setLocationMode(LocationMode.Hight_Accuracy
    );//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
            option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系
            int span=1000;
            option.setScanSpan(0);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
            option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
            option.setOpenGps(true);//可选,默认false,设置是否使用gps
            option.setLocationNotify(true);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
            option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
            option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
    option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死  
            option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集
    option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要
            mLocationClient.setLocOption(option);
        
    }


}


布局============================================================

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bawie.test.MainActivity" >

    <com.baidu.mapapi.map.MapView  
    android:id="@+id/bmapView"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:clickable="true" />

</RelativeLayout>

帮助类======================================================================

public class MyLocationListener implements BDLocationListener {

     private LocationMode mCurrentMode=LocationMode.COMPASS;
        private BitmapDescriptor mCurrentMarker;
        BaiduMap mBaiduMap;
        
        public MyLocationListener(BaiduMap mBaiduMap) {
            this.mBaiduMap=mBaiduMap;
            
        }
    
    @Override
    public void onReceiveLocation(BDLocation location) {
        // TODO Auto-generated method stub

         //Receive Location
        StringBuffer sb = new StringBuffer(256);
        sb.append("time : ");
        sb.append(location.getTime());
        sb.append("\nerror code : ");
        sb.append(location.getLocType());
        sb.append("\nlatitude : ");
        sb.append(location.getLatitude());
        sb.append("\nlontitude : ");
        sb.append(location.getLongitude());
        sb.append("\nradius : ");
        sb.append(location.getRadius());
        if (location.getLocType() == BDLocation.TypeGpsLocation){// GPS定位结果
            sb.append("\nspeed : ");
            sb.append(location.getSpeed());// 单位:公里每小时
            sb.append("\nsatellite : ");
            sb.append(location.getSatelliteNumber());
            sb.append("\nheight : ");
            sb.append(location.getAltitude());// 单位:米
            sb.append("\ndirection : ");
            sb.append(location.getDirection());// 单位度
            sb.append("\naddr : ");
            sb.append(location.getAddrStr());
            sb.append("\ndescribe : ");
            sb.append("gps定位成功");

        } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){// 网络定位结果
            sb.append("\naddr : ");
            sb.append(location.getAddrStr());
            //运营商信息
            sb.append("\noperationers : ");
            sb.append(location.getOperators());
            sb.append("\ndescribe : ");
            sb.append("网络定位成功");
        }
sb.append("\nlocationdescribe : ");
            sb.append(location.getLocationDescribe());// 位置语义化信息
            List<Poi> list = location.getPoiList();// POI数据
            if (list != null) {
                sb.append("\npoilist size = : ");
                sb.append(list.size());
                for (Poi p : list) {
                    sb.append("\npoi= : ");
                    sb.append(p.getId() + " " + p.getName() + " " + p.getRank());
                }
            }
        Log.i("BaiduLocationApiDem", sb.toString());
        
        
     // 开启定位图层  
        mBaiduMap.setMyLocationEnabled(true);  
        // 构造定位数据  
        MyLocationData locData = new MyLocationData.Builder()  
            .accuracy(location.getRadius())  
            // 此处设置开发者获取到的方向信息,顺时针0-360  
            .direction(100).latitude(40.0491290000)  
            .longitude(116.3064050000).build();  
        // 设置定位数据  
        mBaiduMap.setMyLocationData(locData);  
        // 设置定位图层的配置(定位模式,是否允许方向信息,用户自定义定位图标)  
        mCurrentMarker = BitmapDescriptorFactory  
            .fromResource(R.drawable.ic_launcher);  
        MyLocationConfiguration config = new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker);  
        
        mBaiduMap.setMyLocationConfigeration(config);
        
        // 当不需要定位图层时关闭定位图层  
       // mBaiduMap.setMyLocationEnabled(false);
    }

}


导包======================================

arm64-v8a

armeabi

BaiduLBS-Android


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 微信记录全没了怎么办 微信记录没有了怎么办 ipad登录不了下不了软件怎么办 苹果手机打开自动时间不对怎么办 魅族手机时间不同步怎么办 小米手环时间不准怎么办 电脑网络dns配置错误怎么办 去泰国手机要打电话怎么办 苹果6s音量小怎么办 三星0n7充电慢怎么办 在国外接受不到验证码怎么办 ip地址错误网络无法连通怎么办 注册微信收不到验证码怎么办 安卓手机收不到短信怎么办 手机被短信验证码轰炸怎么办 美国访学一年手机卡怎么办方便 小狗吃了葡萄皮怎么办 ios迅雷下载不了的资源怎么办 ipad软件商店内容少怎么办? 脸摔伤后留下黑印怎么办 不确定孩子是不是老公的怎么办 孩子接种证丢了怎么办 孩子的出生证丢了怎么办 私秘边上肿了怎么办 书法作品少写一个字怎么办 炉石传说ios闪退怎么办 ck手表表链大了怎么办 天梭手表卡扣坏了怎么办 机械表平时不戴怎么办 天梭机械表慢了怎么办 手表每天慢10秒怎么办 浪琴机械表不走了怎么办 大提单号被修改了怎么办 入户中山没有三年居住证明怎么办 加拼关单号舱单信息没有怎么办 外贸中交货期晚了怎么办 履约保函到期了怎么办 续贷高校未通过怎么办 安卓手机网速慢怎么办 探探性别错了怎么办 尿酸高导致脚肿怎么办