How to make a custom place picker for Android

后端 未结 2 1296
滥情空心
滥情空心 2021-02-10 07:14

as I know Google does not allow developers to customize place picker layout ,

so I want to make a place picker like the photo below.

it is from deliveroo app, I

2条回答
  •  一向
    一向 (楼主)
    2021-02-10 07:47

    Make activity for map and call that where you want location:

    
    
    

    java code:

    public class InsertMapsActivity extends FragmentActivity implements LocationListener
    {
    
    private GoogleMap googleMap;
    private LatLng garageLocation;
    Button btnSetGarageLocation;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insert_maps);
        btnSetGarageLocation = (Button) findViewById(R.id.btn_set_garage_location);
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    
        if (status != ConnectionResult.SUCCESS)
        { // Google Play Services are not available
    
            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
            dialog.show();
    
        }
        else
        { // Google Play Services are available
    
            // Getting reference to the SupportMapFragment of activity_main.xml
            SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    
            // Getting GoogleMap object from the fragment
            googleMap = fm.getMap();
    
            // Enabling MyLocation Layer of Google Map
            googleMap.setMyLocationEnabled(true);
    
            // Getting LocationManager object from System Service LOCATION_SERVICE
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    
            // Creating a criteria object to retrieve provider
            Criteria criteria = new Criteria();
    
            // Getting the name of the best provider
            String provider = locationManager.getBestProvider(criteria, true);
    
            // Getting Current Location
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
            {
                // TODO: Consider calling
    
                return;
            }
            Location location = locationManager.getLastKnownLocation(provider);
    
            if(location!=null){
                onLocationChanged(location);
            }
            locationManager.requestLocationUpdates(provider, 100, 0, this);
    
            btnSetGarageLocation.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Intent intent = getIntent();
                    intent.putExtra("location_lat",garageLocation.latitude);
                    intent.putExtra("location_lng",garageLocation.longitude);
                    setResult(RESULT_OK,intent);
                    finish();
                }
            });
        }
    
        googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener()
        {
            @Override
            public void onMapClick(LatLng latLng)
            {
                googleMap.clear();
                googleMap.addMarker(new MarkerOptions().position(latLng).title("Custom location").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
                garageLocation = latLng;
            }
        });
    
    
    }
    
    @Override
    public void onLocationChanged(Location location)
    {
    
        // Getting latitude of the current location
        double latitude = location.getLatitude();
    
        // Getting longitude of the current location
        double longitude = location.getLongitude();
    
        // Creating a LatLng object for the current location
        LatLng latLng = new LatLng(latitude, longitude);
    
        // Showing the current location in Google Map
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    
        // Zoom in the Google Map
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
    
        googleMap.getMaxZoomLevel();
    
        // Setting latitude and longitude in the TextView tv_location
    
    }
    
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
    
    }
    
    @Override
    public void onProviderEnabled(String provider)
    {
    
    }
    
    @Override
    public void onProviderDisabled(String provider)
    {
    
    }
    }
    

    in activity:

    private int MAP = 2;
    
    Intent intent = new Intent(getApplicationContext(),InsertMapsActivity.class);
                    startActivityForResult(intent, MAP);
    

    end of your activity:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
    
        ImageView garageImage = new ImageView(this);
        if (resultCode == Activity.RESULT_OK)
        {
    
        }
    
        if(requestCode == MAP)
        {
            double lat = (double) data.getExtras().get("location_lat");
            double lng = (double) data.getExtras().get("location_lng");
    
    
        }
    }
    

提交回复
热议问题