Skip to content
Snippets Groups Projects
Commit 341bc155 authored by Muhammad Fadhil Amri's avatar Muhammad Fadhil Amri
Browse files

feat: show location in Google Maps

parent 5b874e6a
No related merge requests found
package com.example.nerbos package com.example.nerbos
import android.Manifest import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.graphics.Camera import android.graphics.Camera
import android.location.Address
import android.location.Geocoder
import android.location.Location import android.location.Location
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
...@@ -20,13 +24,13 @@ import com.example.nerbos.databinding.ActivityMapsBinding ...@@ -20,13 +24,13 @@ import com.example.nerbos.databinding.ActivityMapsBinding
import com.google.android.gms.location.FusedLocationProviderClient import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices import com.google.android.gms.location.LocationServices
import com.google.android.gms.maps.model.Marker import com.google.android.gms.maps.model.Marker
import java.util.Locale
class MapsActivity : AppCompatActivity(), OnMapReadyCallback { class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
private lateinit var mMap: GoogleMap private lateinit var mMap: GoogleMap
private lateinit var binding: ActivityMapsBinding private lateinit var binding: ActivityMapsBinding
private lateinit var location: Address
private val permissionCode = 1
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
...@@ -37,14 +41,31 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback { ...@@ -37,14 +41,31 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
val mapFragment = supportFragmentManager val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment .findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this) mapFragment.getMapAsync(this)
val myIntent = intent
val locationName : String? = myIntent.getStringExtra("locationName")
val geocoder = Geocoder(this, Locale.getDefault())
val address = geocoder.getFromLocationName(locationName!!, 1)
if (address != null) {
location = address[0]
}
// Navigation
findViewById<TextView>(R.id.title).setOnClickListener {
val mainIntent: Intent = Intent(this, MainActivity::class.java)
startActivity(mainIntent)
finish()
}
} }
override fun onMapReady(googleMap: GoogleMap) { override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap mMap = googleMap
mMap.uiSettings.isZoomControlsEnabled = true mMap.uiSettings.isZoomControlsEnabled = true
val currentLatLong : LatLng = LatLng(10.0,10.0) val currentLatLong : LatLng = LatLng(location.latitude,location.longitude)
val markerOptions = MarkerOptions().position(currentLatLong).title("Current Location") val markerOptions = MarkerOptions().position(currentLatLong).title("Current Location")
mMap.addMarker(markerOptions) mMap.addMarker(markerOptions)
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLong, 15f))
} }
......
...@@ -103,8 +103,9 @@ class TransactionFragment : Fragment() { ...@@ -103,8 +103,9 @@ class TransactionFragment : Fragment() {
} }
private fun showLocationOnMap(location: String) { private fun showLocationOnMap(location: String) {
val intent = Intent(requireActivity(), MapsActivity::class.java) val mapIntent = Intent(requireActivity(), MapsActivity::class.java)
startActivity(intent) mapIntent.putExtra("locationName", location)
startActivity(mapIntent)
} }
private fun showAddTransactionDialog() { private fun showAddTransactionDialog() {
......
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:autoMirrored="true" android:height="24dp" android:tint="#FFFFFF" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">
<path android:fillColor="@android:color/white" android:pathData="M11.67,3.87L9.9,2.1 0,12l9.9,9.9 1.77,-1.77L3.54,12z"/>
</vector>
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto" <androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map" xmlns:app="http://schemas.android.com/apk/res-auto"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".MapsActivity" /> tools:context=".MapsActivity"
\ No newline at end of file >
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="@color/white"
android:background="@color/base_bg"
android:text="@string/maps"
android:padding = "10dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:drawableStartCompat="@drawable/ic_back"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
...@@ -46,5 +46,6 @@ ...@@ -46,5 +46,6 @@
<string name="income">Income</string> <string name="income">Income</string>
<string name="outcome">Outcome</string> <string name="outcome">Outcome</string>
<string name="title_activity_maps">MapsActivity</string> <string name="title_activity_maps">MapsActivity</string>
<string name="maps">Maps</string>
</resources> </resources>
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment