gpt4 book ai didi

android - 在一个 Activity 中使用两个 SupportMapFragment 但无法从我在第一个 SupportMapFragment 中添加的第二个 SupportMapFragment 中删除标记

转载 作者:行者123 更新时间:2023-11-29 01:43:26 24 4
gpt4 key购买 nike

我在一个 Activity 的两个不同 fragment 中使用两个 SupportMapFragment 时遇到奇怪的问题。

实际上,我的问题是在 OneFragment 中添加了 SupprtMapFragment,并在该 map View 中添加了 10 个标记,现在我通过添加另一个 Fragment 转到详细信息页面,并且它现在在这个 Fragment 中也有 SupprtMapFragment 我想清除它并只想显示一个图钉,但它仍然在 map 上显示 11 个标记。

这是我的源代码:

public class MapActivity extends FragmentActivity {

private FragmentManager mFragmrg;

public static String TAG_FRAGMENT_HOME="HomeFragment";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);

initView();
}



private void initView(){

mFragmrg=getSupportFragmentManager();

MapHomeFragment mMapHomeFragment=new MapHomeFragment();

FragmentTransaction mFt=mFragmrg.beginTransaction();


mFt.replace(R.id.frame_holder, mMapHomeFragment, TAG_FRAGMENT_HOME).addToBackStack(TAG_FRAGMENT_HOME).commit();



}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map, menu);
return true;
}

}

Activity 的 XMl 布局:

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MapActivity" >

<FrameLayout
android:id="@+id/frame_holder"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>

</RelativeLayout>

这个 fragment 有我的第一个 SupportMapFragment,我想在 map 上显示超过 11 个标记,它运行良好。

public class MapHomeFragment extends Fragment implements OnInfoWindowClickListener,OnClickListener{

private GoogleMap mMapView;
private FragmentManager mFragMrg;

private SupportMapFragment mSupportMapFragment;


private LatLng mLatnogGuj=new LatLng(23.0626, 72.5809);

private LatLng mLatnogGujOne=new LatLng(18.9158, 72.8267);

private LatLng mLatnogGujTwo=new LatLng(24.5925, 72.7156);

private LatLng mLatnogGuj3=new LatLng(34.0837,74.7974);

private LatLng mLatnogGuj4=new LatLng(23.2245, 72.6379);

private LatLng mLatnogGuj5=new LatLng(12.9716, 77.5946);

private LatLng mLatnogGuj6=new LatLng(23.0396, 72.5660);

private LatLng mLatnogGuj7=new LatLng(22.3140, 73.1748);

private LatLng mLatnogGuj8=new LatLng(20.5732, 73.7442);
private LatLng mLatnogGuj9=new LatLng(21.1785, 72.8191);
private LatLng mLatnogGuj10=new LatLng(28.6644, 77.2349);

private LatLngBounds.Builder mLatlongBuilder;

private Button mBtnMapDetail;


private ViewSwitcher mViewSwitcher;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View mView=inflater.inflate(R.layout.home_map, container, false);


initView(mView);

return mView;
}


