集成初始化地图
//在使用SDK各组件之前初始化context信息,传入ApplicationContext
//注意该方法要再setContentView方法之前实现
SDKInitializer.initialize(getActivity().getApplicationContext());
View view=inflater.inflate(R.layout.mapfragment,null);
mMapView = (MapView)view.findViewById(R.id.bmapView);
TextView title_name = (TextView) view.findViewById(R.id.title_name);
title_name.setText("地图");
mBaiduMap = mMapView.getMap();
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
// 修改为自定义marker
mCurrentMarker= BitmapDescriptorFactory.fromResource(R.drawable.location_marker);
mBaiduMap
.setMyLocationConfigeration(new MyLocationConfiguration(
MyLocationConfiguration.LocationMode.NORMAL, true, mCurrentMarker,
accuracyCircleFillColor, accuracyCircleStrokeColor));
// 定位初始化
mLocClient = new LocationClient(getActivity());
mLocClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); // 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);
mLocClient.setLocOption(option);
mLocClient.start();
initData();
return view;
//获取定位信息和网络信息
private void initData() {
OkHttp.getAsync(Url_Utils.MAP_XX, new OkHttp.DataCallBack() {
@Override
public void requestFailure(Request request, IOException e) {
}
@Override
public void requestSuccess(String result) throws Exception {
Gson gson=new Gson();
final List<MapBean> list=new ArrayList<MapBean>();
MapBean[] mapBean = gson.fromJson(result, MapBean[].class);
for (MapBean b : mapBean) {
list.add(b);
}
//设置目标位置,经纬度位置,设置地图maker对象
ll = new LatLng(Double.parseDouble(list.get(0).getMaplat()
),Double.parseDouble(list.get(0).getMaplng()));
bdA = BitmapDescriptorFactory
.fromResource(R.drawable.mark2);
ooA = new MarkerOptions().position(ll).icon(bdA)
.zIndex(10).draggable(true);
// 掉下动画
//ooA.animateType(MarkerOptions.MarkerAnimateType.drop);
mMarkerA = (Marker) (mBaiduMap.addOverlay(ooA));
//marker监听,弹出窗口
mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
//设置信息窗口设置的文字
View view=View.inflate(getActivity(),R.layout.map_popview,null);
TextView mapname = (TextView) view.findViewById(R.id.map_name);
ImageView logo = (ImageView) view.findViewById(R.id.map_logo);
mapname.setText(list.get(0).getTitle());
//生成信息窗口
infowindow=new InfoWindow(view,ll,0);
mBaiduMap.showInfoWindow(infowindow);
logo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), SearchItemDetailsCompany.class);
//获取id传值,这里注意是int值,所以那边要用int类型接收数据
intent.putExtra("id",list.get(0).getId());
//获取id传值,这里注意是String值,所以那边要用String类型接收数据
intent.putExtra("logo",list.get(0).getLogo());
startActivity(intent);
}
});
return true;
}
});
}
});
}
@Override
public void onDestroy() {
// 退出时销毁定位
mLocClient.stop();
// 关闭定位图层
mBaiduMap.setMyLocationEnabled(false);
mMapView.onDestroy();
mMapView = null;
super.onDestroy();
//mMapView.onDestroy();
}
@Override
public void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
//在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}
定位集成方法
// 定位相关
LocationClient mLocClient;
public MyLocationListenner myListener = new MyLocationListenner();
boolean isFirstLoc = true; // 是否首次定位
private BaiduMap mBaiduMap;
// 定位初始化
mLocClient = new LocationClient(getActivity());
mLocClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); // 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);
mLocClient.setLocOption(option);
mLocClient.start();
/**
* 定位SDK监听函数
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || mMapView == null) {
return;
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(0)
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(100).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
if (isFirstLoc) {
isFirstLoc = false;
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatus.Builder builder = new MapStatus.Builder();
builder.target(ll).zoom(18.0f);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
//设置初始化比例尺大小
float f = mBaiduMap.getMaxZoomLevel();//19.0 最小比例尺
float m = mBaiduMap.getMinZoomLevel();//3.0 最大比例尺
MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, m+9);//设置到100米的大小
mBaiduMap.animateMapStatus(u);
}
}
public void onReceivePoi(BDLocation poiLocation) {
}
}
设置定位图形动画
mBaiduMap = mMapView.getMap();
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
// 修改为自定义marker
mCurrentMarker= BitmapDescriptorFactory.fromResource(R.drawable.location_marker);
mBaiduMap
.setMyLocationConfigeration(new MyLocationConfiguration(
MyLocationConfiguration.LocationMode.NORMAL, true, mCurrentMarker,
accuracyCircleFillColor, accuracyCircleStrokeColor));
自定义坐标点动画
//设置目标位置
ll = new LatLng(40.03679,
116.367732);
bdA = BitmapDescriptorFactory
.fromResource(R.drawable.map_func_icon_nearby);
ooA = new MarkerOptions().position(ll).icon(bdA)
.zIndex(10).draggable(true);
// 掉下动画
ooA.animateType(MarkerOptions.MarkerAnimateType.drop);
mMarkerA = (Marker) (mBaiduMap.addOverlay(ooA));