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

Commit 8fc8663

Browse files
serach place and movethe map sight
1 parent f704af2 commit 8fc8663

File tree

6 files changed

+101
-120
lines changed

6 files changed

+101
-120
lines changed

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

Lines changed: 0 additions & 59 deletions
This file was deleted.

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ class MainActivity : AppCompatActivity() {
2525
when (item.itemId) {
2626

2727
R.id.bottom_navigation_item_run -> {
28-
val autocompleteFrameLayout : FrameLayout = findViewById(R.id.autocomplete_container)
29-
autocompleteFrameLayout.visibility = View.INVISIBLE
30-
3128
val runFragment = RunFragment()
3229
supportFragmentManager
3330
.beginTransaction()
@@ -38,25 +35,16 @@ class MainActivity : AppCompatActivity() {
3835
}
3936

4037
R.id.bottom_navigation_item_map -> {
41-
val autocompleteFrameLayout : FrameLayout = findViewById(R.id.autocomplete_container)
42-
autocompleteFrameLayout.visibility = View.VISIBLE
43-
autocompleteFrameLayout.bringToFront()
44-
4538
val mapsFragment = MapsFragment()
46-
val autocompleteFragment = AutocompleteFragment()
4739
supportFragmentManager
4840
.beginTransaction()
4941
.replace(R.id.fragment_container, mapsFragment)
50-
.replace(R.id.autocomplete_container, autocompleteFragment)
5142
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
5243
.commit()
5344
true
5445
}
5546

5647
R.id.bottom_navigation_item_profile -> {
57-
val autocompleteFrameLayout : FrameLayout = findViewById(R.id.autocomplete_container)
58-
autocompleteFrameLayout.visibility = View.INVISIBLE
59-
6048
val meFragment = MeFragment()
6149
supportFragmentManager
6250
.beginTransaction()

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

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,47 @@ package com.google.samples.quickstart.canonical
22

33
import android.Manifest
44
import android.app.Activity
5+
import android.content.ContentValues.TAG
56
import android.content.pm.PackageManager
67
import android.location.Location
78
import androidx.fragment.app.Fragment
89

910
import android.os.Bundle
11+
import android.util.Log
1012
import android.view.LayoutInflater
1113
import android.view.View
1214
import android.view.ViewGroup
15+
import android.widget.LinearLayout
1316
import androidx.core.content.ContextCompat.checkSelfPermission
1417
import com.google.android.gms.location.FusedLocationProviderClient
1518
import com.google.android.gms.location.LocationServices
19+
import com.google.android.gms.common.api.Status
1620

1721
import com.google.android.gms.maps.CameraUpdateFactory
1822
import com.google.android.gms.maps.GoogleMap
1923
import com.google.android.gms.maps.OnMapReadyCallback
2024
import com.google.android.gms.maps.SupportMapFragment
2125
import com.google.android.gms.maps.model.LatLng
26+
import com.google.android.gms.maps.model.Marker
2227
import com.google.android.gms.maps.model.MarkerOptions
28+
import com.google.android.libraries.places.api.Places
29+
import com.google.android.libraries.places.api.model.Place
30+
import com.google.android.libraries.places.api.model.RectangularBounds
31+
import com.google.android.libraries.places.api.net.PlacesClient
32+
import com.google.android.libraries.places.widget.AutocompleteSupportFragment
33+
import com.google.android.libraries.places.widget.listener.PlaceSelectionListener
2334

2435
class MapsFragment : Fragment() {
2536

2637
private lateinit var map: GoogleMap
2738
private lateinit var fusedLocationClient: FusedLocationProviderClient
2839
private lateinit var lastLocation: Location
40+
private lateinit var placesClient : PlacesClient
41+
private lateinit var autocompleteLayout : LinearLayout
42+
private lateinit var targetLatLng : LatLng
43+
private lateinit var targetName : String
44+
private var currentLatLng : LatLng? = null
45+
private var targetMarker : Marker? = null
2946

3047
companion object {
3148
private const val LOCATION_PERMISSION_REQUEST_CODE = 1
@@ -51,27 +68,65 @@ class MapsFragment : Fragment() {
5168
}
5269
}
5370

71+
private fun initPlaces() {
72+
context?.let { Places.initialize(it, getString(R.string.google_maps_key)) }
73+
placesClient = context?.let { Places.createClient(it) }!!
74+
}
75+
76+
private fun setPlacesSearchBias() {
77+
val autocompleteFragment = childFragmentManager.findFragmentById(R.id.autocomplete_fragment) as AutocompleteSupportFragment
78+
// Search nearby result
79+
currentLatLng?.let{
80+
Log.i(TAG, "currentLatLng")
81+
autocompleteFragment.setLocationBias(
82+
RectangularBounds.newInstance(
83+
LatLng(currentLatLng!!.latitude - 1, currentLatLng!!.longitude - 1),
84+
LatLng(currentLatLng!!.latitude + 1, currentLatLng!!.longitude + 1)
85+
))
86+
}
87+
}
88+
5489
private fun setUpMap() {
5590
if (checkSelfPermission(context as Activity,
5691
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
5792
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), LOCATION_PERMISSION_REQUEST_CODE)
5893
return
5994
}
6095

96+
// Add and adjust the position of MyLocation button.
6197
map.isMyLocationEnabled = true
98+
map.setPadding(0,(1.5 * autocompleteLayout.height).toInt(),0,0)
6299

63100
fusedLocationClient.lastLocation.addOnSuccessListener(this.activity as Activity) { location ->
64101
// Got last known location. In some rare situations this can be null.
65102
if (location != null) {
66103
lastLocation = location
67-
val currentLatLng = LatLng(location.latitude, location.longitude)
68-
map.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 12f))
69-
map.addMarker(MarkerOptions().position(currentLatLng).title("My location"))
70-
map.moveCamera(CameraUpdateFactory.newLatLng(currentLatLng))
104+
currentLatLng = LatLng(location.latitude, location.longitude)
105+
map.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 14f))
106+
map.addMarker(MarkerOptions()
107+
.position(currentLatLng!!)
108+
.title("My location"))
109+
map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 14f))
110+
setPlacesSearchBias()
71111
}
72112
}
73113
}
74114

