Skip to content

Commit ab564f9

Browse files
authored
Merge pull request #13 from mapbox/as/add-android-location-search
Android Location Search Tutorial
2 parents 9a879af + c7e8bd5 commit ab564f9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1258
-0
lines changed

android-location-search/.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
plugins {
2+
alias(libs.plugins.android.application)
3+
alias(libs.plugins.kotlin.android)
4+
}
5+
6+
android {
7+
namespace = "com.example.androidlocationsearch"
8+
compileSdk = 35
9+
10+
defaultConfig {
11+
applicationId = "com.example.androidlocationsearch"
12+
minSdk = 24
13+
targetSdk = 34
14+
versionCode = 1
15+
versionName = "1.0"
16+
17+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
18+
vectorDrawables {
19+
useSupportLibrary = true
20+
}
21+
}
22+
23+
buildTypes {
24+
release {
25+
isMinifyEnabled = false
26+
proguardFiles(
27+
getDefaultProguardFile("proguard-android-optimize.txt"),
28+
"proguard-rules.pro"
29+
)
30+
}
31+
}
32+
compileOptions {
33+
sourceCompatibility = JavaVersion.VERSION_1_8
34+
targetCompatibility = JavaVersion.VERSION_1_8
35+
}
36+
kotlinOptions {
37+
jvmTarget = "1.8"
38+
}
39+
buildFeatures {
40+
compose = true
41+
}
42+
composeOptions {
43+
kotlinCompilerExtensionVersion = "1.5.1"
44+
}
45+
packaging {
46+
resources {
47+
excludes += "/META-INF/{AL2.0,LGPL2.1}"
48+
}
49+
}
50+
}
51+
52+
dependencies {
53+
// Search SDK dependencies
54+
implementation("com.mapbox.search:autofill:2.14.0-beta.1")
55+
implementation("com.mapbox.search:discover:2.14.0-beta.1")
56+
implementation("com.mapbox.search:place-autocomplete:2.14.0-beta.1")
57+
implementation("com.mapbox.search:offline:2.14.0-beta.1")
58+
implementation("com.mapbox.search:mapbox-search-android:2.14.0-beta.1")
59+
implementation("com.mapbox.search:mapbox-search-android-ui:2.14.0-beta.1")
60+
// If you're using compose also add the compose extension
61+
implementation(platform(libs.androidx.compose.bom))
62+
implementation("com.mapbox.extension:maps-compose:11.13.3")
63+
implementation("com.mapbox.maps:android:11.13.3")
64+
implementation(libs.androidx.core.ktx)
65+
implementation(libs.androidx.lifecycle.runtime.ktx)
66+
implementation(libs.androidx.activity.compose)
67+
implementation(platform(libs.androidx.compose.bom))
68+
implementation("androidx.compose.foundation:foundation:1.6.4")
69+
implementation("androidx.compose.ui:ui-text:1.6.4")
70+
implementation(libs.androidx.ui)
71+
implementation(libs.androidx.ui.graphics)
72+
implementation(libs.androidx.ui.tooling.preview)
73+
implementation(libs.androidx.material3)
74+
testImplementation(libs.junit)
75+
androidTestImplementation(libs.androidx.junit)
76+
androidTestImplementation(libs.androidx.espresso.core)
77+
androidTestImplementation(platform(libs.androidx.compose.bom))
78+
androidTestImplementation(libs.androidx.ui.test.junit4)
79+
debugImplementation(libs.androidx.ui.tooling)
80+
debugImplementation(libs.androidx.ui.test.manifest)
81+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.androidlocationsearch
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.example.mapsinstallguide", appContext.packageName)
23+
}
24+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
6+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
7+
8+
<application
9+
android:allowBackup="true"
10+
android:dataExtractionRules="@xml/data_extraction_rules"
11+
android:fullBackupContent="@xml/backup_rules"
12+
android:icon="@mipmap/ic_launcher"
13+
android:label="@string/app_name"
14+
android:roundIcon="@mipmap/ic_launcher_round"
15+
android:supportsRtl="true"
16+
android:theme="@style/Theme.AndroidLocationSearch"
17+
tools:targetApi="31">
18+
<activity
19+
android:name="com.example.androidlocationsearch.MainActivity"
20+
android:exported="true"
21+
android:label="@string/app_name"
22+
android:theme="@style/Theme.AndroidLocationSearch">
23+
<intent-filter>
24+
<action android:name="android.intent.action.MAIN" />
25+
26+
<category android:name="android.intent.category.LAUNCHER" />
27+
</intent-filter>
28+
</activity>
29+
</application>
30+
31+
</manifest>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.example.androidlocationsearch
2+
3+
import android.os.Bundle
4+
import androidx.activity.ComponentActivity
5+
import androidx.activity.compose.setContent
6+
import androidx.compose.foundation.layout.*
7+
import androidx.compose.ui.Alignment
8+
import androidx.compose.ui.ExperimentalComposeUiApi
9+
import androidx.compose.ui.Modifier
10+
import androidx.compose.ui.zIndex
11+
import com.mapbox.geojson.Point
12+
import com.mapbox.maps.extension.compose.MapboxMap
13+
import com.mapbox.maps.extension.compose.animation.viewport.rememberMapViewportState
14+
import androidx.compose.runtime.*
15+
import androidx.compose.ui.res.painterResource
16+
import com.mapbox.maps.extension.compose.annotation.generated.PointAnnotation
17+
import com.mapbox.maps.extension.compose.annotation.rememberIconImage
18+
import com.mapbox.search.result.SearchResult
19+
20+
@OptIn(ExperimentalComposeUiApi::class)
21+
22+
class MainActivity : ComponentActivity() {
23+
24+
25+
override fun onCreate(savedInstanceState: Bundle?) {
26+
super.onCreate(savedInstanceState)
27+
28+
setContent {
29+
val selectedResult = remember { mutableStateOf<SearchResult?>(null) }
30+
val mapViewportState = rememberMapViewportState {
31+
setCameraOptions {
32+
center(Point.fromLngLat(-98.0, 39.5))
33+
zoom(2.0)
34+
}
35+
}
36+
37+
Box(Modifier.fillMaxSize()) {
38+
MapboxMap(
39+
Modifier.fillMaxSize(),
40+
mapViewportState = mapViewportState,
41+
scaleBar = {}, // disables the scale bar by rendering nothing
42+
compass = {} // disable the compass
43+
) {
44+
45+
selectedResult.value?.coordinate?.let { coord ->
46+
val marker = rememberIconImage(
47+
key = R.drawable.map_marker,
48+
painter = painterResource(id = R.drawable.map_marker)
49+
)
50+
PointAnnotation(point = coord) {
51+
iconImage = marker
52+
}
53+
}
54+
}
55+
56+
SearchScreen(
57+
mapViewportState = mapViewportState,
58+
onSuggestionSelected = { result ->
59+
selectedResult.value = result
60+
},
61+
modifier = Modifier
62+
.align(Alignment.TopCenter)
63+
.zIndex(1f)
64+
)
65+
}
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)