I have a fragment which displays the position of store on Google Map:
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.BitmapDescriptorFactory
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
class AboutFragment : Fragment(), OnMapReadyCallback {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val mapFragment = childFragmentManager.findFragmentById(R.id.jollycat_map) as SupportMapFragment?
mapFragment?.getMapAsync(this)
return inflater.inflate(R.layout.fragment_about, container, false)
}
override fun onMapReady(gmap: GoogleMap) {
val AWESOME_STORE_POSITION = LatLng(-6.20175, 106.78208)
val markerOptions = MarkerOptions().position(AWESOME_STORE_POSITION).title("Awesome Store").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_location))
gmap.addMarker(markerOptions)
gmap.moveCamera(CameraUpdateFactory.newLatLng(AWESOME_STORE_POSITION))
gmap.animateCamera(CameraUpdateFactory.zoomTo(20f))
}
}
And the XML layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical"
tools:context=".AboutFragment">
<androidx.cardview.widget.CardView
android:id="@+id/layout_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
app:layout_constraintTop_toTopOf="parent">
<Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:title="About Us"
android:titleTextColor="@color/darkBlue" />
</androidx.cardview.widget.CardView>
<LinearLayout
android:id="@+id/layout_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:gravity="center"
android:orientation="vertical"
android:padding="16dp"
app:layout_constraintTop_toBottomOf="@id/layout_toolbar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="8dp"
android:background="@drawable/rounded_edit_text"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tvDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/robaga_rounded_regular"
android:textColor="@color/darkBlue"
android:textSize="20dp"
android:textStyle="bold"
tools:text="Yet another description" />
<TextView
android:id="@+id/tvDescription1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/robaga_rounded_regular"
android:textColor="@color/darkBlue"
android:textSize="20dp"
android:textStyle="bold"
tools:text="Yet another company..." />
<FrameLayout
android:layout_marginTop="20dp"
android:layout_width="400dp"
android:layout_height="400dp">
<fragment
android:id="@+id/store_map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
val AWESOME_STORE_POSITION = LatLng(-6.20175, 106.78208)
is a real position. Google Map shows it’s an area on West Jakarta, Indonesia. Running the app gives this result:
Initially, Google Map shows Africa. So I have to pinch and zoom a few times till reaching West Jakarta, but the marker isn’t there. What’s wrong here? Isn’t gmap.animateCamera(CameraUpdateFactory.zoomTo(20f))
suppose to auto zoom the map to the marker?