How to make a custom place picker for Android

后端 未结 2 1304
滥情空心
滥情空心 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:43

    This code works like a charm. However it seems that there are new updates to this as Google made new releases. Key differences are the following:

    1. OnMapReadyCallback must be implemented:

      public class InsertMapsActivity extends FragmentActivity implements LocationListener, OnMapReadyCallback

      @Override public void onMapReady(GoogleMap googleMap) { this.googleMap = googleMap;

      }

    2. This code will no longer work:

      // 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();
      

    Instead use this, which calls the callback in #1.

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    
    1. References to the googleMap object need be placed in the OnMapReady callback to ensure that the object is initially assigned else null pointer errors would occur:

      @Override 
      public void onMapReady(GoogleMap googleMap) {
              this.googleMap = googleMap;
              // Enabling MyLocation Layer of Google Map
              this.googleMap.setMyLocationEnabled(true);
      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;
          }
      });
      }
      
    0 讨论(0)
  • 2021-02-10 07:47

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

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".Activity.InsertMapsActivity">
    
    <Button
        android:layout_width="wrap_content"
        android:id="@+id/btn_set_garage_location"
        android:text="set location"
        android:layout_height="wrap_content"/>
    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    

    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");
    
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题