Android app - Google maps location markers disappearing from Map fragment - java

I am very new to Android development. This is my first attempt at creating an app. I have created a tabbed navigation app, with 4 fragments to navigate between: Home, Search, Map and Account fragments from the MainActivity. I have used a SupportMapFragment within my MapsFragment to display the map. Upon visiting the MapsFragment the map displays and zooms in on my Current location and highlights certain important locations, retrieved from a Firebase instance, with markers.
Everything works as expected when I first launch the app and visit the Map Fragment. I am zoomed into my current location and I see all required markers. The problem arises when I switch to a different fragment from that point. When I navigate to the Home, Search or Account fragment and then return to the Map fragment, none of the location markers are visible anymore. I can still see my current location and get zoomed into it on opening the Map fragment but the other markers disappear.
I originally thought it was to do with the fragment being recreated everytime I navigate to another fragment as I use .replace(container, fragment) to change the fragment based on which button was clicked from the bottom navigation bar. But I observed that the Map fragment would load my current location and zoom into it correctly, so the loadMap() function gets executed but somehow the markers don't appear. If i restart the app, the markers behave correctly the first time I open the Map fragment.
Any help on how to keep the markers showing on the Map when I return to it after navigating to other fragments will be greatly appreciated. Thank you!
Here is a preview of my MapsFragment.java:
public class MapsFragment extends Fragment {
private SupportMapFragment supportMapFragment;
private AutocompleteSupportFragment autocompleteSupportFragment;
private FusedLocationProviderClient client;
private Geocoder geocoder;
private ArrayList<String> mPostCodes;
private ArrayList<Marker> searchMarker;
private DatabaseReference locationRef;
private DatabaseReference businessRef;
private String apiKey;
public MapsFragment() {
// Required empty public constructor
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Initialize Map fragment
supportMapFragment = (SupportMapFragment)
getChildFragmentManager().findFragmentById(R.id.google_map);
// initialize Places client
apiKey = getString(R.string.map_key);
if(!Places.isInitialized()){
Places.initialize(requireActivity(), apiKey);
}
PlacesClient placesClient = Places.createClient(requireActivity());
// Initialize AutoComplete search bar and set autocomplete parameters
autocompleteSupportFragment = (AutocompleteSupportFragment)
getChildFragmentManager().findFragmentById(R.id.autocomplete_fragment);
autocompleteSupportFragment.setTypeFilter(TypeFilter.ADDRESS);
autocompleteSupportFragment.setLocationBias(RectangularBounds.newInstance(
new LatLng(55.836229, -4.252612),
new LatLng(55.897463, -4.325364)));
autocompleteSupportFragment.setCountries("UK");
autocompleteSupportFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG));
// initialize search marker list
searchMarker = new ArrayList<Marker>();
// Initialize client to get user's last location on device
client = LocationServices.getFusedLocationProviderClient(requireActivity());
// Initialize geocoder to convert business postcodes to latlng coordinates
mPostCodes = new ArrayList<>();
geocoder = new Geocoder(requireActivity());
// Get business locations
locationRef = FirebaseDatabase.getInstance().getReference("Business Locations");
locationRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
if(snapshot.exists()){
for(DataSnapshot locationSnapshot : snapshot.getChildren()){
BusinessLocation location = locationSnapshot.getValue(BusinessLocation.class);
mPostCodes.add(location.getPostCode());
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
Log.i("Location error", "Error retrieving location: ", error.toException().getCause());
}
});
// initialize reference to business table
businessRef = FirebaseDatabase.getInstance().getReference("Businesses");
// render the map
loadMap();
}
private void loadMap() {
if (ActivityCompat.checkSelfPermission(requireActivity(),
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Task<Location> task = client.getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
if(location != null){
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(#NonNull GoogleMap googleMap) {
// autocomplete place search
autocompleteSupportFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
#Override
public void onError(#NonNull Status status) {
Log.i("AutoComplete error", "error status: " + status);
}
#Override
public void onPlaceSelected(#NonNull Place place) {
LatLng placeLatLng = place.getLatLng();
MarkerOptions placeOptions = new MarkerOptions().position(placeLatLng)
.title("search")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
if(!searchMarker.isEmpty()){
Marker searchedMarker = searchMarker.get(0);
searchMarker.remove(searchedMarker);
searchedMarker.remove();
}
final Marker marker = googleMap.addMarker(placeOptions);
searchMarker.add(marker);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(placeLatLng, 10));
}
});
LatLng myLatLng = new LatLng(location.getLatitude(),
location.getLongitude());
// show current location
if (ActivityCompat.checkSelfPermission(requireActivity(),
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
}
// show markers for all businesses on database
for(String code : mPostCodes){
try{
Address address = geocoder.getFromLocationName(code, 1).get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
MarkerOptions options = new MarkerOptions().position(latLng);
googleMap.addMarker(options);
}catch (IOException e){
Toast.makeText(requireActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myLatLng, 10));
}
});
}
}
});
}else {
ActivityCompat.requestPermissions(requireActivity(),
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 44);
}
}
private ActivityResultLauncher<String> mPermissionResult = registerForActivityResult(
new ActivityResultContracts.RequestPermission(),
result -> {
if(result){
loadMap();
}
}
);
}
Here is my fragment_maps.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".fragments.MapsFragment">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/google_map"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
<fragment
android:id="#+id/autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"/>
</RelativeLayout>
And here is my MainActivity.kt file:
class MainActivity : AppCompatActivity() {
private val homeFragment = HomeFragment()
private val searchFragment = SearchFragment()
private val mapFragment = MapsFragment()
private val accountFragment = AccountFragment()
private val guestAccountFragment = GuestAccountFragment()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
replaceFragment(homeFragment)
bottom_navigation.setOnItemSelectedListener {
when (it.itemId) {
R.id.id_home -> replaceFragment(homeFragment)
R.id.id_search -> replaceFragment(searchFragment)
R.id.id_map -> replaceFragment(mapFragment)
R.id.id_account -> {
if(FirebaseAuth.getInstance().currentUser == null){
replaceFragment(guestAccountFragment)
}else {
replaceFragment(accountFragment)
}
}
}
true
}
}
private fun replaceFragment(fragment : Fragment){
if(fragment != null){
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_container, fragment)
transaction.commit()
}
}
}

