Issue
I have a google maps fragment in my MainActivity
and a SettingsActivity
that has a ListPreference
option to select styles for the map. What I want to do is to listen to the changes in that preference, and change the map on the MainActivity
accordingly. I can only change it on onMapReady
which is a one time thing it is not a listener. How can I do this? I tried to do it in the SettingsActivity
but I can't access mapStyle
.
MainActivity:
private MapFragment mapFragment;
String mapStyle;
private GoogleMap map;
private GoogleApiClient googleApiClient;
SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
mapStyle = prefs.getString("list_preference_1", "<unset>");
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
if (googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
map.setOnMapClickListener(this);
map.setOnMarkerClickListener(this);
map.getUiSettings().setZoomControlsEnabled(true);
try {
// Customise the styling of the base map using a JSON object defined
// in a raw resource file.
int res;
if (mapStyle.equals("Silver")){
res = R.raw.silver_style;
} else if (mapStyle.equals("Retro")) {
res = R.raw.retro_style;
} else if (mapStyle.equals("Dark")) {
res = R.raw.dark_style;
} else if (mapStyle.equals("Night")) {
res = R.raw.night_style;
} else if (mapStyle.equals("Aubergine")) {
res = R.raw.aubergine_style;
} else {
res = R.raw.standard;
}
boolean success = googleMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(
this, res));
if (!success) {
Log.e(TAG, "Style parsing failed.");
}
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Can't find style. Error: ", e);
}
}
SettingsActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
}
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
Solution
Maybe not best solution, but i think you could update your map style on activity resume. After opening settings activity and selecting style you could return to map activity. OnResume function would be called and you would check which style has been selected and update map based on this information.
Maybe something like so would be sufficient:
@Override
protected void onResume() {
super.onResume();
SetMapStyle(map);
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
map.setOnMapClickListener(this);
map.setOnMarkerClickListener(this);
map.getUiSettings().setZoomControlsEnabled(true);
SetMapStyle(googleMap);
}
private void SetMapStyle(GoogleMap googleMap){
if(googleMap == null)
return;
prefs = PreferenceManager.getDefaultSharedPreferences(this);
mapStyle = prefs.getString("list_preference_1", "<unset>");
try {
int res;
if (mapStyle.equals("Silver")){
res = R.raw.silver_style;
} else if (mapStyle.equals("Retro")) {
res = R.raw.retro_style;
} else if (mapStyle.equals("Dark")) {
res = R.raw.dark_style;
} else if (mapStyle.equals("Night")) {
res = R.raw.night_style;
} else if (mapStyle.equals("Aubergine")) {
res = R.raw.aubergine_style;
} else {
res = R.raw.standard;
}
boolean success = googleMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(
this, res));
if (!success) {
Log.e(TAG, "Style parsing failed.");
}
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Can't find style. Error: ", e);
}
}
Answered By - Sabriael
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.