Create and save a map as a web map item to an ArcGIS portal.
Use case
Maps can be created programmatically in code and then serialized and saved as an ArcGIS portal item. In this case, the portal item is a web map which can be shared with others and opened in various applications and APIs throughout the platform, such as ArcGIS Pro, ArcGIS Online, the JavaScript API, Collector, and Explorer.
How to use the sample
When you run the sample, you will be challenged for an ArcGIS Online login. Enter a username and password for an ArcGIS Online named user account (such as your ArcGIS for Developers account). Then, tap the Edit Map button to choose the basemap and layers for your new map. To save the map, add a title, tags and description (optional), and a folder on your portal (you will need to create one in your portal's My Content section if you don't already have one). Click the Save to Account button to save the map to the chosen folder.
How it works
- Configure an
AuthenticatorState
to handle authentication challenges. - Add the
AuthenticatorState
to aDialogAuthenticator
in the app'sMainActivity
to invoke the authentication challenge. - Create a new
Portal
and load it. - Access the
PortalUserContent
withportal.portalInfo?.user?.fetchContent()?.onSuccess
, to get the user's list of portal folders withportalUserContent.folders
. - Create an
ArcGISMap
with aBasemapStyle
and a few operational layers. - Call
ArcGISMap.saveMap()
to save a newArcGISMap
with the specified title, tags, and folder to the portal.
Relevant API
- ArcGISMap
- Portal
Additional information
This sample uses the GeoViewCompose Toolkit module to be able to implement a Composable MapView.
Tags
ArcGIS Online, ArcGIS Pro, geoviewcompose, portal, publish, share, web map
Sample Code
/* Copyright 2024 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.createandsavemap
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.lifecycle.viewmodel.compose.viewModel
import com.arcgismaps.toolkit.authentication.DialogAuthenticator
import com.esri.arcgismaps.sample.createandsavemap.components.MapViewModel
import com.esri.arcgismaps.sample.createandsavemap.screens.MainScreen
import com.esri.arcgismaps.sample.sampleslib.theme.SampleAppTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
SampleAppTheme {
SampleApp()
}
}
}
@Composable
private fun SampleApp() {
val mapViewModel: MapViewModel = viewModel()
Surface(
color = MaterialTheme.colorScheme.background
) {
MainScreen(
sampleName = getString(R.string.create_and_save_map_app_name)
)
}
// authenticator at bottom can draw over the top of the sample
DialogAuthenticator(authenticatorState = mapViewModel.authenticatorState)
}
}