在其他片段中的Google地图上添加标记

时间:2017-05-08 20:52:53

标签: android google-maps android-fragments

如何从主要活动访问片段?我想在片段类中添加带有来自recycledview的位置的标记。具有位置数据的对象位于ClubBean中。我通过界面ClubAdapter.OnClubClickListener获得了这个:

        @Override
        public void onClicked (ClubBean club) {
            ClubBean bean = club;
            Log.d("Name: ", bean.getClubName());
        }

主要活动:

  public class MapsActivity extends FragmentActivity implements LoadAllClubsInterface, ClubAdapter.OnClubClickListener {

        private DrawerLayout drawerLayout;

        private RecyclerView clubRecycler;
        private RecyclerView.LayoutManager clubLayoutManager;
        private ArrayList<ClubBean> clubList = new ArrayList<ClubBean>();

        private RecyclerView.Adapter clubAdapter;

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

            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction().replace(R.id.content_frame, new MyMapFragment()).commit();

            new LoadAllClubs(this).execute(); //load list in background from database

            drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

            clubRecycler = (RecyclerView) findViewById(R.id.recycler_view);
            clubRecycler.setHasFixedSize(true);
            clubLayoutManager = new LinearLayoutManager(this);

            clubRecycler.setLayoutManager(clubLayoutManager);

        }

        @Override
        public void finishDataLoad(ArrayList<HashMap<String, String>> clubs) {
            Iterator<HashMap<String, String>> iterator = clubs.iterator();
            Map<String, String> map = new HashMap<String, String>();
            while (iterator.hasNext()){
                map = iterator.next();
                clubList.add(new ClubBean(map.get("name"),map.get("localization"), map.get("score")));
            }
            //pass the class that implements your listener as a parameter.
            clubAdapter = new ClubAdapter(clubList, this, this);
            clubRecycler.setAdapter(clubAdapter);
        }

     @Override
        public void onClicked (ClubBean club) {
            ClubBean bean = club;
            Log.d("Name: ", bean.getClubName());
        }

    }

我的MapFragment类:

public class MyMapFragment extends Fragment implements OnMapReadyCallback{

    GoogleMap mGoogleMap;
    MapView mMapView;
    View mView;

    public MyMapFragment(){

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.map_fragment, container, false);

        return mView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mMapView = (MapView) mView.findViewById(R.id.map);
        if (mMapView != null){
            mMapView.onCreate(null);
            mMapView.onResume();
            mMapView.getMapAsync(this);
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        MapsInitializer.initialize(getActivity().getApplicationContext());
        mGoogleMap = googleMap;

        LatLng triCity = new LatLng(54.4158773,18.6337789);
        mGoogleMap.addMarker(new MarkerOptions().position(triCity).title("Trojmiasto"));
        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(triCity, 11));
    }


}

4 个答案:

答案 0 :(得分:1)

Activity和Fragment之间的便捷通信方式是通过Interface。以下是基于您的问题的示例代码:

第1步:定义如下界面:

public interface IFragmentController{
          void passDataToFragmentMethod(String someStringValue);
    }

第2步:在您的片段中实现此接口。然后,您将在片段中获得passDataToFragmentMethod(String someStringValue)方法。

public class MyMapFragment extends Fragment implements IFragmentController{


@Override
void passDataToFragmentMethod(String someStringValue){
    // So your logic code here using passed value
}


}

第3步:在您的活动中,只需获取您的片段实例并以这种方式调用passDataToFragmentMethod方法:

Fragment mapFragment=new MyMapFragment();

@Override
public void onClicked (ClubBean club) {
 ClubBean bean = club;
 Log.d("Name: ", bean.getClubName());
 mapFragment.passDataToFragmentMethod(bean.getClubName());
 }

希望这能帮助您解决问题:)

答案 1 :(得分:0)

  

主机活动可以通过使用findFragmentById()捕获Fragment实例,然后直接调用片段的公共方法,将消息传递给片段。

https://developer.android.com/training/basics/fragments/communicating.html

只需在MyMapFragment addMarkers中定义方法,然后从活动中调用它。

答案 2 :(得分:0)

你可以做这样的事情

      public class MapsActivity extends FragmentActivity implements LoadAllClubsInterface, ClubAdapter.OnClubClickListener {

            MyMapFragment mMyFragment;

         @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_maps);
                    mMyFragment=new MyMapFragment();
                    FragmentManager fm = getSupportFragmentManager();
                    fm.beginTransaction().replace(R.id.content_frame,mMyFragment).commit();
        }

 private void addMarkers(){
    //defined this method in Mapfragment
    mMyFragment.addMarkers();
    }

答案 3 :(得分:0)

如果你有列表那么

For(ClubBean obj:ClubList)
{

// latitude and longitude
double latitude =obj.getLatValue();
double longitude = obj.getLngValue();

// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");

// adding marker
googleMap.addMarker(marker);
}