It happening beacuse your map fragment is "dying" after replacing on new fragment. Try this solution:
First of all you need add in your main activity all required fragments which can be called:
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container_view, fragment1)
.add(R.id.fragment_container_view, fragment2)
.add(R.id.fragment_container_view, fragment3)
.commit();
After adding you can change them by calling this code:
getSupportFragmentManager().beginTransaction()
.show(fragment2)
.hide(fragment1)
.commit();
Code was writen on Java but I think there is no problem

Related

Data resets when bottom navigation views are clicked

I have a bottom navigation view on my weather app containing 3 panels(Today, hourly & Daily). My activity hosts 3 fragments for the 3 panels. Currently, if I search for any city on the today fragment, it gives the data for such cities. Now the problem is that if I click on any bottom nav view(down), it resets the data displayed on the today fragment.
Here is an illustration of the issue:
Data is displayed after a city is searched(the part with a red tick):
Data goes on reset after clicking these bottom nav views(the part with a red tick):
I want the data to remain intact irrespective of clicking those nav views.
I tried using https://stackoverflow.com/a/60201555/16020235 suggestion. But it failed with this
exception:
java.lang.IllegalArgumentException: No view found for id 0x7f0a0116 (com.viz.lightweatherforecast:id/my_nav) for fragment ThirdFragment{90bc0de} (8e129d17-010d-41dc-9311-82e273b4e522 id=0x7f0a0116 tag=3)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:513)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3134)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3068)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:501)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:210)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)
at android.app.Activity.performStart(Activity.java:7157)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3037)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1861)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6819)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:912)
I/weatherforecas: Compiler allocated 4MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
I find it hard to implement his code and the remaining suggestions on that question are written in kotlin.
Please how can I resolve this?
Here are my codes:
my_nav.xml(navigation layout):
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/my_nav"
app:startDestination="#id/firstFragment">
<fragment
android:id="#+id/firstFragment"
android:name="com.viz.lightweatherforecast.FirstFragment"
android:label="fragment_first"
tools:layout="#layout/fragment_first" />
<fragment
android:id="#+id/secondFragment"
android:name="com.viz.lightweatherforecast.SecondFragment"
android:label="fragment_second"
tools:layout="#layout/fragment_second" />
<fragment
android:id="#+id/thirdFragment"
android:name="com.viz.lightweatherforecast.ThirdFragment"
android:label="fragment_third"
tools:layout="#layout/fragment_third" />
</navigation>
activity_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layout"
android:background="#drawable/dubai"
tools:context=".Activity.HomeActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_menu" />
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="409dp"
android:layout_height="599dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:navGraph="#navigation/my_nav"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
HomeActivity:
public class HomeActivity extends AppCompatActivity {
// Last update time, click sound, search button, search panel.
TextView time_field;
MediaPlayer player;
ImageView Search;
EditText textfield;
// For scheduling background image change(using constraint layout, start counting from dubai, down to statue of liberty.
ConstraintLayout constraintLayout;
public static int count=0;
int[] drawable =new int[]{R.drawable.dubai,R.drawable.central_bank_of_nigeria,R.drawable.eiffel_tower,R.drawable.hong_kong,R.drawable.statue_of_liberty};
Timer _t;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// use home activity layout.
time_field = findViewById(R.id.textView9);
Search = findViewById(R.id.imageView4);
textfield = findViewById(R.id.textfield);
// find the id's of specific variables.
BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
// host 3 fragments along with bottom navigation.
final NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
assert navHostFragment != null;
final NavController navController = navHostFragment.getNavController();
NavigationUI.setupWithNavController(bottomNavigationView, navController);
// For scheduling background image change
constraintLayout = findViewById(R.id.layout);
constraintLayout.setBackgroundResource(R.drawable.dubai);
_t = new Timer();
_t.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
// run on ui thread
runOnUiThread(() -> {
if (count < drawable.length) {
constraintLayout.setBackgroundResource(drawable[count]);
count = (count + 1) % drawable.length;
}
});
}
}, 5000, 5000);
Search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// make click sound when search button is clicked.
player = MediaPlayer.create(HomeActivity.this, R.raw.click);
player.start();
getWeatherData(textfield.getText().toString().trim());
// make use of some fragment's data
FirstFragment firstFragment = (FirstFragment) navHostFragment.getChildFragmentManager().getFragments().get(0);
firstFragment.getWeatherData(textfield.getText().toString().trim());
}
});
}
});
}
}
EDIT
Fragment class:
public class FirstFragment extends Fragment {
// User current time, current temperature, current condition, sunrise, sunset, temperature, pressure, humidity, wind_speed, visibility, clouds
TextView current_temp, current_output, rise_time, set_time, temp_out, Press_out, Humid_out, Ws_out, Visi_out, Cloud_out;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public FirstFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment SecondFragment.
*/
// TODO: Rename and change types and number of parameters
public static FirstFragment newInstance(String param1, String param2) {
FirstFragment fragment = new FirstFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_first, container, false);
// For displaying weather data
current_temp = rootView.findViewById(R.id.textView10);
current_output = rootView.findViewById(R.id.textView11);
rise_time = rootView.findViewById(R.id.textView25);
set_time = rootView.findViewById(R.id.textView26);
temp_out = rootView.findViewById(R.id.textView28);
Press_out = rootView.findViewById(R.id.textView29);
Humid_out = rootView.findViewById(R.id.textView30);
Ws_out = rootView.findViewById(R.id.textView33);
Visi_out = rootView.findViewById(R.id.textView34);
Cloud_out = rootView.findViewById(R.id.textView35);
return rootView;
}
public void getWeatherData(String name) {
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<Example> call = apiInterface.getWeatherData(name);
call.enqueue(new Callback<Example>() {
#Override
public void onResponse(#NonNull Call<Example> call, #NonNull Response<Example> response) {
try {
assert response.body() != null;
current_temp.setVisibility(View.VISIBLE);
current_temp.setText(response.body().getMain().getTemp() + " ℃");
current_output.setVisibility(View.VISIBLE);
current_output.setText(response.body().getWeather().get(0).getDescription());
rise_time.setVisibility(View.VISIBLE);
rise_time.setText(response.body().getSys().getSunrise() + " ");
set_time.setVisibility(View.VISIBLE);
set_time.setText(response.body().getSys().getSunset() + " ");
temp_out.setVisibility(View.VISIBLE);
temp_out.setText(response.body().getMain().getTemp() + " ℃");
Press_out.setVisibility(View.VISIBLE);
Press_out.setText(response.body().getMain().getPressure() + " hpa");
Humid_out.setVisibility(View.VISIBLE);
Humid_out.setText(response.body().getMain().getHumidity() + " %");
Ws_out.setVisibility(View.VISIBLE);
Ws_out.setText(response.body().getWind().getSpeed() + " Km/h");
Visi_out.setVisibility(View.VISIBLE);
Visi_out.setText(response.body().getVisibility() + " m");
Cloud_out.setVisibility(View.VISIBLE);
Cloud_out.setText(response.body().getClouds().getAll() + " %");
} catch (Exception e) {
Log.e("TAG", "No City found");
current_temp.setVisibility(View.GONE);
current_output.setVisibility(View.GONE);
rise_time.setVisibility(View.GONE);
set_time.setVisibility(View.GONE);
temp_out.setVisibility(View.GONE);
Press_out.setVisibility(View.GONE);
Humid_out.setVisibility(View.GONE);
Ws_out.setVisibility(View.GONE);
Visi_out.setVisibility(View.GONE);
Cloud_out.setVisibility(View.GONE);
Toast.makeText(getActivity(), "No City found", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onFailure(#NotNull Call<Example> call, #NotNull Throwable t) {
t.printStackTrace();
}
});
}
}
I didn't paste everything because I'm following https://stackoverflow.com/help/minimal-reproducible-example
Your problem is that your FirstFragment is not saving its state properly. As per the Saving state with fragments guide:
To ensure the user's state is saved, the Android framework automatically saves and restores the fragments and the back stack. Therefore, you need to ensure that any data in your fragment is saved and restored as well.
But you aren't saving the last name that you pass to getWeatherData, nor are you saving your Example object that you get from your API call in order to repopulate your views when they are recreated.
So need to actually use the APIs described in that guide to save your state. Namely, you should file the Guide to app architecture, which explains how you can separate your from your data loading by using ViewModels to store data across configuration changes (like rotating your device) and LiveData to automatically populate your UI whenever your data is loaded.
The first thing we want to do is move the data loading to a ViewModel. This object survives configuration changes which means any data stored in this class is automatically saved when you rotate your device. This is how we can save your Example class and avoid calling the server over and over.
By using the APIs in the Saved State module for ViewModel (specifically, the SavedStateHandle class), any data you save in there will survive your process being killed and later recreated (say, if your device is low on memory, etc.). This is how we can save the last name so that we will automatically requery for your data.
Here, our ViewModel handles all of the loading from the server and uses a LiveData to let our UI automatically update as the data is loaded.
public class WeatherDataViewModel extends ViewModel {
// This will save the city name
private SavedStateHandle state;
// This is where we'll store our result from the server
private MutableLiveData<Example> mutableWeatherData = new MutableLiveData<>();
public WeatherDataViewModel(SavedStateHandle savedStateHandle) {
state = savedStateHandle;
String savedCityName = state.get("name");
if (savedCityName != null) {
// We already had a previously saved name, so we'll
// start loading right away
loadData();
}
}
// This is what our Fragment will use to get the latest weather data
public LiveData<Example> getWeatherDataLiveData() {
return mutableWeatherData;
}
// When you get a new city name, we'll save that in our
// state, then load the new data from the server
public void setCityName(String name) {
state.set("name", name);
loadData();
}
private void loadData() {
// Get the last name that was set
String name = state.get("name");
// Now kick off a load from the server
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<Example> call = apiInterface.getWeatherData(name);
call.enqueue(new Callback<Example>() {
#Override
public void onResponse(#NonNull Call<Example> call, #NonNull Response<Example> response) {
// Save the response we've gotten
// This will automatically update our UI
mutableWeatherData.setValue(response.body());
}
#Override
public void onFailure(#NotNull Call<Example> call, #NotNull Throwable t) {
t.printStackTrace();
}
});
}
}
Now you can rewrite your FirstFragment to use the WeatherDataViewModel as the source of truth for your UI:
public class FirstFragment extends Fragment {
private WeatherDataViewModel viewModel;
public FirstFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_first, container, false);
// For displaying weather data
final TextView current_temp = rootView.findViewById(R.id.textView10);
final TextView current_output = rootView.findViewById(R.id.textView11);
final TextView rise_time = rootView.findViewById(R.id.textView25);
final TextView set_time = rootView.findViewById(R.id.textView26);
final TextView temp_out = rootView.findViewById(R.id.textView28);
final TextView Press_out = rootView.findViewById(R.id.textView29);
final TextView Humid_out = rootView.findViewById(R.id.textView30);
final TextView Ws_out = rootView.findViewById(R.id.textView33);
final TextView Visi_out = rootView.findViewById(R.id.textView34);
final TextView Cloud_out = rootView.findViewById(R.id.textView35);
// Get our ViewModel instance
viewModel = new ViewModelProvider(this).get(WeatherDataViewModel.class);
// And whenever the data changes, refresh the UI
viewModel.getWeatherDataLiveData().observe(getViewLifecycleOwner(), data -> {
if (data != null) {
current_temp.setVisibility(View.VISIBLE);
current_temp.setText(data.getMain().getTemp() + " ℃");
current_output.setVisibility(View.VISIBLE);
current_output.setText(data.getWeather().get(0).getDescription());
rise_time.setVisibility(View.VISIBLE);
rise_time.setText(data.getSys().getSunrise() + " ");
set_time.setVisibility(View.VISIBLE);
set_time.setText(data.getSys().getSunset() + " ");
temp_out.setVisibility(View.VISIBLE);
temp_out.setText(data.getMain().getTemp() + " ℃");
Press_out.setVisibility(View.VISIBLE);
Press_out.setText(data.getMain().getPressure() + " hpa");
Humid_out.setVisibility(View.VISIBLE);
Humid_out.setText(data.getMain().getHumidity() + " %");
Ws_out.setVisibility(View.VISIBLE);
Ws_out.setText(data.getWind().getSpeed() + " Km/h");
Visi_out.setVisibility(View.VISIBLE);
Visi_out.setText(data.getVisibility() + " m");
Cloud_out.setVisibility(View.VISIBLE);
Cloud_out.setText(data.getClouds().getAll() + " %");
} else {
Log.e("TAG", "No City found");
current_temp.setVisibility(View.GONE);
current_output.setVisibility(View.GONE);
rise_time.setVisibility(View.GONE);
set_time.setVisibility(View.GONE);
temp_out.setVisibility(View.GONE);
Press_out.setVisibility(View.GONE);
Humid_out.setVisibility(View.GONE);
Ws_out.setVisibility(View.GONE);
Visi_out.setVisibility(View.GONE);
Cloud_out.setVisibility(View.GONE);
Toast.makeText(requireActivity(), "No City found", Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
public void getWeatherData(String name) {
// The ViewModel controls loading the data, so we just
// tell it what the new name is - this kicks off loading
// the data, which will automatically call through to
// our observe() call when the data load completes
viewModel.setCityName(name);
}
}
With these changes, you'll find that your Fragment now correctly handles:
Being put on the Fragment back stack
Configuration changes (i.e., rotating your device)
Process death and recreation (i.e., testing with 'Don't keep activities' on)
You'll note how we use new ViewModelProvider(this).get(WeatherDataViewModel.class) - that creates a WeatherDataViewModel that is tied to this - your Fragment itself. This is best if the data your ViewModel loads is only used in that one Fragment.
If you also wanted to use this same data in your Activity, your activity could use new ViewModelProvider(this).get(WeatherDataViewModel.class) to create a WeatherDataViewModel that is scoped to the entire Activity. Any Fragment could then use new ViewModelProvider(requireActivity()).get(WeatherDataViewModel.class) to get that Activity owned ViewModel. This would potentially mean that you wouldn't need a getWeatherData() method on your Fragment at all - instead, your Activity would directly call viewModel.setCityName(name) itself and all Fragments would just instantly update (as they read from the same ViewModel).
I think, this is because your FirstFragment gets reinstantiated every time. To save state, modify onCreate() of your FirstFragment like following:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mParam1 = savedInstanceState.getString(ARG_PARAM1);
mParam2 = savedInstanceState.getString(ARG_PARAM2);
} else if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
And add onSaveInstanceState() to your FirstFragment:
#Override
public void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(ARG_PARAM1, mParam1);
outState.putString(ARG_PARAM2, mParam2);
}
The mistake seems to be that every time you select an item in the bottom navigation bar the fragment gets re-instantized thus creating a new fragment(of same type, but a new object of that fragment).
I would suggest you create an object of all the fragment at the start like this
private final Fragment[] fragments = {
new HomeFragment(),
new MyNotesFragment(),
new PreferencesFragment()
};
and then for the Bottom Nav Listener part, we will change the fragments according to the button clicked by passing already made instances of fragments rather than creating new ones.
bottom_navigation_view.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
// Checking which bottom_nav items is clicked by comparing with string names..
if (item.getTitle().toString().equals("Home")) {
// Code to change the fragments
final FragmentTransaction ft =
getFragmentManager().beginTransaction();
// we passed the already made instance of the home fragment rather than creating a new one
ft.replace(R.id.my_fragment, fragments[0], "HomeFrag");
ft.commit();
} else if (item.getTitle().toString().equals("My Notes")) {
// Code to change the fragments
final FragmentTransaction ft =
getFragmentManager().beginTransaction();
ft.replace(R.id.my_fragment, fragments[1], "MyNotesFrag");
ft.commit();
} else if (item.getTitle().toString().equals("Preferences")) {
// Code to change the fragments
final FragmentTransaction ft =
getFragmentManager().beginTransaction();
ft.replace(R.id.my_fragment, fragments[2], "PrefFrag");
ft.commit();
}
return true;
}
});
(Though this is for the ViewPager library the concept works the same)
If you still have not solve it yet, you might want to try this solution that uses SharedPreferences.
public class FirstFragment extends Fragment
{
// ...
private SharedPreferences prefs;
private SharedPreferences.Editor prefsEditor;
// ...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_first, container, false);
// For displaying weather data
current_temp = rootView.findViewById(R.id.textView10);
current_output = rootView.findViewById(R.id.textView11);
// ...
prefs = new SharedPreferences(getActivity().getApplicationContext());
prefsEditor = prefs.edit();
current_temp.setText(prefs.getString("current_temp", "not yet initialised"));
current_output.setText(prefs.getString("current_output", "not yet initialised"));
// ...
return rootView;
}
public void getWeatherData(String name)
{
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<Example> call = apiInterface.getWeatherData(name);
call.enqueue(new Callback<Example>() {
#Override
public void onResponse(#NonNull Call<Example> call, #NonNull Response<Example> response) {
try {
assert response.body() != null;
final String currentTemp = response.body().getMain().getTemp() + " ℃";
editor.putString("current_temp", currentTemp);
editor.commit();
current_temp.setText(currentTemp);
current_temp.setVisibility(View.VISIBLE);
final String currentOutput = response.body().getWeather().get(0).getDescription();
editor.putString("current_output", currentOutput);
editor.commit();
current_output.setText(currentOutput);
current_output.setVisibility(View.VISIBLE);
// ...
} catch (Exception e) {
Log.e("TAG", "No City found");
current_temp.setVisibility(View.GONE);
current_output.setVisibility(View.GONE);
// ...
Toast.makeText(getActivity(), "No City found", Toast.LENGTH_SHORT).show();
}
}
// ...
});
}
}

