Skip to content
This repository was archived by the owner on Jul 2, 2025. It is now read-only.

Commit 8695acd

Browse files
mapview
1 parent 7ac54d7 commit 8695acd

File tree

10 files changed

+204
-112
lines changed

10 files changed

+204
-112
lines changed

android/canonical/app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ dependencies {
3333
implementation 'com.google.android.gms:play-services-auth:18.0.0'
3434
implementation 'com.google.android.material:material:1.1.0'
3535
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
36+
implementation 'com.google.android.gms:play-services-maps:17.0.0'
37+
implementation 'com.google.android.gms:play-services-location:17.0.0'
3638
testImplementation 'junit:junit:4.12'
3739
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
3840
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<resources>
2+
<!--
3+
TODO: Before you run your application, you need a Google Maps API key.
4+
5+
To get one, follow this link, follow the directions and press "Create" at the end:
6+
7+
https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=CF:9E:07:3A:08:E9:41:50:B3:2D:4C:6E:50:04:A5:E2:6A:64:72:87%3Bcom.google.samples.quickstart.canonical
8+
9+
You can also add your credentials to an existing key, using these values:
10+
11+
Package name:
12+
com.google.samples.quickstart.canonical
13+
14+
SHA-1 certificate fingerprint:
15+
CF:9E:07:3A:08:E9:41:50:B3:2D:4C:6E:50:04:A5:E2:6A:64:72:87
16+
17+
Alternatively, follow the directions here:
18+
https://developers.google.com/maps/documentation/android/start#get-key
19+
20+
Once you have your key (it starts with "AIza"), replace the "google_maps_key"
21+
string in this file.
22+
-->
23+
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaSyDz7brZFFw7tIg8bQLCALELeHp4JNkZmJo</string>
24+
25+
</resources>

android/canonical/app/src/main/AndroidManifest.xml

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,40 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.google.samples.quickstart.canonical">
44

5-
<application
6-
android:allowBackup="true"
7-
android:icon="@mipmap/ic_launcher"
8-
android:label="@string/app_name"
9-
android:roundIcon="@mipmap/ic_launcher_round"
10-
android:supportsRtl="true"
11-
android:theme="@style/AppTheme">
12-
<activity android:name=".MainActivity">
13-
<intent-filter>
14-
<action android:name="android.intent.action.MAIN" />
5+
<!--
6+
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
7+
Google Maps Android API v2, but you must specify either coarse or fine
8+
location permissions for the "MyLocation" functionality.
9+
-->
10+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
1511

16-
<category android:name="android.intent.category.LAUNCHER" />
17-
</intent-filter>
18-
</activity>
19-
</application>
12+
<application
13+
android:allowBackup="true"
14+
android:icon="@mipmap/ic_launcher"
15+
android:label="@string/app_name"
16+
android:roundIcon="@mipmap/ic_launcher_round"
17+
android:supportsRtl="true"
18+
android:theme="@style/AppTheme">
19+
20+
<!--
21+
The API key for Google Maps-based APIs is defined as a string resource.
22+
(See the file "res/values/google_maps_api.xml").
23+
Note that the API key is linked to the encryption key used to sign the APK.
24+
You need a different API key for each encryption key, including the release key that is used to
25+
sign the APK for publishing.
26+
You can define the keys for the debug and release targets in src/debug/ and src/release/.
27+
-->
28+
<meta-data
29+
android:name="com.google.android.geo.API_KEY"
30+
android:value="@string/google_maps_key" />
31+
32+
<activity android:name=".MainActivity">
33+
<intent-filter>
34+
<action android:name="android.intent.action.MAIN" />
35+
36+
<category android:name="android.intent.category.LAUNCHER" />
37+
</intent-filter>
38+
</activity>
39+
</application>
2040

2141
</manifest>

android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/MainActivity.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import org.jetbrains.annotations.Nullable
1313
class MainActivity : AppCompatActivity() {
1414

1515
lateinit var runFragment: RunFragment
16-
lateinit var mapFragment: MapFragment
16+
lateinit var mapsFragment: MapsFragment
1717
lateinit var meFragment: MeFragment
1818

1919
override fun onCreate(savedInstanceState: Bundle?) {
@@ -41,10 +41,10 @@ class MainActivity : AppCompatActivity() {
4141
}
4242

4343
R.id.bottom_navigation_item_map -> {
44-
mapFragment = MapFragment()
44+
mapsFragment = MapsFragment()
4545
supportFragmentManager
4646
.beginTransaction()
47-
.replace(R.id.fragment_layout, mapFragment)
47+
.replace(R.id.fragment_layout, mapsFragment)
4848
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
4949
.commit()
5050
}

android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/MapFragment.kt

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.google.samples.quickstart.canonical
2+
3+
import android.Manifest
4+
import android.app.Activity
5+
import android.content.Intent
6+
import android.content.pm.PackageManager
7+
import android.location.Location
8+
import androidx.fragment.app.Fragment
9+
10+
import android.os.Bundle
11+
import android.view.LayoutInflater
12+
import android.view.View
13+
import android.view.ViewGroup
14+
import androidx.core.app.ActivityCompat
15+
import androidx.fragment.app.FragmentTransaction
16+
import com.google.android.gms.location.FusedLocationProviderClient
17+
import com.google.android.gms.location.LocationServices
18+
19+
import com.google.android.gms.maps.CameraUpdateFactory
20+
import com.google.android.gms.maps.GoogleMap
21+
import com.google.android.gms.maps.OnMapReadyCallback
22+
import com.google.android.gms.maps.SupportMapFragment
23+
import com.google.android.gms.maps.model.LatLng
24+
import com.google.android.gms.maps.model.Marker
25+
import com.google.android.gms.maps.model.MarkerOptions
26+
27+
class MapsFragment : Fragment() {
28+
29+
private lateinit var map: GoogleMap
30+
private lateinit var fusedLocationClient: FusedLocationProviderClient
31+
private lateinit var lastLocation: Location
32+
33+
companion object {
34+
private const val LOCATION_PERMISSION_REQUEST_CODE = 1
35+
}
36+
37+
private fun setUpMap() {
38+
if (ActivityCompat.checkSelfPermission(context as Activity,
39+
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
40+
ActivityCompat.requestPermissions(context as Activity,
41+
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), LOCATION_PERMISSION_REQUEST_CODE)
42+
val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
43+
mapFragment?.getMapAsync(callback)
44+
return
45+
}
46+
47+
map.isMyLocationEnabled = true
48+
49+
fusedLocationClient.lastLocation.addOnSuccessListener(this.activity as Activity) { location ->
50+
// Got last known location. In some rare situations this can be null.
51+
if (location != null) {
52+
lastLocation = location
53+
val currentLatLng = LatLng(location.latitude, location.longitude)
54+
map.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 12f))
55+
map.addMarker(MarkerOptions().position(currentLatLng).title("My location"))
56+
map.moveCamera(CameraUpdateFactory.newLatLng(currentLatLng))
57+
println("****************** %f %f **************\n".format(location.latitude, location.longitude))
58+
}
59+
}
60+
}
61+
62+
63+
private val callback = OnMapReadyCallback { googleMap ->
64+
/**
65+
* Manipulates the map once available.
66+
* This callback is triggered when the map is ready to be used.
67+
* This is where we can add markers or lines, add listeners or move the camera.
68+
* In this case, we just add a marker near Sydney, Australia.
69+
* If Google Play services is not installed on the device, the user will be prompted to
70+
* install it inside the SupportMapFragment. This method will only be triggered once the
71+
* user has installed Google Play services and returned to the app.
72+
*/
73+
map = googleMap
74+
map.uiSettings.isZoomControlsEnabled = true
75+
setUpMap()
76+
}
77+
78+
override fun onCreateView(
79+
inflater: LayoutInflater,
80+
container: ViewGroup?,
81+
savedInstanceState: Bundle?
82+
): View? {
83+
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this.activity as Activity)
84+
return inflater.inflate(R.layout.fragment_maps, container, false)
85+
}
86+
87+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
88+
super.onViewCreated(view, savedInstanceState)
89+
val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
90+
mapFragment?.getMapAsync(callback)
91+
}
92+
}

android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/RunFragment.kt

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ class RunFragment : Fragment() {
2525
// TODO: Rename and change types of parameters
2626
private var param1: String? = null
2727
private var param2: String? = null
28+
private var pauseOffset = 0L
29+
private var isWorking = false
30+
31+
private fun startStopTimer(runBtn: Button, chronometer : Chronometer) {
32+
isWorking = if (!isWorking) {
33+
chronometer.base = SystemClock.elapsedRealtime() - pauseOffset
34+
chronometer.start()
35+
true
36+
} else {
37+
pauseOffset = SystemClock.elapsedRealtime() - chronometer.base
38+
chronometer.stop()
39+
false
40+
}
41+
runBtn.setText(if (!isWorking) R.string.start else R.string.stop)
42+
}
2843

2944
override fun onCreate(savedInstanceState: Bundle?) {
3045
super.onCreate(savedInstanceState)
@@ -66,24 +81,12 @@ class RunFragment : Fragment() {
6681
super.onViewCreated(view, savedInstanceState)
6782
val chronometer = view.findViewById<Chronometer>(R.id.chronometer)
6883
val runBtn = view.findViewById<Button>(R.id.run_btn)
69-
var pauseOffset = 0L
7084

71-
// chronometer.format = "00:00:00"
72-
runBtn.setOnClickListener (object : View.OnClickListener{
73-
var isWorking = false
74-
override fun onClick(v: View) {
75-
isWorking = if (!isWorking) {
76-
chronometer.base = SystemClock.elapsedRealtime() - pauseOffset
77-
chronometer.start()
78-
true
79-
} else {
80-
pauseOffset = SystemClock.elapsedRealtime() - chronometer.base
81-
chronometer.stop()
82-
false
83-
}
84-
run_btn.setText(if (!isWorking) R.string.start else R.string.stop)
85-
}
86-
})
85+
runBtn.setOnClickListener {
86+
startStopTimer(runBtn, chronometer)
87+
}
8788

8889
}
90+
91+
8992
}

android/canonical/app/src/main/res/layout/fragment_map.xml

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:id="@+id/map"
5+
android:name="com.google.android.gms.maps.SupportMapFragment"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
tools:context=".MapsFragment" />
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<resources>
2+
<!--
3+
TODO: Before you release your application, you need a Google Maps API key.
4+
5+
To do this, you can either add your release key credentials to your existing
6+
key, or create a new key.
7+
8+
Note that this file specifies the API key for the release build target.
9+
If you have previously set up a key for the debug target with the debug signing certificate,
10+
you will also need to set up a key for your release certificate.
11+
12+
Follow the directions here:
13+
14+
https://developers.google.com/maps/documentation/android/signup
15+
16+
Once you have your key (it starts with "AIza"), replace the "google_maps_key"
17+
string in this file.
18+
-->
19+
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">YOUR_KEY_HERE</string>
20+
</resources>

0 commit comments

Comments
 (0)