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.