android 开发(百度地图)

百度地图开放平台:http://lbsyun.baidu.com/

百度地图API:http://developer.baidu.com/map/reference/

Android地图SDK

百度地图LBS云是百度地图针对LBS开发者全新推出的平台级服务,不仅适用PC应用开发,同时适用移动设备应用的开发。使用LBS云,可以实现移动开发者存储海量位置数据的服务器零成本及维护压力,且支持高效检索用户数据,且实现地图展现。

开发者指南

eclipse数字签名:windows->perferences->android->build->SHA1 fingerprint

相关下载-〉自定义下载-〉开发包、类参考、示例代码

找到JAR包拷贝到 libs里

SO包里按开发类型选择拷贝到libs里(如:x86、armeapi)

 

集成地图SDK的应用,在打包混淆的时候,需要注意与地图SDK相关的方法不可被混淆。混淆方法如下:

-keep class com.baidu.** {*;}
-keep class vi.com.** {*;}    
-dontwarn com.baidu.**

保证百度类不能被混淆,否则会出现网络不可用等运行时异常

 

清单文件中添加

<application>  
    <meta-data  
        android:name="com.baidu.lbsapi.API_KEY"  
        android:value="开发者 key" />  
</application>


2)添加所需权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />


在布局xml文件中添加地图控件;

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


在应用程序创建时初始化 SDK引用的Context 全局变量:

public class MainActivity extends Activity {  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);   
        //在使用SDK各组件之前初始化context信息,传入ApplicationContext  
        //注意该方法要再setContentView方法之前实现  
        SDKInitializer.initialize(getApplicationContext());  
        setContentView(R.layout.activity_main);  
    }  
}

注意:在SDK各功能组件使用之前都需要调用

SDKInitializer.initialize(getApplicationContext());,因此我们建议该方法放在Application的初始化方法中

 

创建地图Activity,管理地图生命周期;

public class MainActivity extends Activity {  
    MapView mMapView = null;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);   
        //在使用SDK各组件之前初始化context信息,传入ApplicationContext  
        //注意该方法要再setContentView方法之前实现  
        SDKInitializer.initialize(getApplicationContext());  
        setContentView(R.layout.activity_main);  
        //获取地图控件引用  
        mMapView = (MapView) findViewById(R.id.bmapView);  
    }  
    @Override  
    protected void onDestroy() {  
        super.onDestroy();  
        //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理  
        mMapView.onDestroy();  
    }  
    @Override  
    protected void onResume() {  
        super.onResume();  
        //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理  
        mMapView.onResume();  
        }  
    @Override  
    protected void onPause() {  
        super.onPause();  
        //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理  
        mMapView.onPause();  
        }  
    }

完成以上步骤后,运行程序,即可显示地图。

 

GCJ-02是由中国国家测绘局制订的地理信息系统的坐标系统。
它是一种对经纬度数据的加密算法,即加入随机的偏差。
国内出版的各种地图系统(包括电子形式),必须至少采用GCJ-02对地理位置进行首次加密。
 
如果基础开发时申请了密钥,在百度定位时就不需要再申请密钥
参考百度开发文档,配置开发环境
 
如果拷贝到libs里的库文件显示图标,表示库没有加入成功,右键点击库文件->build path->add后库文件显示表示添加成功
 
   声明全局变量
public LocationClient mLocationClient = null;
public BDLocationListener myListener = new MyLocationListener();

在onCreate方法中添加
mLocationClient = new LocationClient(getApplicationContext());     //声明LocationClient类
mLocationClient.registerLocationListener( myListener );    //注册监听函数

配置定位SDK参数

private void initLocation(){
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationMode.Hight_Accuracy
);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
        option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系
        int span=1000;
        option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
        option.setOpenGps(true);//可选,默认false,设置是否使用gps
        option.setLocationNotify(true);//可选,默认false,设置是否当GPS有效时按照1S/1次频率输出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);
    }
View Code

 

实现BDLocationListener接口

BDLocationListener为结果监听接口,异步获取定位结果,实现方式如下:

public class MyLocationListener implements BDLocationListener {
 
