我有一個谷歌地圖片段MainActivity和一個可以選擇地圖樣式的選項SettingsActivity。ListPreference我想要做的是聆聽該偏好的變化,并MainActivity相應地更改地圖。我只能改變它,onMapReady它是一次性的,它不是一個聽眾。我怎樣才能做到這一點?我嘗試在 中執行此操作,SettingsActivity但無法訪問mapStyle.
主要活動:
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);
}
}
設定活動:
@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);
}
}
uj5u.com熱心網友回復:
也許不是最佳解決方案,但我認為您可以在活動恢復時更新您的地圖樣式。打開設定活動并選擇樣式后,您可以回傳地圖活動。將呼叫 OnResume 函式,您將檢查選擇了哪種樣式并根據此資訊更新地圖。
也許這樣的事情就足夠了:
@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);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/469167.html
