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 used map api but it is not totally like this photo.
this picker act like place picker api it is customized.
this is my code:
public class Map extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private LatLng markerLatLong;
private static final int REQUEST_CODE_LOCATION_ENABLE = 1;
private static final int PLACE_AUTOCOMPLETE_REQUEST_CODE = 2;
private static String TAG = Map.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
setToolbar();
setFragment();
}
public void setFragment() {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
public void setToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
onSearchPlacesClicked(toolbar);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_LOCATION_ENABLE) {
if (resultCode == RESULT_OK) {
Log.e(TAG, "location enabled");
setLocation();
}
}else if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = PlaceAutocomplete.getPlace(this, data);
Log.e(TAG, "Place: " + place.getName());
setPlace(place.getLatLng());
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
Status status = PlaceAutocomplete.getStatus(this, data);
// TODO: Handle the error.
Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
}
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
setPlace(latLng);
}
});
Button b = (Button) findViewById(R.id.pick_place);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (markerLatLong != null) {
addAddressToPref(-1, markerLatLong.latitude, markerLatLong.longitude);
startActivity(new Intent(Map.this, StoreList.class));
Map.this.finish();
} else {
Toast.makeText(Map.this, R.string.no_place_selected, Toast.LENGTH_SHORT).show();
}
}
});
setLocation();
}
public void addAddressToPref(long id, double latitude, double longitude) {
Singleton.getInstance().setAddressPref(id, latitude, longitude);
}
public void setPlace(final LatLng latLng) {
mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition arg0) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
// Remove listener to prevent position reset on camera move.
mMap.setOnCameraChangeListener(null);
}
});
markerLatLong = latLng;
mMap.clear();
mMap.addMarker(new MarkerOptions().position(latLng).title(getString(R.string.my_location)));
}
public void setLocation() {
AppLocationService gps = new AppLocationService(this);
// check if GPS enabled
if (gps.canGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
getLocation(latitude, longitude);
// \n is for new line
} else {
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
buildAlertMessageNoGps();
}
}
public void getLocation(double latitude, double longitude) {
final CoordinatorLayout rootLayout = (CoordinatorLayout) findViewById(R.id.root_view);
if (latitude != 0 && longitude != 0) {
LatLng latLng = new LatLng(latitude, longitude);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 15);
mMap.animateCamera(cameraUpdate);
} else {
Snackbar.make(rootLayout, getResources().getString(R.string.location_problem),
Snackbar.LENGTH_INDEFINITE).setAction("سعی دوباره", new View.OnClickListener() {
@Override
public void onClick(View view) {
setLocation();
}
}).show();
}
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.turn_on_location_text))
.setCancelable(false)
.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, REQUEST_CODE_LOCATION_ENABLE);
}
})
.setNegativeButton(getString(R.string.exit), new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
dialog.cancel();
Map.this.finish();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
public void onSearchPlacesClicked(Toolbar toolbar) {
ImageView searchPlace = (ImageView) toolbar.findViewById(R.id.search_place);
searchPlace.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
autoCompleteMethod();
}
});
}
public void autoCompleteMethod() {
try {
Intent intent =
new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
.build(this);
startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
} catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}
}
@Override
protected void onResume() {
super.onResume();
if (mMap != null) {
setLocation();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return true;
}
@Override
public void onBackPressed() {
startActivity(new Intent(Map.this, StoreList.class));
Map.this.finish();
}
}