Adding Markers in Google Map and Focusing Camera when map loaded

Map fragment :xml design

 

   
   


  

Map fragment class




public class MapView extends Fragment {


    View view;
    Bundle bundle;
   
    GoogleMap map;
    Marker marker;
    String name, vicinity, lat, lng, distance;
    public SupportMapFragment mapFragment;

    ImageView home_button, map_button, review_button;

    public MapView() {
        // Required empty public constructor
    }


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


        if (view == null) {

            view = inflater.inflate(R.layout.map_view, container, false);
        } else {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
        }


        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        finder = new LocationFinder(getActivity());
        getActivity().setTitle("Location");
        bundle = this.getArguments();

//here i getting some parameters from the previous page

        if (bundle != null) {
            name = bundle.getString("name");
            vicinity = bundle.getString("vicinity");
            distance = bundle.getString("distance");
            lat = bundle.getString("lat");
            lng = bundle.getString("lng");

        }

      

        initMap();


        getLocation();
    }

    public void initMap() {

        if (map == null) {
            mapFragment = (SupportMapFragment) getChildFragmentManager()
                    .findFragmentById(R.id.map);
        }

        map = mapFragment.getMap();
        map.setMyLocationEnabled(true);
        map.getUiSettings().setMyLocationButtonEnabled(true);

    }


    public void getLocation() {

        MarkerOptions m_options = new MarkerOptions()
                .position(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng)))
                .title(name)
                .snippet(vicinity);
        marker = map.addMarker(m_options);
        marker.showInfoWindow();


        map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
            @Override
            public void onMapLoaded() {

//this function gets called when the map is full loaded
                focusMarker();
            }
        });


    }


    public void focusMarker() {


        CameraPosition pos = new CameraPosition.Builder().target(marker.getPosition())      // Sets the center of the map to Mountain View
                .zoom(17)                   // Sets the zoom
                .bearing(90)                // Sets the orientation of the camera to east
                .tilt(30)                   // Sets the tilt of the camera to 30 degrees
                .build();

        map.animateCamera(CameraUpdateFactory.newCameraPosition(pos));

    }
}

 

Comments

Popular posts from this blog

Creating Simple Horizontal graph in android using android Weight property without any External Libraries