2

我正在尝试基于 GPS tat 实现警报,如果我在城市 x 并且当我到达那个特定的精确点时,我会在那个地方放置一个针点,它应该给我一个警报或警报我已经实现了一个完整的地图结构,我也有编程用于显示我当前的位置并用于定位。我不知道如何附加源代码,请以这种方式帮助我,我对 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();   
      }
    }
4

1 回答 1

0

您上面的代码不足以显示警报对话框。要在您的代码中应用警报,您必须包含这样的代码形式;

/**
     * 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();
    }
}
于 2013-01-19T15:06:43.107 回答