4

私のアプリには、関連する緯度経度の位置を持つラウンド 9 の場所のリストがあり、最終的な目標は、場所のいずれかから約 0.5 マイル以内にあるかどうかをユーザーに通知することです。

これを行う最良の方法は何ですか?これは私がこれまでに持っているものです。

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);
    }

それは機能しますが、ユーザーが近くにあるという通知をキャンセルすると、場所が次に更新されたときに再びポップアップするので、面倒です. 適切な代替手段として、requestLocationUpdates に 30 分などの大きな minTime を設定することはできますか?

4

2 に答える 2

7

これを行う最良の方法は何ですか?

Play サービス SDK のでジオフェンスをLocationClient使用するか、 で近接アラートをLocationManager試してください。

于 2013-09-18T15:25:53.607 に答える