Skip to content

Commit b23c0d3

Browse files
committed
Merge branch 'main' into sample/Download-vector-tiles-to-local-cache
2 parents 5d70ee8 + 5670329 commit b23c0d3

File tree

134 files changed

+3599
-2
lines changed

Some content is hidden

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

134 files changed

+3599
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Find address with reverse geocode
2+
3+
Use an online service to find the address for a tapped point.
4+
5+
![Image of find address with reverse geocode](find-address-with-reverse-geocode.png)
6+
7+
## Use case
8+
9+
You might use a geocoder to find a customer's delivery address based on the location returned by their device's GPS.
10+
11+
## How to use the sample
12+
13+
Tap the map to see the nearest address displayed as a text view.
14+
15+
## How it works
16+
17+
1. Create a `LocatorTask` object using a URL to a geocoder service.
18+
2. Get the matching results from the `GeocodeResult` using `LocatorTask.reverseGeocode`.
19+
3. Show the results using a `PictureMarkerSymbol` and add the symbol to a `Graphic` in the `GraphicsOverlay`.
20+
21+
## Relevant API
22+
23+
* GeocodeParameters
24+
* GeocodeResult
25+
* LocatorTask
26+
* ReverseGeocodeParameters
27+
28+
## Additional information
29+
30+
This sample uses the World Geocoding Service. For more information, see the [Geocoding service](https://developers.arcgis.com/documentation/mapping-apis-and-services/search/services/geocoding-service/) help topic on the ArcGIS Developer website.
31+
32+
## Tags
33+
34+
address, geocode, locate, reverse geocode, search
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"category": "Search and Query",
3+
"description": "Use an online service to find the address for a tapped point.",
4+
"formal_name": "FindAddressWithReverseGeocode",
5+
"ignore": false,
6+
"images": [
7+
"find-address-with-reverse-geocode.png"
8+
],
9+
"keywords": [
10+
"address",
11+
"geocode",
12+
"locate",
13+
"reverse geocode",
14+
"search",
15+
"GeocodeParameters",
16+
"GeocodeResult",
17+
"LocatorTask",
18+
"ReverseGeocodeParameters"
19+
],
20+
"language": "kotlin",
21+
"redirect_from": "",
22+
"relevant_apis": [
23+
"GeocodeParameters",
24+
"GeocodeResult",
25+
"LocatorTask",
26+
"ReverseGeocodeParameters"
27+
],
28+
"snippets": [
29+
"src/main/java/com/esri/arcgismaps/sample/findaddresswithreversegeocode/MainActivity.kt"
30+
],
31+
"title": "Find address with reverse geocode"
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'org.jetbrains.kotlin.android'
3+
4+
android {
5+
compileSdkVersion rootProject.ext.compileSdkVersion
6+
7+
defaultConfig {
8+
applicationId "com.esri.arcgismaps.sample.findaddresswithreversegeocode"
9+
minSdkVersion rootProject.ext.minSdkVersion
10+
targetSdkVersion rootProject.ext.targetSdkVersion
11+
versionCode rootProject.ext.versionCode
12+
versionName rootProject.ext.versionName
13+
buildConfigField("String", "API_KEY", API_KEY)
14+
}
15+
16+
buildTypes {
17+
release {
18+
minifyEnabled false
19+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20+
}
21+
}
22+
23+
buildFeatures {
24+
dataBinding true
25+
}
26+
}
27+
28+
dependencies {
29+
// lib dependencies from rootProject build.gradle
30+
implementation "androidx.constraintlayout:constraintlayout:$constraintLayoutVersion"
31+
implementation "com.google.android.material:material:$materialVersion"
32+
}
373 KB
Loading
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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.esri.arcgismaps.sample.findaddresswithreversegeocode">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
7+
<application
8+
android:allowBackup="true"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:theme="@style/AppTheme">
14+
<activity
15+
android:exported="true"
16+
android:name=".MainActivity"
17+
android:label="@string/app_name">
18+
<intent-filter>
19+
<action android:name="android.intent.action.MAIN" />
20+
21+
<category android:name="android.intent.category.LAUNCHER" />
22+
</intent-filter>
23+
</activity>
24+
</application>
25+
26+
</manifest>
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/* Copyright 2022 Esri
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
*/
16+
17+
package com.esri.arcgismaps.sample.findaddresswithreversegeocode
18+
19+
import android.graphics.drawable.BitmapDrawable
20+
import android.os.Bundle
21+
import android.util.Log
22+
import androidx.appcompat.app.AppCompatActivity
23+
import androidx.core.content.ContextCompat
24+
import androidx.databinding.DataBindingUtil
25+
import androidx.lifecycle.lifecycleScope
26+
import com.arcgismaps.ApiKey
27+
import com.arcgismaps.ArcGISEnvironment
28+
import com.arcgismaps.geometry.GeometryEngine
29+
import com.arcgismaps.geometry.Point
30+
import com.arcgismaps.mapping.ArcGISMap
31+
import com.arcgismaps.mapping.BasemapStyle
32+
import com.arcgismaps.mapping.Viewpoint
33+
import com.arcgismaps.mapping.symbology.PictureMarkerSymbol
34+
import com.arcgismaps.mapping.view.Graphic
35+
import com.arcgismaps.mapping.view.GraphicsOverlay
36+
import com.arcgismaps.tasks.geocode.LocatorTask
37+
import com.esri.arcgismaps.sample.findaddresswithreversegeocode.databinding.ActivityMainBinding
38+
import com.google.android.material.snackbar.Snackbar
39+
import kotlinx.coroutines.launch
40+
41+
class MainActivity : AppCompatActivity() {
42+
43+
private val TAG = MainActivity::class.java.simpleName
44+
45+
// service url to be provided to the LocatorTask (geocoder)
46+
private val GEOCODE_SERVER =
47+
"https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer"
48+
49+
// set up data binding for the activity
50+
private val activityMainBinding: ActivityMainBinding by lazy {
51+
DataBindingUtil.setContentView(this, R.layout.activity_main)
52+
}
53+
54+
// create a MapView using binding
55+
private val mapView by lazy {
56+
activityMainBinding.mapView
57+
}
58+
59+
// display the street of the tapped location
60+
private val titleTV by lazy {
61+
activityMainBinding.titleTV
62+
}
63+
64+
// display the metro area of the tapped location
65+
private val descriptionTV by lazy {
66+
activityMainBinding.descriptionTV
67+
}
68+
69+
// set the pin graphic for tapped location
70+
private val pinSymbol by lazy {
71+
createPinSymbol()
72+
}
73+
74+
// create a graphics overlay
75+
private val graphicsOverlay = GraphicsOverlay()
76+
77+
// locator task to provide geocoding services
78+
private val locatorTask = LocatorTask(GEOCODE_SERVER)
79+
80+
override fun onCreate(savedInstanceState: Bundle?) {
81+
super.onCreate(savedInstanceState)
82+
83+
// authentication with an API key or named user is
84+
// required to access basemaps and other location services
85+
ArcGISEnvironment.apiKey = ApiKey.create(BuildConfig.API_KEY)
86+
lifecycle.addObserver(mapView)
87+
88+
mapView.apply {
89+
// add a map with a imagery basemap style
90+
map = ArcGISMap(BasemapStyle.ArcGISImagery)
91+
// add a graphics overlay to the map for showing where the user tapped
92+
graphicsOverlays.add(graphicsOverlay)
93+
// set initial viewpoint
94+
setViewpoint(Viewpoint(34.058, -117.195, 5e4))
95+
}
96+
97+
lifecycleScope.launch {
98+
// load geocode locator task
99+
locatorTask.load().onSuccess {
100+
// locator task loaded, look for geo view tapped
101+
mapView.onSingleTapConfirmed.collect { event ->
102+
event.mapPoint?.let { mapPoint -> geoViewTapped(mapPoint) }
103+
}
104+
}.onFailure {
105+
showError(it.message.toString())
106+
}
107+
}
108+
}
109+
110+
/**
111+
* Displays a pin of the tapped location using [mapPoint]
112+
* and finds address with reverse geocode
113+
*/
114+
private suspend fun geoViewTapped(mapPoint: Point) {
115+
// create graphic for tapped point
116+
val pinGraphic = Graphic(mapPoint, pinSymbol)
117+
graphicsOverlay.graphics.apply {
118+
// clear existing graphics
119+
clear()
120+
// add the pin graphic
121+
add(pinGraphic)
122+
}
123+
// normalize the geometry - needed if the user crosses the international date line.
124+
val normalizedPoint = GeometryEngine.normalizeCentralMeridian(mapPoint) as Point
125+
// reverse geocode to get address
126+
locatorTask.reverseGeocode(normalizedPoint).onSuccess { addresses ->
127+
// get the first result
128+
val address = addresses.firstOrNull()
129+
if (address == null) {
130+
showError("Could not find address at tapped point")
131+
return@onSuccess
132+
}
133+
// use the street and region for the title
134+
val title = address.attributes["Address"].toString()
135+
// use the metro area for the description details
136+
val description = "${address.attributes["City"]} " +
137+
"${address.attributes["Region"]} " +
138+
"${address.attributes["CountryCode"]}"
139+
// set the strings to the text views
140+
titleTV.text = title
141+
descriptionTV.text = description
142+
}.onFailure {
143+
showError(it.message.toString())
144+
}
145+
}
146+
147+
/**
148+
* Create a picture marker symbol to represent a pin at the tapped location
149+
*/
150+
private fun createPinSymbol(): PictureMarkerSymbol {
151+
// get pin drawable
152+
val pinDrawable = ContextCompat.getDrawable(
153+
this,
154+
R.drawable.baseline_location_pin_red_48
155+
)
156+
//add a graphic for the tapped point
157+
val pinSymbol = PictureMarkerSymbol(
158+
pinDrawable as BitmapDrawable
159+
)
160+
pinSymbol.apply {
161+
// resize the dimensions of the symbol
162+
width = 50f
163+
height = 50f
164+
// the image is a pin so offset the image so that the pinpoint
165+
// is on the point rather than the image's true center
166+
leaderOffsetX = 30f
167+
offsetY = 25f
168+
}
169+
return pinSymbol
170+
}
171+
172+
private fun showError(errorMessage: String) {
173+
Log.e(TAG, errorMessage)
174+
Snackbar.make(mapView, errorMessage, Snackbar.LENGTH_SHORT).show()
175+
}
176+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:aapt="http://schemas.android.com/aapt"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportHeight="108"
6+
android:viewportWidth="108">
7+
<path
8+
android:fillType="evenOdd"
9+
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
10+
android:strokeColor="#00000000"
11+
android:strokeWidth="1">
12+
<aapt:attr name="android:fillColor">
13+
<gradient
14+
android:endX="78.5885"
15+
android:endY="90.9159"
16+
android:startX="48.7653"
17+
android:startY="61.0927"
18+
android:type="linear">
19+
<item
20+
android:color="#44000000"
21+
android:offset="0.0" />
22+
<item
23+
android:color="#00000000"
24+
android:offset="1.0" />
25+
</gradient>
26+
</aapt:attr>
27+
</path>
28+
<path
29+
android:fillColor="#FFFFFF"
30+
android:fillType="nonZero"
31+
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
32+
android:strokeColor="#00000000"
33+
android:strokeWidth="1" />
34+
</vector>
4.34 KB
Loading

0 commit comments

Comments
 (0)