Pregunta

In my app I have a list of round 9 places with the associative lat long positions, and the end goal is to notify the user if they are within about half a mile of any of the locations.

What would be the best way to do this? This is what I have so far.

distanceFromListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            List<JSONObject> centres = sortVenuesByNearest(location);
            Log.d("FUApp", "distanceFromListener setting location");
            haveLocation = true;
            theLocation = location;

            float accuracy = location.getAccuracy();
            for (JSONObject centre : centres) {
                try {
                    Double distance = Double.valueOf(centre.getString("distanceTo"));
                    String name = centre.getString("name");
                    Integer venueID = Integer.valueOf(centre.getString("id"));
                    if (distance < 804.672) {
                        Bundle mBundle = new Bundle();
                        Double miles = (distance * 0.00062137);
                        DecimalFormat df = new DecimalFormat("#.##");
                        String miles2 = df.format(miles);
                        mBundle.putString(ExtraKeyCentreName, name);
                        mBundle.putString(ExtraKeyCentreDist, miles2 + " miles away");
                        mBundle.putString(ExtraKeyCentreJSON, centre.toString());
                        mBundle.putString(ExtraKeyCentrePostcode, "none");

                        sendNotification(ActivityCentrePage.class, "Visit " + name, "You're half a mile from " + name + ". Why not come for a visit?", R.drawable.ic_launcher, venueID, mBundle);
                    } else {
                        cancelNotification(venueID);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }
    };

    Boolean notify_if_close = sp.getBoolean(SettingsNotifyIfClose, true);
    if (notify_if_close) {
        locationService.requestLocationUpdates(LocationManager.GPS_PROVIDER, 50, 100, distanceFromListener);
    }

That works, but if the user cancels a notification saying there close, when the location is next updated it will pop up again, which will get annoying. Would a suitable alternative be put a big minTime, say half an hour on the requestLocationUpdates?

¿Fue útil?

Solución

Otros consejos

Use a service for tracking coordinates...

try this link, it may help u..

http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top