115+
private fun setUpAutocomplete(autocompleteFragment : AutocompleteSupportFragment, mapFragment : SupportMapFragment) {
116+
autocompleteFragment.setPlaceFields(listOf(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG))
117+
autocompleteFragment.setOnPlaceSelectedListener(object : PlaceSelectionListener {
118+
override fun onPlaceSelected(place: Place) {
119+
targetLatLng = place.latLng!!
120+
targetName = place.name.toString()
121+
mapFragment.getMapAsync(searchPlacesCallback)
122+
}
123+
124+
override fun onError(status: Status) {
125+
Log.e(TAG, "An error occurred: $status")
126+
}
127+
})
128+
}
129+
75130

76131
private val mapReadyCallback = OnMapReadyCallback { googleMap ->
77132
/**
@@ -86,6 +141,16 @@ class MapsFragment : Fragment() {
86141
map = googleMap
87142
map.uiSettings.isZoomControlsEnabled = true
88143
setUpMap()
144+
145+
}
146+
147+
private val searchPlacesCallback = OnMapReadyCallback { map ->
148+
targetMarker?.remove()
149+
map.moveCamera(CameraUpdateFactory.newLatLngZoom(targetLatLng, 14f))
150+
targetMarker = map.addMarker(MarkerOptions()
151+
.position(targetLatLng)
152+
.title(targetName)
153+
.draggable(true))
89154
}
90155

91156
override fun onCreateView(
@@ -99,7 +164,13 @@ class MapsFragment : Fragment() {
99164

100165
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
101166
super.onViewCreated(view, savedInstanceState)
102-
val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
103-
mapFragment?.getMapAsync(mapReadyCallback)
167+
initPlaces()
168+
169+
val mapFragment = childFragmentManager.findFragmentById(R.id.map_fragment) as SupportMapFragment
170+
val autocompleteFragment = childFragmentManager.findFragmentById(R.id.autocomplete_fragment) as AutocompleteSupportFragment
171+
autocompleteLayout = view.findViewById(R.id.autocomplete_linearLayout)
172+
mapFragment.getMapAsync(mapReadyCallback)
173+
174+
setUpAutocomplete(autocompleteFragment, mapFragment)
104175
}
105176
}

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,5 @@
2323
app:layout_constraintTop_toTopOf="parent">
2424

2525
</FrameLayout>
26-
<FrameLayout
27-
android:id="@+id/autocomplete_container"
28-
android:layout_width="match_parent"
29-
android:layout_height="wrap_content"
30-
android:measureAllChildren="true"
31-
android:background="@android:color/white"
32-
app:layout_constraintTop_toTopOf="parent">
33-
34-
</FrameLayout>
35-
3626

3727
</androidx.constraintlayout.widget.ConstraintLayout>

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

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<fragment
3-
xmlns:android="http://schemas.android.com/apk/res/android"
4-
xmlns:tools="http://schemas.android.com/tools"
5-
android:id="@+id/map"
6-
android:name="com.google.android.gms.maps.SupportMapFragment"
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
73
android:layout_width="match_parent"
8-
android:layout_height="match_parent"
9-
tools:context=".MapsFragment" />
4+
android:layout_height="match_parent">
5+
6+
<fragment
7+
android:layout_width="match_parent"
8+
android:layout_height="match_parent"
9+
android:id="@+id/map_fragment"
10+
android:name="com.google.android.gms.maps.SupportMapFragment" />
11+
12+
<LinearLayout
13+
android:orientation="vertical"
14+
android:layout_margin="20dp"
15+
android:layout_width="match_parent"
16+
android:layout_height="wrap_content"
17+
android:id="@+id/autocomplete_linearLayout"
18+
android:background="@android:color/white">
19+
20+
<fragment
21+
android:layout_width="match_parent"
22+
android:layout_height="wrap_content"
23+
android:id="@+id/autocomplete_fragment"
24+
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment" />
25+
</LinearLayout>
26+
</RelativeLayout>

0 commit comments

Comments
 (0)