changing getMap to GetMapAsync

I am trying to create an interactive infoWindow based on one of the answers from Stack Overflow.
However, I am getting an error with getMap() used in the code. Although I've tried with getMapAsync(), I'm unable to resolve the problem. Please help me. If any new code is available for an interactive infowindow with a button, then please share the code.
public class MainActivity extends AppCompatActivity {
private ViewGroup infoWindow;
private TextView infoTitle;
private TextView infoSnippet;
private Button infoButton1, infoButton2;
private OnInfoWindowElemTouchListener infoButtonListener;
static final LatLng latlng1 = new LatLng(28.5355, 77.3910);
static final LatLng latlng2 = new LatLng(28.6208768, 77.3726377);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
final MapWrapperLayout mapWrapperLayout = (MapWrapperLayout)findViewById(R.id.map_relative_layout);
final GoogleMap googleMap = mapFragment.getMap();
// MapWrapperLayout initialization
// 39 - default marker height
// 20 - offset between the default InfoWindow bottom edge and it's content bottom edge
mapWrapperLayout.init(googleMap, getPixelsFromDp(this, 39 + 20));
// We want to reuse the info window for all the markers,
// so let's create only one class member instance
this.infoWindow = (ViewGroup)getLayoutInflater().inflate(R.layout.custom_infowindow, null);
this.infoTitle = (TextView)infoWindow.findViewById(R.id.nameTxt);
this.infoSnippet = (TextView)infoWindow.findViewById(R.id.addressTxt);
this.infoButton1 = (Button)infoWindow.findViewById(R.id.btnOne);
this.infoButton2 = (Button)infoWindow.findViewById(R.id.btnTwo);
// Setting custom OnTouchListener which deals with the pressed state
// so it shows up
this.infoButtonListener = new OnInfoWindowElemTouchListener(infoButton1, getResources().getDrawable(R.drawable.btn_bg), getResources().getDrawable(R.drawable.btn_bg)){
#Override
protected void onClickConfirmed(View v, Marker marker) {
// Here we can perform some action triggered after clicking the button
Toast.makeText(MainActivity.this, "click on button 1", Toast.LENGTH_SHORT).show();
}
};
this.infoButton1.setOnTouchListener(infoButtonListener);
infoButtonListener = new OnInfoWindowElemTouchListener(infoButton2, getResources().getDrawable(R.drawable.btn_bg),getResources().getDrawable(R.drawable.btn_bg)){
#Override
protected void onClickConfirmed(View v, Marker marker) {
Toast.makeText(getApplicationContext(), "click on button 2", Toast.LENGTH_LONG).show();
}
};
infoButton2.setOnTouchListener(infoButtonListener);
/*infoWindow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "click on infowindow", Toast.LENGTH_LONG).show();
}
});*/
googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker marker) {
return null;
}
#Override
public View getInfoContents(Marker marker) {
// Setting up the infoWindow with current's marker info
infoSnippet.setText(marker.getTitle());
infoTitle.setText(marker.getSnippet());
infoButtonListener.setMarker(marker);
// We must call this to set the current marker and infoWindow references
// to the MapWrapperLayout
mapWrapperLayout.setMarkerWithInfoWindow(marker, infoWindow);
return infoWindow;
}
});
// Let's add a couple of markers
googleMap.addMarker(new MarkerOptions()
.position(latlng1)
.title("Source")
.snippet("Comapny Name")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
googleMap.addMarker(new MarkerOptions()
.position(latlng2)
.title("Destination")
.snippet("AmisunXXXXXX")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
//googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 15));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng1, 10));
}
public static int getPixelsFromDp(Context context, float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int)(dp * scale + 0.5f);
}
}
The error is coming because Google has removed getMap(), but I dont know the alternative solution for this.
UPDATE
Define private GoogleMap gMapand implement OnMapReadyCallback in your main Activity and implement onMapReady method
in onMapReady you should write this line
gMap = googleMap, then you can do what you want in onMapReady
getting current location
custom setting for your map
and ...
now it's time to use this lines in your OnCreate method
SupportMapFragment supportMapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
supportMapFragment.getMapAsync(this);
it should solve your problem, hope it helped you

getting error with getMap()?

I am trying for interactive infoWindow with one of the answer of stack overflow.
link is given below:
interactive infowindow
but i am getting error with getMap() used in the code. although I tried with getMapAsync but unable to resolve the problem. please help me asap.If any new code available for interactive infowindow with button then please share the code.
public class MainActivity extends Activity {
private ViewGroup infoWindow;
private TextView infoTitle;
private TextView infoSnippet;
private Button infoButton;
private OnInfoWindowElemTouchListener infoButtonListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MapFragment mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
final MapWrapperLayout mapWrapperLayout = (MapWrapperLayout)findViewById(R.id.map_relative_layout);
final GoogleMap map = mapFragment.getMap();
// MapWrapperLayout initialization
// 39 - default marker height
// 20 - offset between the default InfoWindow bottom edge and it's content bottom edge
mapWrapperLayout.init(map, getPixelsFromDp(this, 39 + 20));
// We want to reuse the info window for all the markers,
// so let's create only one class member instance
this.infoWindow = (ViewGroup)getLayoutInflater().inflate(R.layout.info_window, null);
this.infoTitle = (TextView)infoWindow.findViewById(R.id.title);
this.infoSnippet = (TextView)infoWindow.findViewById(R.id.snippet);
this.infoButton = (Button)infoWindow.findViewById(R.id.button);
// Setting custom OnTouchListener which deals with the pressed state
// so it shows up
this.infoButtonListener = new OnInfoWindowElemTouchListener(infoButton,
getResources().getDrawable(R.drawable.btn_default_normal_holo_light),
getResources().getDrawable(R.drawable.btn_default_pressed_holo_light))
{
#Override
protected void onClickConfirmed(View v, Marker marker) {
// Here we can perform some action triggered after clicking the button
Toast.makeText(MainActivity.this, marker.getTitle() + "'s button clicked!", Toast.LENGTH_SHORT).show();
}
};
this.infoButton.setOnTouchListener(infoButtonListener);
map.setInfoWindowAdapter(new InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker marker) {
return null;
}
#Override
public View getInfoContents(Marker marker) {
// Setting up the infoWindow with current's marker info
infoTitle.setText(marker.getTitle());
infoSnippet.setText(marker.getSnippet());
infoButtonListener.setMarker(marker);
// We must call this to set the current marker and infoWindow references
// to the MapWrapperLayout
mapWrapperLayout.setMarkerWithInfoWindow(marker, infoWindow);
return infoWindow;
}
});
// Let's add a couple of markers
map.addMarker(new MarkerOptions()
.title("Prague")
.snippet("Czech Republic")
.position(new LatLng(50.08, 14.43)));
map.addMarker(new MarkerOptions()
.title("Paris")
.snippet("France")
.position(new LatLng(48.86,2.33)));
map.addMarker(new MarkerOptions()
.title("London")
.snippet("United Kingdom")
.position(new LatLng(51.51,-0.1)));
}
public static int getPixelsFromDp(Context context, float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int)(dp * scale + 0.5f);
}
}
error is coming because of google has removed getMap but dont know the alternative solution for this.
Error:(149, 42) error: cannot find symbol method getMap()
Your Existing Code is:
final MapFragment mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
final GoogleMap map = mapFragment.getMap();
Try this instead of that:
MapFragment mapFragment; // declare in global scope
GoogleMap googleMap; // declare in global scope
Put this code in OnCreate method:
mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap map) {
googleMap = map;
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMyLocationEnabled(false); // false to disable
googleMap.setOnMarkerClickListener(onMarkerClick);
googleMap.setOnInfoWindowClickListener(onInfoWindowClick);
googleMap.getUiSettings().setZoomControlsEnabled(true);// Display Zoom Controls
googleMap.setMyLocationEnabled(true);// Display My Location Control
}
});
}
for converting pixels into DP use this:
public int getPixelsFromDp(int px){
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
return Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
}
Use getMapAsync in your onCreatView() method
((SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map))
.getMapAsync(this)
Also imeplement the OnMapReadyCallback interface, and override it's method
In method onMapReady() which belongs to onMapReadyCallback interface do all the operations with your map you need. It will look like after your manipulations next
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// some action with our map
mMap.addMarker(new MarkerOptions().position(randomPosition).title("Random Title"));
mMap.addMarker(new MarkerOptions().position(antoherRandomPosition).title("Another RND Title"));
// some processes with db
DataBaseMapFragment database = new DataBaseMapFragment(dbHelper, mMap);
database.createBD();
// permission to check, if gps access is granted
if (ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
//another actions with map
mMap.setMyLocationEnabled(true);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(camera, 13));
mMap.setBuildingsEnabled(true);
mMap.setOnMapLongClickListener(this);
mMap.setOnMyLocationChangeListener(myLocationChangeListener);
mMap.setOnMarkerClickListener(this);
reffering Google documentation and guide.
Check it to learn more about maps in android and to be clear about
The getMap() function replaced from getMapAsync() more here
https://developers.google.com/android/reference/com/google/android/gms/maps/package-summary

Android Studio retrieve information from XML in a override method

Sorry for the noob question but I did not come across any situations that would work for me. Im sure this is super simple, I just need to be pointed in the right direction.
Objective:
I want to be able to take the name of what the user specified and add it to the markers title.
What I know:
I am unable to inflate the xml and grab it via that way cause the method is being override and if I inflate to my knowledge will no longer allow me to override the method. How should I approach this?
MapsAcitivy
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, addPlace.Communicator {
private GoogleMap mMap;
public static final int MY_PERMISSION_ACCESS_FINE_LOCATION = 0;
int mStackLevel;
int YES_NO_CALL;
FragmentManager manager = getSupportFragmentManager();
final Context context = this;
#InjectView(R.id.btn_login) Button _loginButton;
ClusterManager<ClusterRequest> mClusterManager;
class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
// These are both viewgroups containing an ImageView with id "badge" and two TextViews with id
// "title" and "snippet".
private final View mWindow;
//private final View mContents;
CustomInfoWindowAdapter() {
mWindow = getLayoutInflater().inflate(R.layout.info_window_layout, null);
//mContents = getLayoutInflater().inflate(R.layout.custom_info_contents, null);
}
#Override
public View getInfoWindow(Marker marker) {
render(marker, mWindow);
return mWindow;
}
#Override
public View getInfoContents(Marker marker) {
// Getting view from the layout file info_window_layout
View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
// Getting the position from the marker
LatLng latLng = marker.getPosition();
// Getting reference to the TextView to set latitude
TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);
// Getting reference to the TextView to set longitude
TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);
// Setting the latitude
tvLat.setText("Latitude:" + latLng.latitude);
// Setting the longitude
tvLng.setText("Longitude:"+ latLng.longitude);
// Returning the view containing InfoWindow contents
return v;
//render(marker, mContents);
//return mContents;
}
private void render(Marker marker, View view) {
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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);
}
#Override
#TargetApi(23)
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSION_ACCESS_FINE_LOCATION);
// MY_PERMISSION_ACCESS_FINE_LOCATION is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
LocationManager initialLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String initialProvider = initialLocationManager.getBestProvider(criteria, true);
Location initialLocation = initialLocationManager.getLastKnownLocation(initialProvider);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
double initialLatitude = initialLocation.getLatitude();
double initialLongitude = initialLocation.getLongitude();
LatLng initialLatLng = new LatLng(initialLatitude, initialLongitude);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(initialLatLng, 14.9f));
//mMap.setOnInfoWindowClickListener(this);
mClusterManager = new ClusterManager<ClusterRequest>(getApplicationContext(), mMap);
mMap.setOnCameraChangeListener(mClusterManager);
mMap.setOnMarkerClickListener(mClusterManager);
}
}
public void onButtonClick(View view){
//do something when button is clicked.
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.add_place);
dialog.setTitle("Add Place");
Button btnLogin = (Button) dialog.findViewById(R.id.addPlacebtnSubmit);
Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel);
final EditText txtUsername = (EditText)dialog.findViewById(R.id.txtStopName);
// Attached listener for add place GUI button
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String stopName = txtUsername.getText().toString();
if(stopName.length() > 4){
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(MapsActivity.this,
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(MapsActivity.this,
android.Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(MapsActivity.this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSION_ACCESS_FINE_LOCATION);
// MY_PERMISSION_ACCESS_FINE_LOCATION is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
LocationManager currentLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria currentCriteria = new Criteria();
String currentProvider = currentLocationManager.getBestProvider(currentCriteria, true);
Location currentLocation = currentLocationManager.getLastKnownLocation(currentProvider);
double currentLatitude = currentLocation.getLatitude();
double currentLongitude = currentLocation.getLongitude();
LatLng currentLatLng = new LatLng(currentLatitude, currentLongitude);
ClusterRequest testCluster = new ClusterRequest(currentLatitude, currentLongitude);
mClusterManager.addItem(testCluster);
mClusterManager.setRenderer(new MapIconRender(getApplicationContext(), mMap, mClusterManager));
dialog.dismiss();
}
else{
txtUsername.setError("Stop name must be at least 5 characters long.");
}
}
});
}
MapIconRender.java
public class MapIconRender extends DefaultClusterRenderer<ClusterRequest> {
public MapIconRender(Context context, GoogleMap map,
ClusterManager<ClusterRequest> clusterManager) {
super(context, map, clusterManager);
}
#Override
protected void onBeforeClusterItemRendered (ClusterRequest item, MarkerOptions
markerOptions){
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker_outside_azure));
markerOptions.snippet("Status: ");
markerOptions.title(stopName); //<------ THIS IS WHERE I WANT THE STOP NAME TO BE WHAT THE USER ENTERED.
super.onBeforeClusterItemRendered(item, markerOptions);
}
}
add_place.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_height="match_parent"
android:paddingLeft="10dp" android:paddingRight="10dp"
android:background="#fff" android:layout_width="300dp"
android:paddingTop="10dp">
<View
android:id="#+id/HorizontalLine"
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#aaa" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name:"
android:paddingTop="20dp"
android:id="#+id/name" />
<EditText android:layout_height="wrap_content"
android:id="#+id/txtStopName"
android:maxLength="100"
android:singleLine="true"
android:hint="Name of this stop (5-100 characters)"
android:layout_width="match_parent">
<requestFocus></requestFocus>
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Type:"
android:paddingTop="20dp"
android:layout_marginBottom="10dp"
android:id="#+id/type" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:entries="#array/type_arrays"
android:prompt="#string/type_prompt"
android:layout_marginBottom="10dp"/>
<View
android:id="#+id/HorizontalLine2"
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#aaa" />
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/linearLayout1"
android:paddingTop="5dp">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="0.5"
android:id="#+id/btnCancel" android:text="Cancel"
android:onClick="addPlaceCancel"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="0.5"
android:id="#+id/addPlacebtnSubmit" android:text="Submit"
android:onClick="addPlaceSubmit"
android:layout_marginBottom="10dp"/>
</LinearLayout>
</LinearLayout>
ClusterRequest.java
public class ClusterRequest implements ClusterItem {
private final LatLng mPosition;
public ClusterRequest(double lat, double lng) {
mPosition = new LatLng(lat, lng);
}
#Override
public LatLng getPosition() {
return mPosition;
}
}
Can't you override onClusterItemRendered() method?
I think you can save your marker name in onBeforeClusterItemRendered() to any class-global variable String, and set the marker name in onClusterItemRendered() with Marker.setTitle(yourSavedTitle); or something.
Something like this:
public class MapIconRender extends DefaultClusterRenderer<ClusterRequest> {
private String mMarkerTitle;
public MapIconRender(Context context, GoogleMap map,
ClusterManager<ClusterRequest> clusterManager) {
super(context, map, clusterManager);
}
#Override
protected void onBeforeClusterItemRendered (ClusterRequest item, MarkerOptions
markerOptions){
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker_outside_azure));
markerOptions.snippet("Status: ");
markerOptions.title(stopName); //<------ THIS IS WHERE I WANT THE STOP NAME TO BE WHAT THE USER ENTERED.
mMarkerTitle = stopName;
super.onBeforeClusterItemRendered(item, markerOptions);
}
#Override
protected void onClusterItemRendered (ClusterRequest item, Marker marker){
marker.setTitle(mMarkerTitle);
}
}
#SuhyeonLee Not sure if this is what you meant (please let me know) but I did find the answer surprisingly enough in the demo that google released for their customize cluster demo. Heres what I learned. When calling the MapIconRender.java class via
mClusterManager.setRenderer(new MapIconRender(getApplicationContext(), mMap, mClusterManager));
Which will run this method in the MapIconRender.java class
protected void onBeforeClusterItemRendered (ClusterRequest item, MarkerOptions
markerOptions)
Which means whatever variables in the ClusterRequest class will be accessible here in the MapIconRender.java class and can be accessed via (item.variableName). I apologize for not posting the code for the file ClusterRequest, I didnt think it was needed. ill go back and add that snippet to my original question.
Anyway the new code I needed to add was as follows:
In the MapsActivity class I needed to pass the ClusterRequest class the name of the marker(or title of the marker) via:
ClusterRequest testCluster = new ClusterRequest(currentLatitude, currentLongitude, stopName); //stopName was added here.
And in the ClusterRequest class I needed to add the "global variable" of the marker name (aka stopName) via:
public class ClusterRequest implements ClusterItem {
private final LatLng mPosition;
public final String stopName; //Add a "global variable" here so it can be accessible in the MapIconRender class.
public ClusterRequest(double lat, double lng, String _stopName) {
mPosition = new LatLng(lat, lng);
stopName = _stopName;
}
#Override
public LatLng getPosition() {
return mPosition;
}
}
And finally in my MapIconRender.java class I can add the title name of the marker via:
public class MapIconRender extends DefaultClusterRenderer<ClusterRequest> {
public MapIconRender(Context context, GoogleMap map,
ClusterManager<ClusterRequest> clusterManager) {
super(context, map, clusterManager);
}
#Override
protected void onBeforeClusterItemRendered (ClusterRequest item, MarkerOptions
markerOptions){
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker_outside_azure));
markerOptions.snippet("Status: ");
markerOptions.title("Title: " + item.stopName); //Now we can access the title of the marker by calling item.variableName
super.onBeforeClusterItemRendered(item, markerOptions);
}
}
Im really sorry about the noob question here but I hope this helps someone out that may have the same question as I did and thank you for your answers! "SO" community is awesome!

