百度地图系列之07——只显示圆内的marker+Fragment
本文主要写的是在自定义圆内显示marker,判断marker是否在圆内,如果在就显示,不在就不显示,方法marker与圆心的距离小于圆的半径。这里与前面的百度地图系列之05类似。
判断是否在圆内并添加marker的方法
//获得marker与圆中心点之间的距离。
double distance = DistanceUtil.getDistance(pt_ll, mCenter_ll);
Log.i("distance", String.valueOf(distance));
//圆的半径是8000,这里为美观取7600,距离小于7600的显示
if(distance<7600){
BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding);
OverlayOptions options = new MarkerOptions()
.position(pt_ll)
.icon(bitmap)
.zIndex(14)
.draggable(true);
marker = (Marker) mBaiduMap.addOverlay(options);
}
其中百度地图api中有个DistanceUtil类,该类有个getDistance方法,可以计算两点之间的距离。
源码:
public class HomeFragment extends Fragment {
private Activity activity;
private MapView mMapView;
public MyLocationListener myListener = new MyLocationListener();
BaiduMap mBaiduMap;
LocationClient mLocClient;
boolean isFirstLoc = true;
boolean isLocationClientStop = false;
ConnectServerIml iml;
ArrayList<String> poleName,poleAddr,poleFix;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreateView(inflater, container, savedInstanceState);
this.activity = getActivity();
SDKInitializer.initialize(activity.getApplicationContext());
return inflater.inflate(R.layout.fragment_home, container,false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
SharedPreferences shared = activity.getSharedPreferences("latlng", 0);
mMapView = (MapView) activity.findViewById(R.id.mapview);
mBaiduMap = mMapView.getMap();
mBaiduMap.setMyLocationEnabled(true);
mLocClient = new LocationClient(activity);
mLocClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);
option.setCoorType("bd09ll");//bd09LL
option.setScanSpan(1000);
mLocClient.setLocOption(option);
mLocClient.start();
Log.i("LatLng", shared.getString("Lat", "this is null"));
mBaiduMap.setOnMapStatusChangeListener(new OnMapStatusChangeListener() {
//地图状态改变前
@Override
public void onMapStatusChangeStart(MapStatus status) {
// TODO Auto-generated method stub
// updateMapStatus(status);
}
//地图状态改变后
@Override
public void onMapStatusChangeFinish(MapStatus status) {
// TODO Auto-generated method stub
updateMapStatus(status);
}
//地图状态改变时
@Override
public void onMapStatusChange(MapStatus status) {
// TODO Auto-generated method stub
// updateMapStatus(status);
drawCircle(status);
}
});
}
private void drawCircle(MapStatus status){
LatLng mCenter = status.target;
mBaiduMap.clear();
//画圆
OverlayOptions ooCircle = new CircleOptions().fillColor(0x384d73b3)
.center(mCenter).stroke(new Stroke(3, 0x784d73b3))
.radius(8000);
mBaiduMap.addOverlay(ooCircle);
}
//更新地图状态
private void updateMapStatus(MapStatus status){
//中心点坐标
final LatLng mCenter = status.target;
//异步线程
asyncTask task = new asyncTask();
task.execute(mCenter);
}
class asyncTask extends AsyncTask<LatLng, Integer, String>{
@Override
protected String doInBackground(LatLng... params) {
// TODO Auto-generated method stub
//充电桩服务器查询
iml = new ConnectServerIml();
String url = ShareData.CHARGE_SEARCH;
String result = iml.ConcernByClientGet(url);
//json数据解析
ArrayList<PolePoint> list = JsonParse.search(result);
PolePoint pp;
poleName = new ArrayList<String>();
poleAddr = new ArrayList<String>();
poleFix = new ArrayList<String>();
for (int i = 0; i < list.size(); i++) {
pp = list.get(i);
poleName.add(pp.getPoleName());
poleAddr.add(pp.getPoleAdress());
poleFix.add(pp.getPoleFix());
}
/*
* Bundle类用作携带数据,它类似于Map,用于存放key-values键值
* 对形式的值,相当于map,它提供了各种常用的类型如putXxx()/
* getXxx()方法,Bundle内部实际上是使用了HashMap类型的变量来
* 存放putXxx()方法的值。
*
*/
Bundle bundle = new Bundle();
Message msg = new Message();
msg.what = 0x01;
bundle.putStringArrayList("poleNameList", poleName);
bundle.putStringArrayList("poleAddrList", poleAddr);
bundle.putStringArrayList("poleFixList", poleFix);
bundle.putString("mCenter", params[0].latitude+","+params[0].longitude);
msg.setData(bundle);
//发送消息给hander
handler.sendMessage(msg);
return null;
}
}
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
if(msg.what == 0x01){
Marker marker;
ArrayList<String> list = msg.getData().getStringArrayList("poleFixList");
int length = list.size();
for (int i = 0; i < length; i++) {
String pt = list.get(i);
double lat = iml.getLat(pt);
double lng = iml.getLng(pt);
//marker的经纬度
LatLng pt_ll = new LatLng(lat, lng);
//将String类型的经纬度转换成LatLng类型,getLat()和getLng()函数为自己编写,方法是字符串截取。
String mCenter = msg.getData().getString("mCenter");
LatLng mCenter_ll = new LatLng(iml.getLat(mCenter), iml.getLng(mCenter));
//获得marker与圆中心点之间的距离。
double distance = DistanceUtil.getDistance(pt_ll, mCenter_ll);
Log.i("distance", String.valueOf(distance));
//圆的半径是8000,这里为美观取7600,距离小于7600的显示
if(distance<7600){
BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding);
OverlayOptions options = new MarkerOptions()
.position(pt_ll)
.icon(bitmap)
.zIndex(14)
.draggable(true);
marker = (Marker) mBaiduMap.addOverlay(options);
}
}
}
};
};
public class MyLocationListener implements BDLocationListener{
@Override
public void onReceiveLocation(BDLocation location) {
// TODO Auto-generated method stub
if(location == null||isLocationClientStop)
return;
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
.direction(100)
.latitude(location.getLatitude())
.longitude(location.getLongitude())
.build();
mBaiduMap.setMyLocationData(locData);
if(isFirstLoc){
isFirstLoc = false;
//定位点坐标
LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
//设置地图中心点和缩放级别
MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, 12);
//以动画方式更新地图状态,动画耗时 300 ms
mBaiduMap.animateMapStatus(u);
}
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
// mLocClient.stop();
// mBaiduMap.setMyLocationEnabled(false);
// mMapView = null;
mMapView.onDestroy();
super.onDestroy();
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
mMapView.onPause();
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
mMapView.onResume();
}
}
截图:
在此就不给出源码下载地址了。