Professional Documents
Culture Documents
EXP9
EXP9
EXP9
Prerequisites
Android Studio installed on your computer.
In your Android Studio project, open the "AndroidManifest.xml" file and add the following code
inside the <application> tag:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY"/>
XML
Copy
dependencies {
implementation 'com.google.android.gms:play-services-maps:17.0.1'
Gradle
Copy
Click "Sync Now" to sync the project with the new dependencies.
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.FragmentActivity
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.LatLng
import com.google.android.gms.maps.model.MarkerOptions
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
mMap = googleMap
mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
Java
Copy
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.example.locationbasedapp.CustomMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CustomMapFragment" />
XML
Copy
Congratulations! You have successfully created a location-based app using Kotlin and Google Maps
API. You can now customize the app further by adding more features like user location, search
functionality, and custom markers.