1

I found some solution. But something was always missing. For example place id here

How can I get place details from Google Places API for Android?

I work on my app and I need place details.

Here my methods

/**
 *
 */
private void geoLocate() {
    Log.d(TAG, "geoLocate: geolocating");

    String searchString = mSearchText.getText().toString();

    Geocoder geocoder = new Geocoder(MapsActivity.this);
    List<Address> list = new ArrayList<>();
    try {
        list = geocoder.getFromLocationName(searchString, 1);
    } catch (IOException e) {
        Log.e(TAG, "geoLocate: IOException: " + e.getMessage());
    }

    if (list.size() > 0) {
        Address address = list.get(0);

        Log.d(TAG, "geoLocate: found a location: " + address.toString());
        //Toast.makeText(this, address.toString(), Toast.LENGTH_SHORT).show();

        moveCamera(address, DEFAULT_ZOOM);
    }
}

/**
 *
 */
private void getDeviceLocation() {
    Log.d(TAG, "getDeviceLocation: getting the devices current location");

    mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

    try {
        if (mLocationPermissionsGranted) {

            final Task location = mFusedLocationProviderClient.getLastLocation();
            location.addOnCompleteListener(new OnCompleteListener() {
                @Override
                public void onComplete(@NonNull Task task) {
                    if (task.isSuccessful() && task.getResult() != null) {
                        Log.d(TAG, "onComplete: found location!");
                        Location currentLocation = (Location) task.getResult();

                        Geocoder geocoder = new Geocoder(MapsActivity.this);
                        List<Address> list = new ArrayList<>();
                        try {
                            list = geocoder.getFromLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), 1);
                        } catch (IOException e) {
                            Log.e(TAG, "geoLocate: IOException: " + e.getMessage());
                        }

                        if (list.size() > 0) {
                            Address address = list.get(0);

                            Log.d(TAG, "geoLocate: found a location: " + address.toString());
                            //Toast.makeText(this, address.toString(), Toast.LENGTH_SHORT).show();

                            moveCamera(address, DEFAULT_ZOOM);
                        }

                    } else {
                        Log.d(TAG, "onComplete: current location is null");
                        Toast.makeText(MapsActivity.this, "unable to get current location", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    } catch (SecurityException e) {
        Log.e(TAG, "getDeviceLocation: SecurityException: " + e.getMessage());
    }
}

/**
 *
 * @param address
 * @param zoom
 */
private void moveCamera(Address address, float zoom) {

    LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());

    Log.d(TAG, "moveCamera: moving the camera to: lat: " + latLng.latitude + ", lng: " + latLng.longitude);

    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));

    mMap.clear();

    mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter(MapsActivity.this));

    if(address != null) {
        try {
            String snippet =
                    "Address: " + address.getAddressLine(0) + "\n" +
                    "Phone Number: " + address.getPhone() + "\n" +
                    "Website: " + address.getUrl() + "\n" +
                    "Price Rating: ";

            MarkerOptions options = new MarkerOptions()
                    .position(latLng)
                    .title(address.getAddressLine(0))
                    .snippet(snippet);

            mMarker = mMap.addMarker(options);

            mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(Marker marker) {
                    Log.d(TAG, "moveCamera: default marker clicked ");
                    return false;
                }
            });

        } catch (NullPointerException e) {
            Log.e(TAG, "moveCamera: NullPointerExepcion: " + e.getMessage());
        }
    } else {
        mMap.addMarker(new MarkerOptions().position(latLng));
    }

    hideSoftKeyboard();
}

"address" has no enough information. can anybody help me

Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39
bendeniz61
  • 11
  • 1

0 Answers0