1

How to show all the restaurant(highlighted),or any other public place at near by user and also show all the nearby restaurant or public places in a list view.with feature user can define range i.e. show restaurants in range of 100m,500m,in this city etc.

i used this code to know the current location of user in map:-->

package com.example.locationandmapdemo;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.nfc.Tag;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

LocationManager locationManager;
LocationListener locationListener;

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] 
grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == 1) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == 
PackageManager.PERMISSION_GRANTED) {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, 
locationListener);
                mMap.setMyLocationEnabled(true);
                
            }
        }
    }
}






@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    msearchtext=(EditText)findViewById(R.id.input_serach);



    
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
 }



 


 @Override
 public void onMapReady(GoogleMap googleMap) {
    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            mMap.clear();//this will clear the map before we add something.
            LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
            mMap.addMarker(new MarkerOptions().position(userLocation).title("Your Location"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));

            
            Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
            try{
                List<Address> listaddresses = 
geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);
  match for original address, obiusly that is address itself

                if(listaddresses !=null && listaddresses.size() >0){
                    Log.i("placeInfo",listaddresses.get(0).toString());//get(0) means to get the item 
from ArrayList at index 0.
               String address ="";
                    if(listaddresses.get(0).getAdminArea() != null){
                     address +=listaddresses.get(0).getAdminArea() + " ";

                    }
                    if(listaddresses.get(0).getLocality() != null){
                        address +=listaddresses.get(0).getLocality() + " ";
                    }

                    if(listaddresses.get(0).getThoroughfare() != null){
                        address +=listaddresses.get(0).getThoroughfare() + " ";
                    }
                    Toast.makeText(MapsActivity.this,address,Toast.LENGTH_SHORT).show();
                    Log.i("Place Location",address);


                }
            }catch (Exception 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) {

        }
    };

    if (Build.VERSION.SDK_INT < 23) {

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    } else {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != 
PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[] 
{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        } else {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, 
locationListener);
            Location lastKnownLocation = 
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            init();

            mMap.clear();
            LatLng userLocation = new LatLng(lastKnownLocation.getLatitude(), 
lastKnownLocation.getLongitude());
            mMap.addMarker(new MarkerOptions().position(userLocation).title("Your Location"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));
    }
        
    }
}



}

I want to add the feature that when user opens the map then it shows the all nearby restaurant.and add features like in what range.

Any one can please help, how it can done.any code,documentation etc will work.

ThankYOu for reading.

Shubham
  • 25
  • 5
  • Since you have already detected the user's location, you can now use the Places API - Nearby Search service to show a certain place type (like restaurant, school, etc.) within the radius (max. of 50,000 meters) that you'll specify in the request. You can learn more about this service by going to [this documentation](https://developers.google.com/places/web-service/search#PlaceSearchRequests). You might also find this post helpful to learn how you can implement this service in an Android app. Please see [I'm trying to search nearby places...](https://stackoverflow.com/q/30161395/10758035) – annkylah Aug 12 '20 at 03:23

0 Answers0