        @Override
        public void onReceiveLocation(BDLocation location) {
            //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("网络定位成功");
            } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
                sb.append("\ndescribe : ");
                sb.append("离线定位成功,离线定位结果也是有效的");
            } else if (location.getLocType() == BDLocation.TypeServerError) {
                sb.append("\ndescribe : ");
                sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因");
            } else if (location.getLocType() == BDLocation.TypeNetWorkException) {
                sb.append("\ndescribe : ");
                sb.append("网络不同导致定位失败,请检查网络是否通畅");
            } else if (location.getLocType() == BDLocation.TypeCriteriaException) {
                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());
        }
View Code

启用 

mLocationClient.start();

启动后,因为设置了mLocationClient.registerLocationListener(myListener); // 注册监听函数

所以myListener会调用private class MyLocationListener implements BDLocationListener {里的onReceiveLocation方法返回结果

private class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            if (isFirst) {
                // 让地图中心跑到我的真实位置处
                LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
                MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLngZoom(latLng, 12);
                // 没有动画效果
                // mBaiduMap.setMapStatus(mapStatusUpdate);
                mBaiduMap.animateMapStatus(mapStatusUpdate, 1000);
                isFirst = false;
            }

            mCurrentLocation = location;
            String addrStr = location.getAddrStr();
            tv_info.setText(addrStr);

            // 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());//
             * 单位:公里每小时 授时 4颗 // 2 1位置服务站 // 双向定位 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());// 0 1 2
             * sb.append("\ndescribe : "); sb.append("网络定位成功"); } else if
             * (location.getLocType() == BDLocation.TypeOffLineLocation) {//
             * 离线定位结果 sb.append("\ndescribe : ");
             * sb.append("离线定位成功,离线定位结果也是有效的"); } else if (location.getLocType()
             * == BDLocation.TypeServerError) { sb.append("\ndescribe : ");
             * sb.append
             * ("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因"); } else
             * if (location.getLocType() == BDLocation.TypeNetWorkException) {
             * sb.append("\ndescribe : "); sb.append("网络不同导致定位失败,请检查网络是否通畅"); }
             * else if (location.getLocType() ==
             * BDLocation.TypeCriteriaException) { 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.d("tag", sb.toString());
        }
    }
MyLocationListener

 

关闭

protected void onDestroy() {
super.onDestroy();
// 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
mLocationClient.stop();
mPoiSearch.destroy();
}

//定位到自身位置
        iv_location.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mCurrentLocation != null) {
                    LatLng latLng = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
                    MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLng(latLng);
                    mBaiduMap.animateMapStatus(mapStatusUpdate, 1000);
                }

            }
        });

显示我的位置(蓝色圆点)

if (mCurrentLocation == null) {
                        return;
                    }
                    MyLocationData.Builder myLocationDataBuilder = new MyLocationData.Builder();
                    myLocationDataBuilder.accuracy(mCurrentLocation.getRadius());
                    myLocationDataBuilder.latitude(mCurrentLocation.getLatitude());
                    myLocationDataBuilder.longitude(mCurrentLocation.getLongitude());
                    MyLocationData myLocationData = myLocationDataBuilder.build();
                    mBaiduMap.setMyLocationEnabled(true);
                    mBaiduMap.setMyLocationData(myLocationData);

 POI检索

第一步,创建POI检索实例

mPoiSearch = PoiSearch.newInstance();

第二步,创建POI检索监听者;

OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener(){  
    public void onGetPoiResult(PoiResult result){  
    //获取POI检索结果  
    }  
    public void onGetPoiDetailResult(PoiDetailResult result){  
    //获取Place详情页检索结果  
    }  
};

第三步,设置POI检索监听者;

mPoiSearch.setOnGetPoiSearchResultListener(poiListener);

第四步,发起检索请求;

mPoiSearch.searchInCity((new PoiCitySearchOption())  
    .city(“北京”)  
    .keyword(“美食”)  
    .pageNum(10));

第五步,释放POI检索实例;

mPoiSearch.destroy();
    private OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {
        public void onGetPoiResult(PoiResult result) {
            // 获取POI检索结果
            List<PoiInfo> allPoi = result.getAllPoi();
            for (int i = 0; i < allPoi.size(); i++) {
                PoiInfo poiInfo = allPoi.get(i);
                String tmp = new String();
                tmp += "位置:" + poiInfo.address + "\r\n";
                tmp += "名称:" + poiInfo.name + "\r\n";
                tmp += "电话:" + poiInfo.phoneNum + "\r\n";
                Log.d("MainActivity", tmp);
            }
            mBaiduMap.clear();
            PoiOverlay poiOverlay = new PoiOverlay(mBaiduMap);
            poiOverlay.setData(result);
            poiOverlay.addToMap();

        }

        public void onGetPoiDetailResult(PoiDetailResult result) {
            // 获取Place详情页检索结果
        }
    };

 

        iv_search.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mCurrentLocation == null) {
                    return;
                }
                String key = et_key.getText().toString().trim();
                if (TextUtils.isEmpty(key)) {
                    Toast.makeText(MainActivity.this, "输入内容不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                PoiNearbySearchOption poiNearbySearchOption = new PoiNearbySearchOption();
                LatLng latLng = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
                poiNearbySearchOption.location(latLng);
                poiNearbySearchOption.keyword(key);
                poiNearbySearchOption.pageCapacity(100).pageNum(0).radius(10 * 1000).sortType(PoiSortType.distance_from_near_to_far);
                mPoiSearch.searchNearby(poiNearbySearchOption);

            }
        });

路线检索

点击Marker,弹出对话框,进行路线检索

InfoWindow infoWindow = new InfoWindow(view, position, -20);相对于原点的偏移位置

        mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {

            @Override
            public boolean onMarkerClick(Marker marker) {
                mCurrentMarker = marker;
                LatLng position = marker.getPosition();
                View view = View.inflate(MainActivity.this, R.layout.info_window, null);
                ImageView iv_baidu = (ImageView) view.findViewById(R.id.iv_baidu);
                ImageView iv_walk = (ImageView) view.findViewById(R.id.iv_walk);
                ImageView iv_bus = (ImageView) view.findViewById(R.id.iv_bus);
                ImageView iv_car = (ImageView) view.findViewById(R.id.iv_car);
                ImageView iv_share = (ImageView) view.findViewById(R.id.iv_share);
                iv_baidu.setOnClickListener(MainActivity.this);
                iv_walk.setOnClickListener(MainActivity.this);
                iv_bus.setOnClickListener(MainActivity.this);
                iv_car.setOnClickListener(MainActivity.this);
                iv_share.setOnClickListener(MainActivity.this);
                InfoWindow infoWindow = new InfoWindow(view, position, -20);
                mBaiduMap.showInfoWindow(infoWindow);
                return true;
            }
        });
View Code

 点击图片选择:步行、公交、自驾等,加载到百度地图

public void onClick(View v) {
        if (mCurrentMarker == null) {
            return;
        }
        switch (v.getId()) {
        case R.id.iv_baidu:
            BaiduMapRoutePlan.setSupportWebRoute(true);
            RouteParaOption routeParaOption = new RouteParaOption();
            routeParaOption.cityName(mCurrentLocation.getCity());
            routeParaOption.startPoint(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()));
            routeParaOption.endPoint(mCurrentMarker.getPosition());
            BaiduMapRoutePlan.openBaiduMapTransitRoute(routeParaOption, this);
            break;
        case R.id.iv_walk:
            WalkingRoutePlanOption walkingRoutePlanOption = new WalkingRoutePlanOption();
            PlanNode planNode = PlanNode.withLocation(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()));
            walkingRoutePlanOption.from(planNode );
            PlanNode toPlanNode = PlanNode.withLocation(mCurrentMarker.getPosition());
            walkingRoutePlanOption.to(toPlanNode );
            mRoutePlanSearch.walkingSearch(walkingRoutePlanOption );
            
            break;
        case R.id.iv_bus:
//            Tra transitRouteOverlay = new Tr();
            TransitRoutePlanOption transitRoutePlanOption = new TransitRoutePlanOption();
            transitRoutePlanOption.city(mCurrentLocation.getCity());
            PlanNode planNode2 = PlanNode.withLocation(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()));
            PlanNode toPlanNode2 = PlanNode.withLocation(mCurrentMarker.getPosition());
            transitRoutePlanOption.from(planNode2);
            transitRoutePlanOption.to(toPlanNode2);
            mRoutePlanSearch.transitSearch(transitRoutePlanOption);
            break;
        case R.id.iv_car:
            DrivingRoutePlanOption drivingRoutePlanOption = new DrivingRoutePlanOption();
            PlanNode planNode3 = PlanNode.withLocation(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()));
            PlanNode toPlanNode3 = PlanNode.withLocation(mCurrentMarker.getPosition());
            drivingRoutePlanOption.from(planNode3);
            drivingRoutePlanOption.to(toPlanNode3);
            mRoutePlanSearch.drivingSearch(drivingRoutePlanOption );
            break;
        case R.id.iv_share:
            LocationShareURLOption locationShareURLOption = new LocationShareURLOption();
            locationShareURLOption.location(mCurrentMarker.getPosition());
            locationShareURLOption.name("分享一个位置");
            locationShareURLOption.snippet("来自66的分享");
            mShareUrlSearch.requestLocationShareUrl(locationShareURLOption );
            break;

        default:
            break;
        }

    }
