我正试图实现基于GPS的警报,如果我在x市,当我到达特定的精确位置时,我在那个地方放置一个pin点,它应该给我一个警报或警报。我已经实现了一个完整的地图结构,我也已经编程显示我的当前位置和放置一个精确位置。我不知道如何附上源代码,请以这种方式帮助我,我真的是新的android,我不知道如何做到这一点。这是代码,请告诉我哪里出错了。
public void onLocationChanged(Location l)
{
// TODO Auto-generated method stub
lat=(int) (l.getLatitude() *1E6) ;
longi=(int) (l.getLongitude() *1E6);
ourLocation= new GeoPoint(lat,longi);
OverlayItem overlayItem= new OverlayItem(ourLocation,"","");
CustomPinpoint custom=new CustomPinpoint(d,Main.this);
custom.insertPinPoint(overlayItem);
overlayList.add(custom);
controller.setCenter(ourLocation);
geocoder= new Geocoder(getBaseContext(), Locale.getDefault());
try
{
List<Address>address1=geocoder.getFromLocation
(ourLocation.getLatitudeE6()/1E6,ourLocation .getLongitudeE6()/1E6, 1);
if(address1.size()>0 )
{
for(int i=0; i<address1.get(0).getMaxAddressLineIndex();i++)
{
display1 += address1.get(0).getAddressLine(i)+"n";
}
}
}
catch(IOException e1)
{
e1.printStackTrace();
Toast.makeText(getBaseContext(), "error", Toast.LENGTH_LONG).show();
}
if(display1.equals(display))
{
AlertDialog alert= new AlertDialog.Builder(Main.this).create();
alert.setTitle("Destination");
alert.setMessage("You Reached to destination");
alert.setButton("OK",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
// TODO Auto-generated method stub
}
});
alert.show();
}
}
上面的代码不足以显示警告对话框。要在代码中应用警告,必须包含这样的代码形式;
/**
* Function to show settings alert dialog On pressing Settings button will
* lauch Settings Options
* */
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog
.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
你必须在你的项目的src/目录下创建一个单独的java代码(类);
下面的类名是"AlertDialogManager.java"
public class AlertDialogManager {
/**
* Function to display simple Alert Dialog
* @param context - application context
* @param title - alert dialog title
* @param message - alert message
* @param status - success/failure (used to set icon)
* - pass null if you don't want icon
* */
public void showAlertDialog(Context context, String title, String message,
Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
if(status != null)
// Setting alert dialog icon
alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
// Showing Alert Message
alertDialog.show();
}
}