1

I'm trying to integrate Google maps to show a particular location in my activity, ResortDetails to display the latitude and longitude of the values I receive from my DB, but I am unable to do so. Here is my java code:

public class ResortDetails extends AppCompatActivity implements  View.OnClickListener, OnMapReadyCallback{

private GoogleMap mMap;
protected void onCreate(Bundle savedInstanceState) {
       try {
        // Loading map
        initializeMap();

    } catch (Exception e) {
        e.printStackTrace();
    }
 }

 private void initializeMap() {
    if (mMap == null) {
        SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFrag.getMapAsync(this);

        // check if map is created successfully or not
        if (mMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title(res_name);
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude, longitude)).zoom(12).build();

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    setUpMap();
}

public void setUpMap(){

    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    mMap.addMarker(marker);
    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    //mMap.setMyLocationEnabled(true);
    mMap.setTrafficEnabled(true);
    mMap.setIndoorEnabled(true);
    mMap.setBuildingsEnabled(false);
    mMap.getUiSettings().setZoomControlsEnabled(true);
}

It always ends up with the toast: Sorry! unable to create maps

My XML file was giving me errors to select a layout for the fragment so I did. Here is the XML file for ResortDetails:

`<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1"
app:layout_collapseParallaxMultiplier="1.0"
app:layout_scrollFlags="scroll">

<TextView
    android:id="@+id/res_n"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal|center"
    android:gravity="top|center_vertical|center_horizontal"
    android:textSize="28sp" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1.00">

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

        <TextView
            android:id="@+id/description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="10dp"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="245dp"
            android:layout_gravity="center_vertical|center_horizontal"
            android:padding="20dp"
            android:paddingBottom="10dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="15dp"
            tools:context="com.tripoffbeat.ResortDetails"
            tools:layout="@layout/activity_maps" />


    </LinearLayout>
</ScrollView>

<Button
    android:id="@+id/back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal"
    android:padding="10dp"
    android:paddingBottom="10dp"
    android:paddingEnd="10dp"
    android:paddingStart="10dp"
    android:paddingTop="10dp"
    android:text="Back" />

</LinearLayout>`

I use @layout/activity_maps as the layout for the fragment and that contains a map view in it.

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

<com.google.android.gms.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

I'm not too sure if the layout I've chosen is correct or if the way I am making this is correct. Please do help me out.

Dhruv Verma
  • 420
  • 5
  • 13

1 Answers1

1

In the latest update of Google for Maps, only MapView is supported for fragments. MapFragment & SupportMapFragment doesn't work. I might be wrong but this is what I saw after trying to implement MapFragment & SupportMapFragment.

Setting up the layout for showing the map. location_fragment.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<com.google.android.gms.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
 </RelativeLayout>

Now we code the java class for showing the Map. MapViewFragment

public class MapViewFragment extends Fragment {

MapView mMapView;
private GoogleMap googleMap;

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

    mMapView = (MapView) rootView.findViewById(R.id.mapView);
    mMapView.onCreate(savedInstanceState);

    mMapView.onResume(); // needed to get the map to display immediately

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

    mMapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap mMap) {
            googleMap = mMap;

            // For showing a move to my location button
            googleMap.setMyLocationEnabled(true);

            // For dropping a marker at a point on the Map
            LatLng sydney = new LatLng(-34, 151);
            googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description"));

            // For zooming automatically to the location of the marker
            CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        }
    });

    return rootView;
}

@Override
public void onResume() {
    super.onResume();
    mMapView.onResume();
}

@Override
public void onPause() {
    super.onPause();
    mMapView.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mMapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mMapView.onLowMemory();
}

}

Finally you need to get the API Key for your app by registering your app at Google Cloud Console. Register your app as Native Android App

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • After doing this do I include an object of the MapViewFragment in my ResortDetails class or will this implement the map in my ResortDetails class directly? @Nilesh – Dhruv Verma May 31 '17 at 11:29
  • By using this code you can setup MapView anywhere, inside any ViewPager or Fragment or Activity – AskNilesh May 31 '17 at 11:31
  • *In the latest update of Google for Maps, only MapView is supported for fragments.* **where did you read this?** *MapFragment & SupportMapFragment doesn't work* **how did you get this** *I might be wrong but this is what I saw after trying to implement MapFragment & SupportMapFragment.* **yeah, you are definitely wrong** – Selvin May 31 '17 at 16:09