Maps marker point image with picasso not loaded at first time

I'm implementing android Mapview with Custom marker. I'm using picasso to load image into marker view. And when i launch the app at the first time, it shows me all the markers, but only one marker that has loaded from database with picasso, the other markers are not loaded from database, they only show me the default maps marker pin. But when i go to the previous activity and go back into MapsActivity, it shows me all the markers that loaded from database with picasso.
Here's my PicassoMarker class
public class PicassoMarker implements Target {
Marker mMarker;
PicassoMarker(Marker marker) {
mMarker = marker;
}
#Override
public int hashCode() {
return mMarker.hashCode();
}
#Override
public boolean equals(Object o) {
if(o instanceof PicassoMarker) {
Marker marker = ((PicassoMarker) o).mMarker;
return mMarker.equals(marker);
} else {
return false;
}
}
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
mMarker.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
//mMarker.setIcon(BitmapDescriptorFactory.fromResource(R.mipmap.here));
}
}
Here's the method in MapsActivity
public void plotMarkers(ArrayList<MyMarker> markers) {
if(markers.size() > 0) {
for (MyMarker myMarker : markers)
{
markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));
location_marker = mMap.addMarker(markerOption);
target = new PicassoMarker(location_marker);
Picasso.with(MapsActivity.this).load(myMarker.getmIcon()).resize(84, 125).into(target);
mMarkersHashMap.put(location_marker, myMarker);
i = getIntent();
if(i.getBooleanExtra("maps", true)) {
buttonNavigasi.setVisibility(View.VISIBLE);
location_marker.setTitle(i.getStringExtra("nama"));
dest = new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(dest, 16));
}
else {
mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
}
}
}
}
What's going wrong here?
Thanks.
Okay, so I managed to reproduce what you are experiencing and found what was causing your problem. In the code you provided, notice this line in MapsActivity:
target = new PicassoMarker(location_marker);
I presumed that you are using a global single variable for target. I added some logs and managed to see that the only Marker that gets the image loaded using Picasso is the last Marker in the for loop.
The reason is because, every time you enter the loop, the value of target changes to the newer PicassoMarker that you have, making the onBitmapLoaded of the previous PicassoMarker you have useless, since it no longer has a target. :(
So what I did is, I just added a List<Target> variable (make sure you don't forget to initialize it) to store the instances of the targets. In the line where that I specified earlier, I just added the code to store the value of the target to the list, like so:
Target target = new PicassoMarker(location_marker);
targets.add(target);
Tested it on my emulator and it loads the images to all the Markers.
EDIT
Here is the Activity code I used to reproduce your error and then modified it to make it work:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
Intent i;
MarkerOptions markerOption;
List<Target> targets;
HashMap<Marker, MyMarker> mMarkersHashMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
mMarkersHashMap = new HashMap<>();
targets = 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);
}
/**
* 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;
// Add a marker in Sydney and move the camera
// LatLng sydney = new LatLng(-34, 151);
// mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
// mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
ArrayList<MyMarker> markers = new ArrayList<MyMarker>();
MyMarker m1 = new MyMarker(new LatLng(-34, 151.1), "https://developer.chrome.com/extensions/examples/api/idle/idle_simple/sample-128.png");
MyMarker m2 = new MyMarker(new LatLng(-34, 151.2), "https://developer.chrome.com/extensions/examples/api/idle/idle_simple/sample-128.png");
MyMarker m3 = new MyMarker(new LatLng(-34, 151.3), "https://developer.chrome.com/extensions/examples/api/idle/idle_simple/sample-128.png");
markers.add(m1);
markers.add(m2);
markers.add(m3);
plotMarkers(markers);
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Log.d(MapsActivity.class.getSimpleName(), "MARKER Longitude: " + marker.getPosition().longitude);
return false;
}
});
}
public void plotMarkers(ArrayList<MyMarker> markers) {
if (markers.size() > 0) {
for (MyMarker myMarker : markers) {
markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));
Marker location_marker = mMap.addMarker(markerOption);
Target target = new PicassoMarker(location_marker);
targets.add(target);
Picasso.with(MapsActivity.this).load(myMarker.getmIcon()).resize(84, 125).into(target);
mMarkersHashMap.put(location_marker, myMarker);
i = getIntent();
if (i.getBooleanExtra("maps", true)) {
// buttonNavigasi.setVisibility(View.VISIBLE);
location_marker.setTitle(i.getStringExtra("nama"));
LatLng dest = new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(dest, 8f));
} else {
Log.d(MapsActivity.class.getSimpleName(), "In else{}");
// mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
}
}
}
}
}

Categories

Resources