如何跟踪GPS如果到达特定位置或大约20米?

时间:2014-04-05 04:31:09

标签: android mysql gps

我使用以下代码跟踪gps,但如何获得通知如果gps跟踪器到达该位置...

Capture.java

public class Capture extends Activity implements LocationListener{


    Button btnNWShowLocation,btnsave;
    TextView etlat,etlng,alreadyposted,t3,t4,t5;
    EditText etplace;
    String state1,state2;
    protected String v1, v2, v3;
    JSONParser jsonParser = new JSONParser();
    JSONArray jsonarray;
    ArrayList<HashMap<String,String>> arraylist;
    JSONObject jsonobject;
    String TAG_SUCCESS="success";

    double latitude,longitude;

    boolean isGPSEnabled = false;
    boolean isNetworkEnabled = false;
    double lat1,lng1;
    String lat2,lng2;
    String providernet,speed1,accuracy1;
    float speed,accuracy;
    Location location;

    public LocationManager locationManager;
    String provider;

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ThreadPolicy tp = ThreadPolicy.LAX;
        StrictMode.setThreadPolicy(tp);

        btnsave = (Button) findViewById(R.id.buttonsave);
        etlat=(TextView)findViewById(R.id.textlatitude);
        etlng=(TextView)findViewById(R.id.textlongitude);
        alreadyposted=(TextView)findViewById(R.id.alreadyposted);
        etplace=(EditText)findViewById(R.id.editplace);
        t3=(TextView)findViewById(R.id.provider);
        t4=(TextView)findViewById(R.id.speed);
        t5=(TextView)findViewById(R.id.accuracyset);

        alreadyposted.setVisibility(View.INVISIBLE);

        locationManager = (LocationManager)Capture.this.getSystemService(Context.LOCATION_SERVICE);

        // Define the criteria how to select the locatioin provider -> use
        // default
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setCostAllowed(false);
        provider = locationManager.getBestProvider(criteria,false);
        Location location = locationManager.getLastKnownLocation(provider);

        //Initialize the location fields
        if (location!= null){

            onLocationChanged(location);

            latitude = location.getLatitude();
            longitude =location.getLongitude();

        } else {
            Toast.makeText(getApplicationContext(), "Not available", Toast.LENGTH_SHORT).show();
        }

        btnsave.setOnClickListener(new View.OnClickListener() {

            protected ArrayList<NameValuePair> parameters;

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                parameters = new ArrayList<NameValuePair>();

                v1 = etlat.getText().toString();
                v2 = etlng.getText().toString();
                v3 = etplace.getText().toString();

                arraylist = new ArrayList<HashMap<String, String>>();
                // Retrieve JSON Objects from the given URL address
                jsonobject = JSONfunctions
                        .getJSONfromURL("http://placementreadyguarantee.com/android/gps_tracking/Jsoncheckcapture.php?latitude="+v1+"&longitude="+v2);

                try {
                    // Locate the array name in JSON
                    int success = jsonobject.getInt(TAG_SUCCESS);

                    if(success==1){

                        alreadyposted.setVisibility(View.VISIBLE);

                    }
                    else{

                        NameValuePair latitudejson = new BasicNameValuePair("latitude",v1);
                        NameValuePair longitudejson = new BasicNameValuePair("longitude",v2);
                        NameValuePair placejson = new BasicNameValuePair("place",v3);

                        parameters.add(latitudejson);
                        parameters.add(longitudejson);
                        parameters.add(placejson);

                        try {
                            JSONObject json =jsonParser.makeHttpRequest("http://placementreadyguarantee.com/android/gps_tracking/captureupload.php?latitude="
                                    +URLEncoder.encode(v1,"UTF-8")
                                    +"&longitude="
                                    +URLEncoder.encode(v2,"UTF-8")
                                    +"&place="
                                    +URLEncoder.encode(v3,"UTF-8"),
                                    "POST",parameters);

                        } catch (UnsupportedEncodingException e){
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        alreadyposted.setVisibility(View.INVISIBLE);

                    }
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        });

    }

    /*Request updates at startup */
    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider,400,1,this);
        //getting gps
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if(isGPSEnabled){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,400,1,this);
        }
        else if(isNetworkEnabled){
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,400,1,this);
        }

    }

    /* Remove the locationlistener updates when Activity is paused */
    @Override
    protected void onPause(){
        super.onPause();
        //locationManager.removeUpdates(this);
        locationManager.requestLocationUpdates(provider,400,1,this);
        //getting gps
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if(isGPSEnabled){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,400,1,this);
        }
        else if(isNetworkEnabled){
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,400,1,this);
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

        lat1=location.getLatitude();
        lng1=location.getLongitude();
        providernet=location.getProvider();
        speed=location.getSpeed();
        accuracy=location.getAccuracy();

        accuracy1=Float.toString(accuracy);
        speed1=Float.toString(speed);
        lat2=Double.toString(lat1);
        lng2=Double.toString(lng1);

        etlat.setText(lat2);
        etlng.setText(lng2);
        t3.setText(providernet);
        t4.setText(speed1);
        t5.setText(accuracy1);

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Disabled provider " + provider,
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Enabled new provider " + provider,
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

在此我使用json将经度和经度存储在mysql数据库中(这是在我的服务器空间中)。

通过点击按钮,它在数据库中保存了当前的经度和纬度。

现在我需要做更多,如果设备到达已经保存的地方或大约10米或20个流星它发送通知,或者存储在数据库中,就像我需要做的任何事情一样。

提前致谢。 :)

0 个答案:

没有答案