private void initView(View mView){

mViewSwitcher=(ViewSwitcher)mView.findViewById(R.id.viewSwitcher);

mViewSwitcher.showNext();

try{
MapsInitializer.initialize(getActivity());
}catch(Exception Exc){
Exc.printStackTrace();
}

mBtnMapDetail=(Button)mView.findViewById(R.id.btn_mapdetail);
mBtnMapDetail.setOnClickListener(this);

mFragMrg=getFragmentManager();

mSupportMapFragment=(SupportMapFragment) mFragMrg.findFragmentByTag("map_home_fragment");


if(mSupportMapFragment!=null){

mMapView=mSupportMapFragment.getMap();

if(mMapView!=null){


//mMapView.getUiSettings().setAllGesturesEnabled(true);

mMapView.getUiSettings().setMyLocationButtonEnabled(true);

mMapView.getUiSettings().setZoomGesturesEnabled(true);

mMapView.getUiSettings().setZoomControlsEnabled(true);

mMapView.setOnInfoWindowClickListener(this);

mMapView.setInfoWindowAdapter(new CustomInfoViewAdapter(getActivity()));


mLatlongBuilder=new LatLngBounds.Builder();

MarkerOptions mMarkerOption=new MarkerOptions();
mMarkerOption.title("Map One").position(mLatnogGuj).icon(BitmapDescriptorFactory.fromResource(R.drawable.map)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGuj);

mMapView.addMarker(mMarkerOption);


MarkerOptions mMarkerOption1=new MarkerOptions();
mMarkerOption1.title("Map One").position(mLatnogGujOne).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_pub)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGujOne);

mMapView.addMarker(mMarkerOption1);

MarkerOptions mMarkerOption2=new MarkerOptions();
mMarkerOption2.title("Map One").position(mLatnogGujTwo).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_rest)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGujTwo);

mMapView.addMarker(mMarkerOption2);

MarkerOptions mMarkerOption3=new MarkerOptions();
mMarkerOption3.title("Map One").position(mLatnogGuj3).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_small)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGuj3);
mMapView.addMarker(mMarkerOption3);


MarkerOptions mMarkerOption4=new MarkerOptions();
mMarkerOption4.title("Map One").position(mLatnogGuj4).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_take)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGuj4);
mMapView.addMarker(mMarkerOption4);

MarkerOptions mMarkerOption5=new MarkerOptions();
mMarkerOption5.title("Map One").position(mLatnogGuj5).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_top)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGuj5);
mMapView.addMarker(mMarkerOption5);


MarkerOptions mMarkerOption6=new MarkerOptions();
mMarkerOption6.title("Map One").position(mLatnogGuj6).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_top_hover)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGuj6);
mMapView.addMarker(mMarkerOption6);


MarkerOptions mMarkerOption7=new MarkerOptions();
mMarkerOption7.title("Map One").position(mLatnogGuj7).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_pub)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGuj7);
mMapView.addMarker(mMarkerOption7);

MarkerOptions mMarkerOption8=new MarkerOptions();
mMarkerOption8.title("Map One").position(mLatnogGuj8).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_rest)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGuj8);
mMapView.addMarker(mMarkerOption8);

MarkerOptions mMarkerOption9=new MarkerOptions();
mMarkerOption9.title("Map One").position(mLatnogGuj9).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_small)).snippet("My Name is");
mMapView.addMarker(mMarkerOption9);

mLatlongBuilder.include(mLatnogGuj9);


MarkerOptions mMarkerOption10=new MarkerOptions();
mMarkerOption10.title("Map One").position(mLatnogGuj10).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_take)).snippet("My Name is");

mLatlongBuilder.include(mLatnogGuj10);

mMapView.addMarker(mMarkerOption10);

//mMapView.animateCamera(CameraUpdateFactory.newLatLngBounds(mLatlongBuilder.build(), 120));

mBtnMapDetail.postDelayed(new Runnable() {

@Override
public void run() {

mMapView.animateCamera(CameraUpdateFactory.newLatLngBounds(mLatlongBuilder.build(), 120));

}
}, 250);

}


}



}


public class CustomInfoViewAdapter implements InfoWindowAdapter{


View mLayoutInfo;


public CustomInfoViewAdapter(Context _Context){


LayoutInflater mLayoutInflater=(LayoutInflater) _Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

mLayoutInfo=mLayoutInflater.inflate(R.layout.info_window_layout, null);



}



@Override
public View getInfoContents(Marker marker) {

return mLayoutInfo;
}

@Override
public View getInfoWindow(Marker marker) {

return null;
}


}


@Override
public void onInfoWindowClick(Marker arg0) {
// TODO Auto-generated method stub

}


@Override
public void onClick(View v) {

intiDetailFragment();

}


