简体   繁体   English

在Android上使用Google Places API获取附近地点的列表

[英]get list of nearby places using google places API on android

I'm using this What is the simplest and most robust way to get the user's current location on Android? 我正在使用这种方法在Android上获取用户当前位置的最简单,最可靠的方法是什么? to get my current location and list nearby restaurants when i click on swith button.It works before i remove the app and install it again. 单击swith按钮可获取我的当前位置并列出附近的餐馆。在删除该应用并重新安装该应用之前,该方法有效。 this is my implementation of gotLocation method 这是我对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();
                }
            }
        });

I got not null LocationResult Object but the gotLocation method was not executed . 我没有得到空的LocationResult对象,但没有执行gotLocation方法。 Can any one tell me where is exactly the problem please? 有人可以告诉我问题出在哪里吗?

this is the code of MyLocationManager Class 这是MyLocationManager类的代码

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

}

The problem was isPermissed returns always false. 问题是isPermissed返回始终为false。 I created a method getLocationPermissions() to grant permissions. 我创建了一个方法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);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM