功能点:
- 使用高德地图实现地理围栏功能
- 可设置手机号和短信间隔,设置后间隔发送当前位置
- 可地图选点设置地理围栏
- 离开进入地理围栏会有相应短信提醒(设置手机号并获取权限后)
![zbG1G.md.png](https://s1.ax2x.com/2017/12/20/zbG1G.md.png)
关键代码
public class MainActivity extends BaseActivity
implements NavigationView.OnNavigationItemSelectedListener,View.OnClickListener,
GeoFenceListener,
AMap.OnMapClickListener,
LocationSource,
AMapLocationListener,
CompoundButton.OnCheckedChangeListener {
private View lyOption;
private TextView tvGuide;
private TextView tvResult;
private EditText etRadius;
private CheckBox cbAlertIn;
private CheckBox cbAlertOut;
private CheckBox cbAldertStated;
private Button btAddFence;
private Button btOption;
private AMapLocationClient mlocationClient;
private LocationSource.OnLocationChangedListener mListener;
private AMapLocationClientOption mLocationOption;
private MapView mMapView;
private AMap mAMap;
private LatLng centerLatLng = null;
private Marker centerMarker;
private BitmapDescriptor ICON_YELLOW = BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW);
private BitmapDescriptor ICON_RED = BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED);
private MarkerOptions markerOption = null;
private List<Marker> markerList = new ArrayList<Marker>();
private LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();
private GeoFenceClient fenceClient = null;
private float fenceRadius = 0.0F;
private int activatesAction = GeoFenceClient.GEOFENCE_IN;
private static final String GEOFENCE_BROADCAST_ACTION = "com.example.geofence.round";
private HashMap<String, GeoFence> fenceMap = new HashMap<String, GeoFence>();
private View map_top;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermission();
initView();
initMap(savedInstanceState);
}
private void requestPermission() {
Acp.getInstance(this).request(new AcpOptions.Builder()
.setPermissions(Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE)
.build(),
new AcpListener() {
@Override
public void onGranted() {
}
@Override
public void onDenied(List<String> permissions) {
ToastUtil.show(MainActivity.this,"权限不够,应用可能会出现问题");
}
});
}
private void initView() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendMessage();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View headerView = navigationView.getHeaderView(0);
TextView user = (TextView) headerView.findViewById(R.id.user_textview);
String userid = SpUtil.getString(ConstantValue.USER_LOGIN_INFO, "");
user.append(userid+"");
}
private void sendMessage() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
showMissingPermissionDialog();
}else{
startMessage();
}
}
private void showMissingPermissionDialog() {
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
builder.setTitle(R.string.notifyTitle);
builder.setMessage(R.string.notifyMsg);
builder.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.setPositiveButton(R.string.setting,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startAppSettings();
}
});
builder.setCancelable(false);
builder.show();
}
private void startMessage() {
handler.post(runnable);
}
private