1.首先获取任务地点的实际坐标值,用hashmap存储
HashMap<Integer, double[]> hashMap = new HashMap<>();
hashMap.put(0, new double[]{xxx.xxxx,36.xxxx});//三区
hashMap.put(1, new double[]{xxx.xxxx,36.xxxx});//一号食堂
hashMap.put(2, new double[]{xxx.xxxx,36.xxxx});//图书馆
2.创建任务提示弹窗
使用户接受任务之后随机定位目标地点,
missionButton = (Button) findViewById(R.id.missionButton);
missionButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(!jieshourenwu){
dia1 = new AlertDialog.Builder(LocationDemo.this).setTitle("是否接受任务?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
jieshourenwu = true;
dia2 = new AlertDialog.Builder(LocationDemo.this).setTitle("任务地点").setMessage(missionLocation[didian])
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.show();
}
})
.show();
}
3.每隔一段时间获取自己的位置,判断是否在任务地点的区域内
for(int i = 0;i<missionLocation.length;i++){
// double longtitude = hashMap.get(i)[0];
// double latitude = hashMap.get(i)[1];
if(Math.abs(location.getLongitude()-hashMap.get(i)[0])<=0.0003 && Math.abs(location.getLatitude()-hashMap.get(i)[1])<=0.0003) {
if((!kaishirenwuDialog.isShowing())&&i==didian){
Toast.makeText(LocationDemo.this, "你已到达"+missionLocation[i],Toast.LENGTH_SHORT).show();
kaishirenwuDialog.show();
}
break;
} else{
//Toast.makeText(LocationDemo.this, location.getLongitude()+" , "+location.getLatitude(),Toast.LENGTH_SHORT).show();
}
}
如果到达地点,弹出开始任务框
//开始任务
AlertDialog.Builder kaishirenwuDialogBuilder = new AlertDialog.Builder(LocationDemo.this).setTitle("是否开始任务?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
double mi = Math.random() * 3;
int missiond = (int) mi;//3个任务
switch (missiond){
case 0: {
kaishirenwuDialog.dismiss();
dia1.dismiss();
dia2.dismiss();
startActivityForResult(new Intent(LocationDemo.this, unityactivity.class), 1);
break;
}
case 1: {
kaishirenwuDialog.dismiss();
dia1.dismiss();
dia2.dismiss();
startActivityForResult(new Intent(LocationDemo.this, HandgestureActivity.class), 4);
break;
}
case 2: {
break;
}
}
}
});
kaishirenwuDialog = kaishirenwuDialogBuilder.create();
最后进行页面跳转