Saturday, December 14, 2013

[android-developers] Re: Displaying Google Map Android API V2 in fragment

17:08 (1 minute ago)
Hi,
I have done same task, adding Map inside fragment, where fragment is placed inside ViewPager.

public class Map extends Fragment {

    /** The view. */
    private static View view;

    /** The g map. */
    private static GoogleMap gMap;

    /** The bangalore. */
    private final LatLng BANGALORE = new LatLng(12.971689, 77.594504);

    /** The zoom. */
    private float zoom = 11.0f;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }

        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null)
                parent.removeView(view);
        }
        try {
            view = inflater.inflate(R.layout.map, container, false);
            setUpMapIfNeeded();
        } catch (InflateException e) {
            // Log.wtf("S*****", e.getMessage());
        }
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        gMap = null;
        if (gMap != null)
            setUpMap();

        if (gMap == null) {
            gMap = ((SupportMapFragment) this.getActivity().
getSupportFragmentManager()
                    .findFragmentById(R.id.mapd)).getMap();
            if (gMap != null)
                setUpMap();
        }
    }

    /***** Sets up the map if it is possible to do so *****/
    public void setUpMapIfNeeded() {

        if (gMap == null) {
            gMap = ((SupportMapFragment) getActivity().getSupportFragmentManager()
                    .findFragmentById(R.id.mapd)).getMap();
            if (gMap != null)
                setUpMap();
        }
    }

    /**
     * This is where we can add markers or lines, add listeners or move the
     * camera.
     * This should only be called once and when we are sure that {@link #gMap}
     * is not null.
     */
    private void setUpMap() {
        gMap.addMarker(new MarkerOptions().position(BANGALORE).title("My Home")
                .snippet("Home Address"));
        gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(BANGALORE, zoom));
    }

    /****
     * The mapfragment's id must be removed from the FragmentManager or else if
     * the same it is passed on the next time then app will crash
     ****/
    // @Override
    // public void onDestroyView() {
    // super.onDestroyView();
    // FragmentManager fm = getActivity().getSupportFragmentManager();
    // Fragment fragment = (fm.findFragmentById(R.id.mapd));
    // FragmentTransaction ft = fm.beginTransaction();
    // ft.remove(fragment);
    // ft.commit();
    // }
}


In the above code, uncomment  onDestroyView() only if necessary. manytimes destroying after saving, will crash. but it's  upto you.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate