Skip to content

Commit

Permalink
add overlay and location tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
solygambas committed Apr 18, 2022
1 parent c5ec46e commit 519dd64
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
6 changes: 3 additions & 3 deletions 18-wander/.idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 3 additions & 6 deletions 18-wander/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

A Google Maps app that displays customized maps and the user's location.

<!-- <p align="center">
<p align="center">
<img src="screenshot.png" style="width:528px;max-width: 100%;">
</p> -->
</p>

## Features

Expand All @@ -16,7 +16,4 @@ A Google Maps app that displays customized maps and the user's location.
- enabling the user to place a marker on a point of interest.
- enabling location tracking.

Based on 2 tutorials by Google Codelabs (2022):

- [Android Google Maps](https://codelabs.developers.google.com/codelabs/advanced-android-kotlin-training-maps)
- [Adding Geofencing to Your Map](https://codelabs.developers.google.com/codelabs/advanced-android-kotlin-training-geofencing)
Based on [Android Google Maps](https://codelabs.developers.google.com/codelabs/advanced-android-kotlin-training-maps) by Google Codelabs (2022).
2 changes: 1 addition & 1 deletion 18-wander/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hfad.wander">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
55 changes: 50 additions & 5 deletions 18-wander/app/src/main/java/com/hfad/wander/MapsActivity.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.hfad.wander

import android.Manifest
import android.content.pm.PackageManager
import android.content.res.Resources
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

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.MapStyleOptions
import com.google.android.gms.maps.model.MarkerOptions
import com.google.android.gms.maps.model.*
import com.hfad.wander.databinding.ActivityMapsBinding
import java.util.*

Expand All @@ -23,6 +24,7 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
private lateinit var map: GoogleMap
private lateinit var binding: ActivityMapsBinding
private val TAG = MapsActivity::class.java.simpleName
private val REQUEST_LOCATION_PERMISSION = 1

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -83,13 +85,34 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
val latitude = 43.60886861403767
val longitude = 3.879782838452318
val homeLatLng = LatLng(latitude, longitude)
val zoomLevel = 15f
val zoomLevel = 18f
map.addMarker(MarkerOptions().position(homeLatLng))
map.moveCamera(CameraUpdateFactory.newLatLngZoom(homeLatLng, zoomLevel))

// add a ground overlay
val overlaySize = 100f
val androidOverlay = GroundOverlayOptions()
.image(BitmapDescriptorFactory.fromResource(R.drawable.android))
.position(homeLatLng, overlaySize)
map.addGroundOverlay(androidOverlay)

setMapLongClick(map)
setPoiClick(map)
setMapStyle(map)
enableMyLocation()
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
if (requestCode == REQUEST_LOCATION_PERMISSION) {
if (grantResults.contains(PackageManager.PERMISSION_GRANTED)) {
enableMyLocation()
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}

// allow users to add a marker
Expand Down Expand Up @@ -125,6 +148,7 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
}

// add custom style
// use https://mapstyle.withgoogle.com/ to generate your json file
private fun setMapStyle(map: GoogleMap) {
try {
val success = map.setMapStyle(
Expand All @@ -140,4 +164,25 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
Log.e(TAG, "Can't find style. Error: ", e)
}
}

// check permissions
private fun isPermissionGranted() : Boolean {
return ContextCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
}

// enable location tracking
private fun enableMyLocation() {
if (isPermissionGranted()) {
map.isMyLocationEnabled = true
}
else {
ActivityCompat.requestPermissions(
this,
arrayOf<String>(Manifest.permission.ACCESS_FINE_LOCATION),
REQUEST_LOCATION_PERMISSION
)
}
}
}
Binary file added 18-wander/app/src/main/res/drawable/android.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 18-wander/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 519dd64

Please sign in to comment.