使用 google places API on android 获取附近地点的列表

get list of nearby places using google places API on android

我正在使用这个 What is the simplest and most robust way to get the user's current location on Android? 来获取我的当前位置并在我点击开关时列出附近的餐馆 button.It 在我删除应用程序并重新安装之前工作。 这是我对 gotLocation 方法的实现

switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    MyLocationManager.LocationResult locationResult= new MyLocationManager.LocationResult() {
                        @Override
                        public void gotLocation(final Location location) {
                            //Got the location!


                                    currentLocation = location;
                                    browseNearbyBusinessesList(currentLocation);

                        }
                        };
                    MyLocationManager myLocationManager = new MyLocationManager();
                    myLocationManager.getLocation(getBaseContext(), locationResult);
                }else {
                    searchDefaultList();
                }
            }
        });

我得到了不为空的 LocationResult 对象,但没有执行 gotLocation 方法。谁能告诉我问题到底出在哪里?

这是 MyLocationManager 的代码 Class

public class MyLocationManager {
    public LocationResult locationResult;
    public Context context;
    Timer timer1;
    LocationManager lm;
    boolean gps_enabled = false;
    boolean network_enabled = false;
    LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerNetwork);
        }

        public void onProviderDisabled(String provider) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };
    LocationListener locationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerGps);
        }

        public void onProviderDisabled(String provider) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };
    private boolean isPermessed = false;

    public MyLocationManager() {

    }

    public MyLocationManager(boolean isPermessed) {
        this.isPermessed = isPermessed;
    }

    public boolean getLocation(Context context, LocationResult result) {
        //I use LocationResult callback class to pass location value from MyLocation to user code.
        this.context = context;
        locationResult = result;
        if (lm == null)
            lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        //exceptions will be thrown if provider is not permitted.
        try {
            gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception ex) {
        }
        try {
            network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ex) {
        }

        //don't start listeners if no provider is enabled
        if (!gps_enabled && !network_enabled)
            return false;

        if (gps_enabled) {
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                isPermessed = false;
                return false;

            } else {
                isPermessed = true;
            }
            if (isPermessed)
                lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
        }
        if (network_enabled) {
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                isPermessed = false;
                return false;
            } else {
                isPermessed = true;
            }
            if (isPermessed)
                lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
        }

        timer1 = new Timer();
        timer1.schedule(new GetLastLocation(), 4000);
        return true;
    }

    public static abstract class LocationResult {
        public abstract void gotLocation(final Location location);
    }

    class GetLastLocation extends TimerTask {
        @Override
        public void run() {
            lm.removeUpdates(locationListenerGps);
            lm.removeUpdates(locationListenerNetwork);

            Location net_loc = null, gps_loc = null;
            if (gps_enabled) {

                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    isPermessed = false;
                    return;
                } else {
                    isPermessed = true;
                }
                if (isPermessed)
                    gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            }
            if (network_enabled) {
                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    isPermessed = false;
                    return;
                } else {
                    isPermessed = true;
                }
                if (isPermessed)
                    net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            }
            System.out.println(isPermessed);
            //if there are both values use the latest one
            if (gps_loc != null && net_loc != null) {
                if (gps_loc.getTime() > net_loc.getTime())
                    locationResult.gotLocation(gps_loc);
                else
                    locationResult.gotLocation(net_loc);
                return;
            }

            if (gps_loc != null) {
                locationResult.gotLocation(gps_loc);
                return;
            }
            if (net_loc != null) {
                locationResult.gotLocation(net_loc);
                return;
            }
            locationResult.gotLocation(null);
        }
    }

}

问题是 isPermissed returns 总是错误的。我创建了一个方法 getLocationPermissions() 来授予权限。

        private final static String FINE_LOCATION= Manifest.permission.ACCESS_FINE_LOCATION;
        private final static String COARSE_LOCATION= Manifest.permission.ACCESS_COARSE_LOCATION;
        private final static int LOCATION_PERMISSION_REQUEST_CODE=1234;
                   --------------------------------------

        if (gps_enabled) {
                    getLocationPermission();
                    if (((ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) && (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED))) {

                        isPermessed = true;
                        Log.i("isPermessed = ", isPermessed+"");

                    } else {
                        isPermessed = false;
                        Log.i("isPermessed = ", isPermessed+"");
                        return false;


                    }

                     --------------------------------------
 private void getLocationPermission() {
        String[] permissions = {android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.ACCESS_COARSE_LOCATION};

        if (ContextCompat.checkSelfPermission(context,
                FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            if (ContextCompat.checkSelfPermission(context,
                    COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            } else {
                ActivityCompat.requestPermissions((Activity) context,
                        permissions,
                        LOCATION_PERMISSION_REQUEST_CODE);
            }
        } else {
            ActivityCompat.requestPermissions((Activity) context,
                    permissions,
                    LOCATION_PERMISSION_REQUEST_CODE);
        }
    }