How to make a custom place picker for Android

后端 未结 2 1299
滥情空心
滥情空心 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;
          }
      });
      }
      

提交回复
热议问题