想用定位和画面做一个围栏功能,超出这个区域会震动提醒。但是打开后底图会消失,Fence坐标系为WGS84,底图是天地图。
部分代码:
private boolean drawRegion(){
if(editLayer != null){
editLayer.setEditable(true);
mapView.getMapControl().setAction( Action.DRAWPLOYGON);
System.out.println("绘制结束1");
return true;
}
Workspace workspace = mapView.getMapControl().getMap().getWorkspace();
String datasourceName = "fence";
// 设计图层风格
GeoStyle geoStyle_R = new GeoStyle ();
geoStyle_R.setFillForeColor(new com.supermap.data.Color(220, 220,220));
geoStyle_R.setFillBackOpaque(true);
geoStyle_R.setFillOpaqueRate(40);
geoStyle_R.setLineWidth(0.1);
geoStyle_R.setLineColor(new com.supermap.data.Color(40,40,40));
LayerSettingVector layerSettingVector = new LayerSettingVector ();
layerSettingVector.setStyle(geoStyle_R);
if (mapView.getMapControl().getMap().getLayers().getCount() == 1) {
Datasource datasource = workspace.getDatasources().get(datasourceName);
Dataset dataset = datasource.getDatasets().get(0);
editLayer = mapView.getMapControl().getMap().getLayers().add(dataset, true);
} else {
editLayer = mapView.getMapControl().getMap().getLayers().get(0);
}
editLayer.setEditable(true);
editLayer.setAdditionalSetting(layerSettingVector);
mapView.getMapControl().setAction( Action.DRAWPLOYGON);
mapView.getMapControl().addGeometryAddedListener(mGeometryAddedListener);
return true;
}
private void initLocation(){
/**
* 定位SDK是否同意隐私政策接口
* false不同意:不支持定位,SDK抛出异常
* true同意:支持定位功能
*/
LocationClient.setAgreePrivacy(true);
// 定位初始化时捕获异常
try {
mLocationClient = new LocationClient(this);
MyLocationListener mMyLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(mMyLocationListener);
LocationClientOption option = new LocationClientOption();
// 打开gps
option.setOpenGps(true);
// 设置坐标类型
option.setCoorType("bd09ll");
option.setScanSpan(1000);
mLocationClient.setLocOption(option);
mLocationClient.start();
} catch (Exception e) {
e.printStackTrace();
}
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
option.setCoorType("gcj02");//可选,默认gcj02,设置返回的定位结果坐标系,
int span=0;
option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
option.setOpenGps(true);//可选,默认false,设置是否使用gps
option.setLocationNotify(true);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
option.setIgnoreKillProcess(true);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要
option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
mLocationClient.setLocOption(option);
}
/**
* 实现定位回调
*/
public class MyLocationListener extends BDAbstractLocationListener {
public double latitude;
public double longitude;
@Override
public void onReceiveLocation(BDLocation location) {
//此处的BDLocation为定位结果信息类,通过它的各种get方法可获取定位相关的全部结果
//以下只列举部分获取经纬度相关(常用)的结果信息
//更多结果信息获取说明,请参照类参考中BDLocation类中的说明
//获取纬度信息
latitude = location.getLatitude();
//获取经度信息
longitude = location.getLongitude();
point2d = new Point2D();
point2d.setX(longitude);
point2d.setY(latitude);
PrjCoordSys Prj = map.getPrjCoordSys();
if (Prj.getType() != PrjCoordSysType.PCS_EARTH_LONGITUDE_LATITUDE) {
Point2Ds point2Ds = new Point2Ds();
point2Ds.add(point2d);
PrjCoordSys prjCoordSys = new PrjCoordSys();
prjCoordSys.setType(PrjCoordSysType.PCS_EARTH_LONGITUDE_LATITUDE);
CoordSysTranslator.convert(point2Ds, prjCoordSys, Prj, new CoordSysTransParameter(), CoordSysTransMethod.MTH_GEOCENTRIC_TRANSLATION);
point2d = point2Ds.getItem(0);
addCallOutByName(point2d);
}
monitor(latitude,longitude);
}
}
private void addCallOutByName(Point2D point2d) {
mapView.removeAllCallOut();
CallOut callout = new CallOut(this);
callout.setStyle(CalloutAlignment.CENTER);//该类设置了点标注对齐方式类型常量。--------中心对齐。
callout.setCustomize(true);//设置是否自定义背景。
callout.setLocation(point2d.getX(), point2d.getY());
ImageView image = new ImageView(this);
image.setBackgroundResource(R.drawable.c_shouye_location);
callout.setContentView(image);
Map map = mapView.getMapControl().getMap();
map.setCenter(point2d);
map.setScale(1.0 / 2000);//地图界面大小
mapView.addCallout(callout);
map.refresh();
}
public DynamicPoint queryPeople(double latitude, double longitude){
peoID=1;
DynamicPoint dynamicPoint=new DynamicPoint();
Point2Ds point2Ds=new Point2Ds(new Point2D[]{new Point2D(longitude,latitude)});
Point2D ex=new Point2D(point2Ds.getItem(0).getX(),point2Ds.getItem(0).getY());
CoordSysTranslator.forward(point2Ds, mapView.getMapControl().getMap().getPrjCoordSys());
Point2D newPoint2D = new Point2D (point2Ds.getItem(0).getX(), point2Ds.getItem(0).getY());
dynamicPoint.addPoint(newPoint2D);
return dynamicPoint;
}
private void monitor(double latitude,double longitude){
DynamicPoint dynamicPoint=queryPeople(latitude,longitude);
Rectangle2D rectangle2D=dynamicPoint.getBounds();
if(rectangle2D==null){
return;
}
Rectangle2D rectangle2D2=new Rectangle2D(rectangle2D.getLeft(),rectangle2D.getBottom(),rectangle2D.getRight()*1.1,rectangle2D.getTop()*1.1);
GeoRectangle geoRectangle=new GeoRectangle(rectangle2D2,0);
Point2Ds point2Ds=dynamicPoint.getGeoPoints();
Point2D point2D=point2Ds.getItem(0);
GeoPoint geoPoint=new GeoPoint(point2D);
int id=peoID;
System.out.println(peoID);
if(geoRectangle!=null&&m_MonitorDomainList.size()>0){
for(FenceActivity.MonitorDomain monitorDomain:m_MonitorDomainList){
Geometry monitorDomainGeo = monitorDomain.getMonitorGeometry();
if(monitorDomainGeo == null ){
break;
}
boolean isContainOld = Geometrist.canContain(monitorDomainGeo, geoPoint);
boolean isContainOldID = monitorDomain.getMointorIDs().contains(id);
System.out.println(String.valueOf(isContainOld));
if(isContainOld){
//在监控区域内
textView.setVisibility(View.VISIBLE);
} else{
// 不在监控区域内
Toast.makeText(context,"您已离开规定范围",Toast.LENGTH_SHORT).show();
vibrator=(Vibrator) context.getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(2000);
textView.setVisibility(View.VISIBLE);
textView.setText("您已离开规定区域");
textView.setTextColor(getResources().getColor(R.color.red));
}
}
}
}