Android使⽤⾼德地图地理围栏定位⾃动功能点:
使⽤⾼德地图实现地理围栏功能
可设置⼿机号和短信间隔,设置后间隔发送当前位置
可地图选点设置地理围栏
离开进⼊地理围栏会有相应短信提醒(设置⼿机号并获取权限后)
关键代码
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;
// 中⼼点marker
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;
// 地理围栏的⼴播action
private static final String GEOFENCE_BROADCAST_ACTION = "und";
// 记录已经添加成功的围栏
private HashMap<String, GeoFence> fenceMap = new HashMap<String, GeoFence>();
private View map_top;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
requestPermission();
initView();
initMap(savedInstanceState);
}
private void requestPermission() {
.setPermissions(Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
alertdialog使用方法
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(lbar);
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 = HeaderView(0);
TextView user = (TextView) headerView.findViewById(R.id.user_textview);
String userid = String(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(ifyTitle);
builder.setMessage(ifyMsg);
// 拒绝, 退出应⽤
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 int sendMessageDelay = 60000;
Runnable runnable=new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//要做的事情
handler.postDelayed(this, 60000);
send("定位成功 ");
}
};
private void send(String message) {
if(TextUtils.isEmpty(phone)) {
ToastUtil.show(this,"您还没有设置⼿机号,请在侧滑菜单中设置⼿机号后重试!");
return;
}
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
ToastUtil.show(this,"您没有获得的权限");
}else{
SmsManager smsManager = Default();
Log.e("tag",""+currentLocation.length());
smsManager.sendTextMessage(phone,"",message+currentLocation+"",null,null);            ToastUtil.show(this,"发送位置成功");
ToastUtil.show(this,"发送位置成功");
}
}
/**
*  启动应⽤的设置
*
* @since 2.5.0
*
*/
private void startAppSettings() {
Intent intent = new Intent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
}
private void initMap(Bundle savedInstanceState) {
// 初始化地理围栏
fenceClient = new GeoFenceClient(getApplicationContext());
tvResult = (TextView)findViewById(R.id.tv_result);
tvResult.setVisibility(View.GONE);
showSetMapDialog = new Dialog(this);
View view = LayoutInflater.from(this).inflate(R.layout.plugin_geofence_option, null);        showSetMapDialog.setContentView(view);
map_top = view.findViewById(R.id.map_top);
lyOption = view.findViewById(R.id.ly_option);
btAddFence = (Button) view.findViewById(R.id.bt_addFence);
btOption = (Button) view.findViewById(R.id.bt_option);
tvGuide = (TextView) view.findViewById(R.id.tv_guide);
etRadius = (EditText) view.findViewById(_radius);
cbAlertIn = (CheckBox) view.findViewById(R.id.cb_alertIn);
cbAlertOut = (CheckBox) view.findViewById(R.id.cb_alertOut);
cbAldertStated = (CheckBox) view.findViewById(R.id.cb_alertStated);
mMapView = (MapView) findViewById(R.id.map);
markerOption = new MarkerOptions().draggable(true);
init();
String username = String(ConstantValue.USER_LOGIN_INFO,"");
List<User> users = DataSupport.where("username = ? ", username).find(User.class);
if (users!=null && users.size()>0) {
try{
User user = (0);
phone = SendMessagePhone();
sendMessageDelay = Integer.SendMessageDelay());
}catch (Exception e) {
}
}
}
private Dialog showSetMapDialog;
private void showSetMapDialog() {
if(showSetMapDialog.isShowing()) {
showSetMapDialog.dismiss();
}else {
showSetMapDialog.show();
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().u.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = ItemId();
if (id == R.id.action_zhuxiao) {
SpUtil.putInt(ConstantValue.USER_LOGIN_INFO,0);
ToastUtil.show(this,"已注销当前帐号,请重新登录");
startActivity(new Intent(this, LoginActivity.class));
finish();
return true;
}
if(id==R.id.action_exit) {
finish();
return true;
}
OptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = ItemId();
if (id == R.id.nav_phone) {
showPhoneDialog();
}else if(id == R.id.nav_weilan) {
showSetMapDialog();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private String phone;
private void showPhoneDialog() {
final EditText et = new EditText(this);
et.setHint("11位⼿机号");
if(!TextUtils.isEmpty(phone)) {
et.setText(phone);
}
final EditText et1 = new EditText(this);
et1.setHint("间隔(s)");
et1.setInputType(InputType.TYPE_CLASS_NUMBER);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.addView(et);
linearLayout.addView(et1);
linearLayout.setOrientation(LinearLayout.VERTICAL);
new AlertDialog.Builder(this).setTitle("请输⼊⼿机号和短信间隔")
.setView(linearLayout)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String input = et.getText().toString();
if (input.equals("")) {
Toast.makeText(getApplicationContext(), "⼿机号不能为空!" + input, Toast.LENGTH_LONG).show();                        }
else {