I followed a tutorial but the codes did not work well. On the tutorial, he only types the BottomSheetDialog(); and doesn't have requirements inside the parenthesis but on my activity, it requires to put something there and I don't know it.
private GoogleMap mMap;
ArrayList<LatLng> MarkerPoints;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Marker mCurrLocationMarker;
LocationRequest mLocationRequest;
double mLatitude;
double mLongitude;
private FirebaseAuth mAuth;
private Button btnShow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Initialize Firebase Auth
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
// Initializing
MarkerPoints = new ArrayList<>();
// 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);
btnShow = (Button) findViewById(R.id.bottomsheet);
btnShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog();
bottomSheetDialog.show(getSupportFragmentManager(),bottomSheetDialog.getTag());
}
});
}
According to the BottomSheetDialog code, only one these constructors are supported:
public BottomSheetDialog(#NonNull Context context) {
...
}
public BottomSheetDialog(#NonNull Context context, #StyleRes int theme) {
...
}
So you are missing a context parameter:
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(context);
Related
I want to display AlertDialogue onMapReady. I implemented the alert dialogue in other class and it working fine.
When I add the alert dialogue code in the MapsActivity which is extending FragmentActivity, it doesn't work as intended to be.
When I run the activity, the AlertDialogue does show up with the button. But, title and message doesn't show
public class MapsActivity extends FragmentActivity implements GoogleMap.OnCameraIdleListener,OnMapReadyCallback {
private GoogleMap mMap;
private Location currentLocation;
private FusedLocationProviderClient fusedLocationProviderClient;
private static final int LOCATION_REQUEST_CODE =101;
private static final String Log_TAG = "Maps_Log_Tag_Results";
private Context ctx = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
if (ActivityCompat.checkSelfPermission(MapsActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MapsActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[] {android.Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST_CODE);
return;
}
fetchLastLocation();
}
private void fetchLastLocation(){
Task<Location> task = fusedLocationProviderClient.getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
if (location != null) {
currentLocation = location;
SupportMapFragment supportMapFragment= (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1);
supportMapFragment.getMapAsync(MapsActivity.this);
}else{
Toast.makeText(MapsActivity.this,"No Location recorded",Toast.LENGTH_SHORT).show();
}
}
});
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMyLocationButtonClickListener(onMyLocationButtonClickListener);
mMap.setOnMyLocationClickListener(onMyLocationClickListener);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setZoomGesturesEnabled(true);
mMap.setMyLocationEnabled(true);
mMap.setOnCameraIdleListener(this);
LatLng latLng = new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,13));
// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Hello");
builder.setMessage("This is alert dialogue");
// add the buttons
builder.setPositiveButton("Awesome", null);
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}
}
I think it has something to do with the FragmentActivity, because in alert dialogue in ActivityCompat work just fine.
Try to change
MainActivity extends FragmentActivity
to be
MainActivity extends AppCompatActivity
I'm building an app, and I run on a problem with Google Maps. I wrote most of the code, but I don't how how to set that when user clicks on item(method onItemClick), in my case I have ListView on Firebase that is showing Tours of concerts, which you can see here:my tours listview to open a specific place and show it on map. For example, user clicks on Anaheim, CA concert and it shows where that place is. Thanks in advance.
public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
private static final int REQUEST_LOCATION_PERMISSION = 10;
private GoogleMap.OnMapClickListener mCustomOnMapClickListener;
private GoogleMap mGoogleMap;
private MapFragment mMapFragment;
#BindView(R.id.lvTours) ListView lvTours;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
this.initialize();
}
public void initialize(){
this.mMapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.fGoogleMap);
this.mMapFragment.getMapAsync(this);
this.mCustomOnMapClickListener = new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
MarkerOptions newMarkerOptions = new MarkerOptions();
newMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.tour));
newMarkerOptions.title("Tour");
newMarkerOptions.snippet("It' was here!");
newMarkerOptions.position(latLng);
mGoogleMap.addMarker(newMarkerOptions);
}
};
}
#Override
public void onMapReady(GoogleMap googleMap) {
this.mGoogleMap = googleMap;
UiSettings uiSettings = this.mGoogleMap.getUiSettings();
uiSettings.setZoomControlsEnabled(true);
uiSettings.setMyLocationButtonEnabled(true);
uiSettings.setZoomGesturesEnabled(true);
this.mGoogleMap.setOnMapClickListener(this.mCustomOnMapClickListener);
}
private boolean hasLocationPermission() {
String LocationPermission = android.Manifest.permission.ACCESS_FINE_LOCATION;
int status = ContextCompat.checkSelfPermission(this, LocationPermission);
if (status == PackageManager.PERMISSION_GRANTED) {
this.mGoogleMap.setMyLocationEnabled(true);
return true;
}
return false;
}
private void requestPermission() {
String[] permission = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
ActivityCompat.requestPermissions(MapActivity.this, permission, REQUEST_LOCATION_PERMISSION);
}
#OnItemClick(R.id.lvTours)
public void onClick()
{
}
}
Since you have your coordinates, you can build a LatLng(latitude, longitude) object
then you can move the camera of your map like this:
build a new camera position using CameraPosition.Builder() and then ask to your mGoogleMap to animate to that position:
CameraPosition position = CameraPosition.builder()
.target(location)
.zoom(16f)
.bearing(0.0f)
.tilt(0.0f)
.build();
mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(position), null)
using that position you can even put a marker on the map:
mGoogleMap.addMarker(new MarkerOptions().position(position)
.title("some title"));
I'm working on a project where I am switching between two map activities.
The structure I'm attempting is something like this:
BaseMapsActivity extends FragmentActivity
MapsActivity1 extends BaseMapsActivity
MapsActivity2 extends BaseMapsActivity
In order to prevent code duplication. I'd like to put my Google API client code in the base Activity and then some map styling code in the base onMapReady() and simply make changes for each sub activity afterwards (adding markers, etc).
My question is, how can I prepare this mapFragment with style and location logic in the base class, and then inflate a frame layout with the fragment in the sub activities to modify it? How would the steps of this process play out? Is this even possible?
public abstract class BaseMapsActivity extends FragmentActivity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;
Location mLastLocation;
Marker mCurrLocationMarker;
public static final String TAG = "BaseMapsActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.base_map_activity);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
try {
// Customise the styling of the base map using a JSON object defined
// in a raw resource file.
boolean success = googleMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(
this, R.raw.style_json));
if (!success) {
Log.e(TAG, "Style parsing failed.");
}
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Can't find style. Error: ", e);
}
mMap.moveCamera(CameraUpdateFactory.zoomTo(14));
//Initialize Google Play Services
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
} else {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
}
and for the sub activity 1...
public class MapsActivity1 extends BaseMapsActivity implements LocationListener, View.OnClickListener {
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
private GoogleMap mMap;
private FirebaseAuth firebaseAuth;
private FirebaseUser user;
private DatabaseReference databaseRef;
private static final String TAG = "MapsActivity1";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onMapReady(mMap);
and the second activity...
public class MapsActivity2 extends BaseMapsActivity implements OnMapReadyCallback,
View.OnClickListener, OnProgressListener {
// GoogleMap mMap;
private static GoogleMap mMap;
SeekBar colorSeek;
private double longitude;
private double latitude;
private Bundle mapSpots;
private Button backToMap;
private SeekBar.OnSeekBarChangeListener seekBarListener;
private LatLng user;
private static final String TAG = "MapsActivity2";
private static final int HUE_MAX = 360;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
super.onMapReady(mMap);
Thanks T
Needed to call super.onMapReady(Bundle savedInstanceState) not in the onMapReady override in the child class, so instead of:
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
super.onMapReady(mMap);
It needs to be this:
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
super.onMapReady(mMap);
#Override
protected void onMapReady(GoogleMap googleMap){
mMap = googleMap;
super.onMapReady(mMap);
I am getting a java.lang.NullPointerException on the line map.setOnMyLocationChangeListener(myLocationChangeListener);. Strangely, this exception arises while running it some devices while in some it doesn't. How do I solve this?
public class MyMap extends ActionBarActivity implements
RoutingListener, OnMapReadyCallback,
SensorEventListener {
// private GoogleApiClient mGoogleApiClient;
protected GoogleMap map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_map);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.map, mapFragment).commit();
}
mapFragment.getMapAsync(this);
map = mapFragment.getMap();
map.setOnMyLocationChangeListener(myLocationChangeListener);
}
EDIT:
#Override
public void onMapReady(GoogleMap mMap) {
map = mMap;
mUiSettings = map.getUiSettings();
mUiSettings.setZoomControlsEnabled(true);
mUiSettings.setCompassEnabled(true);
updateMyLocation();
}
By calling mapFragment.getMapAsync(this);, I suppose you have to override this method public void onMapReady(GoogleMap googleMap). Set your mMap = googleMap there and set all other listener there. Keep in mind that your Map object needs some time to be initialised, that's why they give you getMapAsync method.
Editted:
Please remove this from your onCreated
map = mapFragment.getMap();
map.setOnMyLocationChangeListener(myLocationChangeListener);
and put them map.setOnMyLocationChangeListener(myLocationChangeListener); inside your onMapReady() after this line map = mMap. Please acknowledge when your map is initialized.
When you work with GoogleMap then it takes time to initialize. Sometimes it takes longer time in some devices than others.
To solve this issue, you should initialize the GoogleMap object with some delay. You can follow the below strategy:
private Handler handler = new Handler();
private Runnable mapChecker = new Runnable() {
#Override
public void run() {
try {
if (mapFragment.getMap() != null) {
map = mapFragment.getMap();
map.setOnMyLocationChangeListener(myLocationChangeListener);
return;
}
} catch (Exception e) {
e.printStackTrace();
}
handler.postDelayed(mapChecker, 500);
}
};
Then from onCreate(),
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_map);
..........
handler.postDelayed(mapChecker, 500);
..........
}
Instantiating the Geocoder causes my app to crash with NullPointerException.
This is my code (only single Activity):
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private final int MAX_RESULTS = 1;
//if I comment out the following line, app will not crash
private Geocoder geocoder = new Geocoder(getApplicationContext()); //line 27
private GoogleMap gmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap map) {
setUpMap(map);
gmap = map;
}
...
}
I have tried new Geocoder(getBaseContext()), new Geocoder(this) and new Geocoder(MapsActivity.this) but they all crash. How to fix this?
logcat:
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
at android.location.GeocoderParams.<init>(GeocoderParams.java:50)
at android.location.Geocoder.<init>(Geocoder.java:83)
at android.location.Geocoder.<init>(Geocoder.java:95)
at rubiks.cres.MapsActivity.<init>(MapsActivity.java:27)
Move the initialization in onCreate :
private Geocoder geocoder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
geocoder = new Geocoder(this);
SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
getApplicationContext() is likely to return null if the Activity creation is not finished.