android开发:百度地图及定位的演示代码

//百度地图定位的代码,需要加载百度地图API的SDK
LocationClient client = new LocationClient(mContext);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);
option.setAddrType("all");
option.setCoorType("bd0911");
option.setScanSpan(100);
client.setLocOption(option);

client.registerLocationListener(new BDLocationListener() {
    @Override
    public void onReceiveLocation(BDLocation bdLocation) {
        Log.i(TAG,"onReceiveLocation");
        Toast.makeText(mContext,"lat:"+bdLocation.getLatitude()
        +" lng:"+bdLocation.getLongitude(),Toast.LENGTH_LONG).show();
    }
});
client.start();//开始定位

//百度地图添加遮敝层
double lat = 29.56301;
double lng = 106.551557;
LatLng ll = new LatLng(lat,lng);
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(msu);

BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.notification);
OverlayOptions options = new MarkerOptions().position(ll).icon(bitmap);

Marker marker = (Marker) mBaiduMap.addOverlay(options);
marker.setTitle("测试");
Bundle bundle = new Bundle();
bundle.putSerializable("recore","c水电费卡机红烧豆腐");
marker.setExtraInfo(bundle);

//如果需要点击地图的遮蔽层获取地址信息可以创建一个事件监听
mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
    @Override
    public void onMapClick(LatLng latLng) {
        GeoCoder getCoder = GeoCoder.newInstance();
        ReverseGeoCodeOption reCodeOption = new ReverseGeoCodeOption();
        reCodeOption.location(arg0);
        getCoder.reverseGeoCode(reCodeOption);
        getCoder.setOnGetGeoCodeResultListener(new GetAddress());
    }

    @Override
    public boolean onMapPoiClick(MapPoi mapPoi) {
        return false;
    }
});

//GetAddress的类代码
class GetAddress implements OnGetGeoCoderResultListener { //点击地图获取点击位置处的详细地址

    @Override
    public void onGetGeoCodeResult(GeoCodeResult arg0) {

    }

    @Override
    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult arg0) {
        arg0.getAddress();

        mBaiduMap.hideInfoWindow();
        mark(arg0.getLocation().latitude, arg0.getLocation().longitude,
                arg0.getAddress());
    }

}

			

Leave a Comment

 
Copyright © 2008-2021 lanxinbase.com Rights Reserved. | 粤ICP备14086738号-3 |