1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* Copyright 2022 Esri
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/package com.esri.arcgismaps.sample.changeviewpoint
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.lifecycleScope
import com.arcgismaps.ApiKey
import com.arcgismaps.ArcGISEnvironment
import com.arcgismaps.geometry.Point
import com.arcgismaps.geometry.PolylineBuilder
import com.arcgismaps.geometry.SpatialReference
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.mapping.Viewpoint
import com.esri.arcgismaps.sample.changeviewpoint.databinding.ActivityMainBinding
import kotlinx.coroutines.launch
classMainActivity : AppCompatActivity() {
privateval viewpointScale = 5000.0// set up data binding for the activityprivateval activityMainBinding: ActivityMainBinding by lazy {
DataBindingUtil.setContentView(this, R.layout.activity_main)
}
privateval mapView by lazy {
activityMainBinding.mapView
}
overridefunonCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// authentication with an API key or named user is// required to access basemaps and other location services ArcGISEnvironment.apiKey = ApiKey.create(BuildConfig.API_KEY)
// add the MapView to the lifecycle lifecycle.addObserver(mapView)
// create and add a map with a imagery basemap style mapView.map = ArcGISMap(BasemapStyle.ArcGISImagery)
// set the start point of the ViewPointval startPoint = Point(-14093.0, 6711377.0, SpatialReference.webMercator())
lifecycleScope.launch {
// set viewpoint of map view to starting point and scale mapView.setViewpointCenter(startPoint, viewpointScale)
}
}
funonGeometryClicked(view: View) {
// create a collection of points around Westminsterval westminsterPolylineBuilder = PolylineBuilder(SpatialReference.webMercator()) {
addPoint(Point(-13823.0, 6710390.0))
addPoint(Point(-13823.0, 6710150.0))
addPoint(Point(-14680.0, 6710390.0))
addPoint(Point(-14680.0, 6710150.0))
}
val geometry = westminsterPolylineBuilder.toGeometry()
// set the map view's viewpoint to Westminster lifecycleScope.launch {
mapView.setViewpointGeometry(geometry)
}
}
funonCenterClicked(view: View) {
// create the Waterloo location pointval waterlooPoint = Point(-12153.0, 6710527.0, SpatialReference.webMercator())
// set the map view's viewpoint centered on Waterloo and scaled lifecycleScope.launch {
mapView.setViewpointCenter(waterlooPoint, viewpointScale)
}
}
funonAnimateClicked(view: View) {
// create the London location pointval londonPoint = Point(-14093.0, 6711377.0, SpatialReference.webMercator())
// create the viewpoint with the London point and scaleval viewpoint = Viewpoint(londonPoint, viewpointScale)
// set the map view's viewpoint to London with a seven second animation duration lifecycleScope.launch {
mapView.setViewpointAnimated(viewpoint, 7f)
}
}
}