onClick

 

    

如果没有装百度地图

  

自定义线路检索

private void initView() {
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
mSpinner = (Spinner) findViewById(R.id.spinner);
tv_info = (TextView) findViewById(R.id.tv_info);
iv_location = (ImageView) findViewById(R.id.iv_location);
mPoiSearch = PoiSearch.newInstance();
mPoiSearch.setOnGetPoiSearchResultListener(poiListener);
et_key = (EditText) findViewById(R.id.et_key);
iv_search = (ImageView) findViewById(R.id.iv_search);
tv_route = (TextView) findViewById(R.id.tv_route);
mRoutePlanSearch = RoutePlanSearch.newInstance();
mRoutePlanSearch.setOnGetRoutePlanResultListener(listener);

.........

 

定义监听

private OnGetRoutePlanResultListener listener = new OnGetRoutePlanResultListener() {
        public void onGetWalkingRouteResult(WalkingRouteResult result) {
            if (walkingRouteOverlay!=null) {
                walkingRouteOverlay.removeFromMap();
                walkingRouteOverlay = null;
            }
            if (transitRouteOverlay!=null) {
                transitRouteOverlay.removeFromMap();
                transitRouteOverlay = null;
            }
            if (drivingRouteOverlay!=null) {
                drivingRouteOverlay.removeFromMap();
                drivingRouteOverlay = null;
            }
            walkingRouteOverlay = new WalkingRouteOverlay(mBaiduMap);
            WalkingRouteLine walkingRouteLine = result.getRouteLines().get(0);
            int distance = walkingRouteLine.getDistance();
            int duration = walkingRouteLine.getDuration();
            tv_route.setText(distance+"米"+duration/60+"分钟");
            /*TaxiInfo taxiInfo = result.getTaxiInfo();
            if (taxiInfo!=null) {
                tv_route.setText("约"+taxiInfo.getTotalPrice()+"约"+taxiInfo.getDistance()+"米");
            }else {
                tv_route.setText("没有找到打的信息");
            }*/
            walkingRouteOverlay.setData(walkingRouteLine);
            walkingRouteOverlay.addToMap();
            walkingRouteOverlay.zoomToSpan();
            
        }

        public void onGetTransitRouteResult(TransitRouteResult result) {
            if (walkingRouteOverlay!=null) {
                walkingRouteOverlay.removeFromMap();
                walkingRouteOverlay = null;
            }
            if (transitRouteOverlay!=null) {
                transitRouteOverlay.removeFromMap();
                transitRouteOverlay = null;
            }
            if (drivingRouteOverlay!=null) {
                drivingRouteOverlay.removeFromMap();
                drivingRouteOverlay = null;
            }
            transitRouteOverlay = new TransitRouteOverlay(mBaiduMap);
            TransitRouteLine transitRouteLine = result.getRouteLines().get(0);
            tv_route.setText(transitRouteLine.getDistance()+"米"+transitRouteLine.getDuration()/60+"分钟");
            transitRouteOverlay.setData(transitRouteLine);
            transitRouteOverlay.addToMap();
            transitRouteOverlay.zoomToSpan();
        }

        public void onGetDrivingRouteResult(DrivingRouteResult result) {
            if (walkingRouteOverlay!=null) {
                walkingRouteOverlay.removeFromMap();
                walkingRouteOverlay = null;
            }
            if (transitRouteOverlay!=null) {
                transitRouteOverlay.removeFromMap();
                transitRouteOverlay = null;
            }
            if (drivingRouteOverlay!=null) {
                drivingRouteOverlay.removeFromMap();
                drivingRouteOverlay = null;
            }
            drivingRouteOverlay = new DrivingRouteOverlay(mBaiduMap);
            DrivingRouteLine drivingRouteLine = result.getRouteLines().get(0);
            tv_route.setText(drivingRouteLine.getDistance()+"米"+drivingRouteLine.getDuration()/60+"分钟");
            drivingRouteOverlay.setData(result.getRouteLines().get(0));
            drivingRouteOverlay.addToMap();
            drivingRouteOverlay.zoomToSpan();
            
        }
    };
View Code

定义点击事件

 

public void onClick(View v) {
        if (mCurrentMarker == null) {
            return;
        }
        switch (v.getId()) {
        case R.id.iv_baidu:
            BaiduMapRoutePlan.setSupportWebRoute(true);
            RouteParaOption routeParaOption = new RouteParaOption();
            routeParaOption.cityName(mCurrentLocation.getCity());
            routeParaOption.startPoint(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()));
            routeParaOption.endPoint(mCurrentMarker.getPosition());
            BaiduMapRoutePlan.openBaiduMapTransitRoute(routeParaOption, this);
            break;
        case R.id.iv_walk:
            WalkingRoutePlanOption walkingRoutePlanOption = new WalkingRoutePlanOption();
            PlanNode planNode = PlanNode.withLocation(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()));
            walkingRoutePlanOption.from(planNode );
            PlanNode toPlanNode = PlanNode.withLocation(mCurrentMarker.getPosition());
            walkingRoutePlanOption.to(toPlanNode );
            mRoutePlanSearch.walkingSearch(walkingRoutePlanOption );
            
            break;
        case R.id.iv_bus:
//            Tra transitRouteOverlay = new Tr();
            TransitRoutePlanOption transitRoutePlanOption = new TransitRoutePlanOption();
            transitRoutePlanOption.city(mCurrentLocation.getCity());
            PlanNode planNode2 = PlanNode.withLocation(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()));
            PlanNode toPlanNode2 = PlanNode.withLocation(mCurrentMarker.getPosition());
            transitRoutePlanOption.from(planNode2);
            transitRoutePlanOption.to(toPlanNode2);
            mRoutePlanSearch.transitSearch(transitRoutePlanOption);
            break;
        case R.id.iv_car:
            DrivingRoutePlanOption drivingRoutePlanOption = new DrivingRoutePlanOption();
            PlanNode planNode3 = PlanNode.withLocation(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()));
            PlanNode toPlanNode3 = PlanNode.withLocation(mCurrentMarker.getPosition());
            drivingRoutePlanOption.from(planNode3);
            drivingRoutePlanOption.to(toPlanNode3);
            mRoutePlanSearch.drivingSearch(drivingRoutePlanOption );
            break;
        case R.id.iv_share:
            LocationShareURLOption locationShareURLOption = new LocationShareURLOption();
            locationShareURLOption.location(mCurrentMarker.getPosition());
            locationShareURLOption.name("分享一个位置");
            locationShareURLOption.snippet("来自66的分享");
            mShareUrlSearch.requestLocationShareUrl(locationShareURLOption );
            break;

        default:
            break;
        }

    }
View Code

 

位置分享

 短串分享是指,用户搜索查询后得到的每一个地理位置结果将会对应一条短串(短链接),用户可以通过短信、邮件或第三方分享组件(如微博、微信等)把短串分享给其他用户从而实现地理位置信息的分享。当其他用户收到分享的短串后,点击短串即可打开手机上的百度地图客户端或者手机浏览器进行查看。

在INITVIEW初始化

mShareUrlSearch = ShareUrlSearch.newInstance();
mShareUrlSearch.setOnGetShareUrlResultListener(shareUrlResultListener);

定义监听

private OnGetShareUrlResultListener shareUrlResultListener = new OnGetShareUrlResultListener() {  
        public void onGetPoiDetailShareUrlResult(ShareUrlResult result) {  
            //分享POI详情  
        }  
        public void onGetLocationShareUrlResult(ShareUrlResult result) {  
            //分享位置信息 
            //TODO:///
            String url = result.getUrl();
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, url);
            sendIntent.setType("text/plain");//
            startActivity(Intent.createChooser(sendIntent, "调用的系统分享"));
        }  
    };

在点击事件定义

case R.id.iv_share:
            LocationShareURLOption locationShareURLOption = new LocationShareURLOption();
            locationShareURLOption.location(mCurrentMarker.getPosition());
            locationShareURLOption.name("分享一个位置");
            locationShareURLOption.snippet("来自66的分享");
            mShareUrlSearch.requestLocationShareUrl(locationShareURLOption );
            break;

 

posted @ 2016-11-21 21:00  ecollab  阅读(887)  评论(0编辑  收藏  举报