In my Android app I am showing a Google Maps map inside a class that extends Fragment.
At the moment, only the map is shown, but I am not able to obtain the map fro
Follow this code.Hope this might help.Make the changes where ever necessary according to your requirement.I think you are adding this map view as a fragment in a tab view.
public class DealsCloseBy extends Fragment {
SupportMapFragment mMapView;
private GoogleMap googleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View v = inflater.inflate(R.layout.fragment_deals_close_by, container, false);
mMapView = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map));
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
googleMap = mMapView.getMap();
// latitude and longitude
double latitude = 13.0294278;
double longitude =80.24667829999999;
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps").icon(BitmapDescriptorFactory.fromResource(R.mipmap.map_pin));
// Changing marker icon
// marker.icon(BitmapDescriptorFactory
// .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(13.0294278, 80.24667829999999)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
// Perform any camera updates here
return v;
}
}
In xml file,in place of map fragment,use:-
<fragment
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
class="com.google.android.gms.maps.SupportMapFragment"/>
If you use the latest GooglePlayServices
you can use the getMapAsync
function which will automatically initializes the maps system and the view.
Also use a MapView
in the layout
This is how I added the map in fragment.
public class MapFragment extends Fragment implements OnMapReadyCallback {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_tab_fragment1, container, false);
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
SupportMapFragment fragment = new SupportMapFragment();
transaction.add(R.id.mapView, fragment);
transaction.commit();
fragment.getMapAsync(this);
return view;
}
@Override
public void onMapReady(GoogleMap map) {
//map is ready
map.addMarker(...
}
}
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
To use the latest GooglePlayService put this in gradle
compile 'com.google.android.gms:play-services:8.3.0'