单击切换开关后,MapView无法返回正常状态 [英] MapView not returning to normal state after clicking toggle switch
问题描述
当我将片段中的开关滑动到OFF位置时,我试图使地图视图恢复为正常样式,但它不起作用。以下是我所面对的过程中发生的事情:
I'm trying to get my map view to return to the normal style when I flick the switch within my fragment to the the OFF position but it's not working. The following is what occurs during the process that I am facing:
- 碎片(污染地图)和开关出现
- 轻弹开关切换到ON位置
- 出现样式化地图
- 轻弹开关切换到OFF位置
- 地图不变
- Fragment (containg map) and switch appeared
- Flicked switch to ON position
- Styled map appeared
- Flicked switch to OFF position
- Map doesn't change
我尝试使用 mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
,但是当我轻按开关时,地图仍然停留在样式化的地图视图上。不确定什么地方出了问题。解决此问题必须做什么?
I tried using mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
but the map still remains stuck on the styled map view when I flick the switch. Not really sure as to what has gone wrong. What must be done to resolve this problem?
public class FragmentMap extends android.support.v4.app.Fragment implements OnMapReadyCallback {
private SwitchCompat swt;
public FragmentMap() {
}
GoogleMap mGoogleMap;
MapView mMapView;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_map, container, false);
mMapView = (MapView) v.findViewById(R.id.map_park);
mMapView.onCreate(savedInstanceState);
mMapView.getMapAsync(this);
swt = (SwitchCompat) v.findViewById(R.id.switch_map_park);
swt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
boolean success = mGoogleMap.setMapStyle(new MapStyleOptions(getResources()
.getString(R.string.style_json)));
if (!success) {
Log.e("TabFragmentMap", "Style parsing failed.");
}
}else{
// What goes here?
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
}
});
return v;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
mGoogleMap.setBuildingsEnabled(true);
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
}
推荐答案
在您的 else
代码,您正在使用 setMapType
,而您应该使用 setMapStyle
带有 null
参数以清除先前设置的样式,如此处。
In your else
code, you are using setMapType
while you should be using setMapStyle
with a null
parameter to clear the previously set styling, as mentioned here.
设置为null可清除以前的任何自定义样式。
Set to null to clear any previous custom styling.
代码应该像这样:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
boolean success = mGoogleMap.setMapStyle(new MapStyleOptions(getResources()
.getString(R.string.style_json)));
if (!success) {
Log.e("TabFragmentMap", "Style parsing failed.");
}
}else{
boolean success = mGoogleMap.setMapStyle(null);
if (!success) {
Log.e("TabFragmentMap", "Removing style failed.");
}
}
}
您还应该返回在您的
if(!success)
条件下都切换到先前的检查状态,并显示向用户敬酒
,使他们可以了解正在发生的事情。
You should also return the Switch
back to its previous checked state in both your if (!success)
conditions and show a Toast
to the users so they can understand what is happening.
这篇关于单击切换开关后,MapView无法返回正常状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!