How it works
You can open the MainActivity.kt file to see how to access and display
the number of votes for a social event while displaying an interactive map.
1 override fun onCreate(savedInstanceState: Bundle?)
2 {
3 super.onCreate(savedInstanceState)
4 setContentView(R.layout.activity_main)
5 progressBar = findViewById(R.id.progress_bar)
6 gemSurfaceView = findViewById(R.id.gem_surface_view)
7 eventVotingContainer = findViewById(R.id.event_voting_container)
8 icon = findViewById(R.id.icon)
9 text = findViewById(R.id.text)
10 time = findViewById(R.id.time)
11 score = findViewById(R.id.score)
12 thumbUpButton = findViewById(R.id.thumb_up_button)
13 pleaseSelectText = findViewById(R.id.please_select_text)
14 SdkSettings.onMapDataReady = onMapDataReady@{ isReady ->
15 if (!isReady) return@onMapDataReady
16 // Defines an action that should be done when the world map is ready (Updated/loaded).
17 gemSurfaceView.mapView?.onTouch = { xy ->
18 SdkCall.execute {
19 gemSurfaceView.mapView?.cursorScreenPosition = xy
20 val overlays = gemSurfaceView.mapView?.cursorSelectionOverlayItems
21 if (!overlays.isNullOrEmpty())
22 {
23 val overlay = overlays[0]
24 if (overlay.overlayInfo?.uid == ECommonOverlayId.SocialReports.value)
25 {
26 Util.postOnMain { showVotingPanel(overlay) }
27 return@execute
28 }
29 }
30 Util.postOnMain { hideVotingPanel() }
31 }
32 }
33 }
34 SdkSettings.onApiTokenRejected = {
35 showDialog("TOKEN REJECTED")
36 }
37 if (!Util.isInternetConnected(this))
38 {
39 showDialog("You must be connected to the internet!")
40 }
41}
The onCreate()
function is overridden in the
MainActivity: AppCompatActivity()
class, and checks if
internet access is available, showing a dialog message if not.
findViewById()
is used to obtain pointers to the various
graphical user interface elements where text or graphical data
is to be displayed.
When the map is loaded,
SdkSettings.onMapDataReady =
a touch handler is defined to get the screen position where a
user tap occurred:
gemSurfaceView.mapView?.onTouch = { xy ->
then the xy screen position is set so the mapView
can use it
to search for map elements near that position:
gemSurfaceView.mapView?.cursorScreenPosition = xy
When the user taps on the map, the overlay items, if any, near
the tap position are obtained:
val overlays = gemSurfaceView.mapView?.cursorSelectionOverlayItems
and if the array is not empty, then the first item, at index 0,
is selected:
val overlay = overlays[0]
and if it is a social reports item,
if (overlay.overlayInfo?.uid == ECommonOverlayId.SocialReports.value)
then the voting panel is displayed:
Util.postOnMain { showVotingPanel(overlay) }
1private fun showVotingPanel(overlay: OverlayItem)
2{
3 if (!eventVotingContainer.isVisible)
4 {
5 eventVotingContainer.visibility = View.VISIBLE
6 pleaseSelectText.visibility = View.GONE
7 }
8 var bitmap: Bitmap? = null
9 var textStr = ""
10 var timeStr = ""
11 var scoreStr = ""
12 var thumbUpBitmap: Bitmap? = null
13 val eventImageSize = resources.getDimension(R.dimen.event_image_size).toInt()
14 val imageSize = resources.getDimension(R.dimen.image_size).toInt()
15 SdkCall.execute {
16 bitmap = overlay.image?.asBitmap(eventImageSize, eventImageSize)
17 textStr = overlay.name.toString()
18 scoreStr = overlay.getPreviewData()?.find { it.key == "score" }?.valueString.toString()
19 val stamp = overlay.getPreviewData()?.find { it.key == "create_stamp_utc" }?.valueLong ?: 0
20 val eventTime = Calendar.getInstance(Locale.getDefault()).also { it.timeInMillis = stamp * 1000 }
21 val now = Calendar.getInstance(Locale.getDefault()).also { it.timeInMillis = System.currentTimeMillis() }
22 val dateFormat = if (eventTime.get(Calendar.YEAR) == now.get(Calendar.YEAR) &&
23 eventTime.get(Calendar.MONTH) == now.get(Calendar.MONTH) &&
24 eventTime.get(Calendar.DAY_OF_MONTH) == now.get(Calendar.DAY_OF_MONTH))
25 {
26 "HH:mm"
27 }
28 else { "dd/MM/yyyy" }
29 timeStr = SimpleDateFormat(dateFormat, Locale.getDefault()).format(Date(eventTime.timeInMillis))
30 if (overlay.getPreviewData()?.find { it.key == "allow_thumb" }?.valueBoolean == true)
31 {
32 thumbUpBitmap =
33 GemUtilImages.asBitmap(ImageDatabase().getImageById(SdkImages.SocialReports.Social_Thumbs_Up.value),
34 imageSize, imageSize)
35 }
36 }
37 icon.setImageBitmap(bitmap)
38 text.text = textStr
39 time.text = timeStr
40 score.text = scoreStr
41 if (thumbUpBitmap != null)
42 {
43 thumbUpButton.apply {
44 visibility = View.VISIBLE
45 setImageBitmap(thumbUpBitmap)
46 setOnClickListener {
47 val errorCode = SdkCall.execute
48 { SocialOverlay.confirmReport(overlay, ProgressListener()) } ?: -1
49 if (errorCode < 0)
50 {
51 showDialog("Confirm report failed with error: ${GemError.getMessage(errorCode)}")
52 }
53 eventVotingContainer.visibility = View.GONE
54 }
55 }
56 }
57 else { thumbUpButton.visibility = View.GONE }
58}
The showVotingPanel()
function first makes the voting container
panel visible, if it is not.
eventVotingContainer.visibility = View.VISIBLE
Next, the time of the event is converted to hours and minutes, if
it occurred today, or in day, month and year format otherwise.
The icon image associated with the event is
icon.setImageBitmap(bitmap)
;
the number of votes displayed in the upper right corner of the
panel is the score.text = scoreStr
whereas the name of the
event is text.text = textStr
and the
date/time is time.text = timeStr
A click listener is set for the panel,
setOnClickListener {
and the panel is made to disappear when the user clicks on it:
eventVotingContainer.visibility = View.GONE
Social Event Voting¶
Setup¶
Download the
SocialEventVoting project
archive file or clone the project with GitSee the Configure Android Example guide.
Run the example¶
In Android Studio, from the
File
menu, selectSync Project with Gradle Files
How it works¶
onCreate()
function is overridden in theMainActivity: AppCompatActivity()
class, and checks if internet access is available, showing a dialog message if not.findViewById()
is used to obtain pointers to the various graphical user interface elements where text or graphical data is to be displayed.SdkSettings.onMapDataReady =
gemSurfaceView.mapView?.onTouch = { xy ->
mapView
can use it to search for map elements near that position:gemSurfaceView.mapView?.cursorScreenPosition = xy
val overlays = gemSurfaceView.mapView?.cursorSelectionOverlayItems
val overlay = overlays[0]
if (overlay.overlayInfo?.uid == ECommonOverlayId.SocialReports.value)
Util.postOnMain { showVotingPanel(overlay) }
showVotingPanel()
function first makes the voting container panel visible, if it is not.eventVotingContainer.visibility = View.VISIBLE
icon.setImageBitmap(bitmap)
;score.text = scoreStr
whereas the name of the event istext.text = textStr
and the date/time istime.text = timeStr
setOnClickListener {
eventVotingContainer.visibility = View.GONE
Android Examples¶
Maps SDK for Android Examples can be downloaded or cloned with Git