private void intiDetailFragment(){

MapDetailFragment mMapDetailFragment=new MapDetailFragment();

FragmentTransaction mFt=mFragMrg.beginTransaction();

mFt.add(R.id.frame_holder, mMapDetailFragment, "DetailFragment").addToBackStack("DetailFragment").commit();
}


}

输出屏幕截图: SupportMap One with 11 Marker

现在,当您单击该 DetailMap 按钮时,它会将用户带到详细信息屏幕,我只想在该 SupportMapFragment 中显示一个标记,但我不知道为什么它仍然显示我在第一个 fragment 中添加的所有标记。

我的第二个 fragment 代码:

public class MapDetailFragment extends Fragment {

private GoogleMap mGoogleMapView;

private SupportMapFragment mSupportMapDetail;

private FragmentManager mFragMrg;

private LatLng mLatnogGujOne=new LatLng(18.9158, 72.8267);

private LatLngBounds.Builder mLatlongBuilder;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View mView = inflater.inflate(R.layout.map_detail_screen, container,
false);

intView(mView);

return mView;
}

private void intView(View mView) {

mFragMrg=getChildFragmentManager();

mSupportMapDetail=(SupportMapFragment) mFragMrg.findFragmentByTag("map_home_detail");



if(mSupportMapDetail!=null){

mGoogleMapView=mSupportMapDetail.getMap();

if(mGoogleMapView!=null){

mGoogleMapView.clear();

mGoogleMapView.getUiSettings().setAllGesturesEnabled(true);

mGoogleMapView.getUiSettings().setMyLocationButtonEnabled(true);

mGoogleMapView.getUiSettings().setZoomGesturesEnabled(true);

mGoogleMapView.getUiSettings().setZoomControlsEnabled(true);

mGoogleMapView.setMyLocationEnabled(true);

mLatlongBuilder=new LatLngBounds.Builder();

MarkerOptions mMarkerOption=new MarkerOptions();

mMarkerOption.title("Map One").position(mLatnogGujOne).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_pub)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGujOne);

mGoogleMapView.addMarker(mMarkerOption);


mGoogleMapView.animateCamera(CameraUpdateFactory.newLatLngBounds(mLatlongBuilder.build(), 120));

}

}

}

}

这是我的 home_map.xml 的 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/btn_mapdetail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mapdetail" />

<ViewSwitcher
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/viewSwitcher"
>

<LinearLayout
android:id="@+id/llMapDemo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

<fragment
android:id="@+id/map_home_fragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="map_home_fragment" />
</ViewSwitcher>

</LinearLayout>

另一个 xml 在这里 map_detail_screen.xml :

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:clickable="true" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/empty_photo" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/empty_photo" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/empty_photo" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/empty_photo" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/empty_photo" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/empty_photo" />

<FrameLayout
android:id="@+id/frame_detail"
android:layout_width="match_parent"
android:layout_height="350dp" >

<fragment
android:id="@+id/map_home_fragment_detail"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="map_home_detail" />
</FrameLayout>
</LinearLayout>

</ScrollView>

这是我的第二个屏幕输出:

SupportMap Second Screen

正如您在我的第二个屏幕上看到的那样,它仍然在 map 上显示标记 为什么会发生这种情况?

任何人都可以提供适当的解决方案,所以当我返回上一个屏幕时,所有标记都与我添加的一样。

最佳答案

替换

mSupportMapDetail=(SupportMapFragment) mFragMrg.findFragmentByTag("map_home_detail");

mSupportMapDetail=(SupportMapFragment) mFragMrg.findFragmentById("map_home_fragment_detail");

也尝试将 findFragmentByTag 替换为 findFragmentById 无处不在..

关于android - 在一个 Activity 中使用两个 SupportMapFragment 但无法从我在第一个 SupportMapFragment 中添加的第二个 SupportMapFragment 中删除标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22946152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com