fix: Fix v18.0.0 build errors (#740)

* fix: Fix build errors from v18.0.0

Change-Id: Id4a2b5edf71832ffe71e2296de979b9a48b7786a

* Update CI to only build GMS on non-snippet code.

Change-Id: I1cbc08546758ec1ae40851777efdbe2ac2e941a4

* Remove redundant snippet end tag.

Change-Id: I8101762c705a31af6c63deec91152246e19b9622

* Fix nullability errors.

Change-Id: I3e535c0389586bf69a6780c5ce46c813e2b3241f
This commit is contained in:
Chris Arriola 2021-10-27 13:21:21 -05:00 committed by GitHub
parent cd44d81c3c
commit 4ee2170919
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 195 additions and 65 deletions

View File

@ -49,7 +49,7 @@ jobs:
- name: Build and check
run: |
cd ApiDemos
for dir in ./*/ ; do ( cd "$dir" && ./gradlew assemble lint ); done
for dir in ./*/ ; do ( cd "$dir" && ./gradlew buildGmsDebugPreBundle ); done
build-WearOS:
runs-on: ubuntu-latest
@ -65,7 +65,7 @@ jobs:
java-version: '11'
- name: Build and check
run: cd WearOS && ./gradlew assembleDebug lintDebug
run: cd WearOS && ./gradlew build
build-Snippets:
runs-on: ubuntu-latest
@ -110,7 +110,7 @@ jobs:
cd "$dir"
for tutorial in ./*/
do
cd "$tutorial" && ./gradlew assembleDebug lintDebug
cd "$tutorial" && ./gradlew buildDebug
cd ..
done
cd ..

View File

@ -0,0 +1,73 @@
// Copyright 2020 Google LLC
//
// 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.example.mapdemo;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.libraries.maps.GoogleMap;
import com.google.android.libraries.maps.OnMapReadyCallback;
import com.google.android.libraries.maps.SupportMapFragment;
/**
* This shows how to use Cloud-based Map Styling in a simple Activity. For more information on how
* to style a map using this method, see:
* https://developers.google.com/maps/documentation/android-sdk/cloud-based-map-styling
**/
public class CloudBasedMapStylingDemoActivity extends AppCompatActivity implements
OnMapReadyCallback {
private static final String MAP_TYPE_KEY = "map_type";
private GoogleMap map;
private int currentMapType = GoogleMap.MAP_TYPE_NORMAL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
currentMapType = savedInstanceState.getInt(MAP_TYPE_KEY);
}
// The underlying style the map will use has been set in the layout
// `cloud_styling_basic_demo` under the SupportMapFragment's `map:mapId` attribute.
setContentView(R.layout.cloud_styling_basic_demo);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
setUpButtonListeners();
}
@Override
public void onMapReady(GoogleMap map) {
this.map = map;
map.setMapType(currentMapType);
}
private void setUpButtonListeners() {
findViewById(R.id.styling_normal_mode).setOnClickListener(
v -> setMapType(GoogleMap.MAP_TYPE_NORMAL));
findViewById(R.id.styling_satellite_mode).setOnClickListener(
v -> setMapType(GoogleMap.MAP_TYPE_SATELLITE));
findViewById(R.id.styling_hybrid_mode).setOnClickListener(
v -> setMapType(GoogleMap.MAP_TYPE_HYBRID));
findViewById(R.id.styling_terrain_mode).setOnClickListener(
v -> setMapType(GoogleMap.MAP_TYPE_TERRAIN));
}
private void setMapType(int mapType) {
currentMapType = mapType;
map.setMapType(mapType);
}
}

View File

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2020 Google LLC
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.
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
class="com.google.android.libraries.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTargetLat="47.6089945"
map:cameraTargetLng="-122.3410462"
map:cameraZoom="14"
map:mapId="@string/cloud_styling_basic_map_id" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#D000"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:id="@+id/cloud_styling_basic_demo_mode_buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/styling_normal_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lite_styling_normal_mode" />
<Button
android:id="@+id/styling_satellite_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lite_styling_satellite_mode" />
<Button
android:id="@+id/styling_hybrid_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lite_styling_hybrid_mode" />
<Button
android:id="@+id/styling_terrain_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lite_styling_terrain_mode" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

View File

@ -28,9 +28,7 @@ import com.google.android.gms.maps.model.MarkerOptions
/**
* This shows how to create a simple activity with a map and a marker on the map.
*/
class BasicMapDemoActivity :
AppCompatActivity(),
OnMapReadyCallback {
class BasicMapDemoActivity : AppCompatActivity(), OnMapReadyCallback {
val SYDNEY = LatLng(-33.862, 151.21)
val ZOOM_LEVEL = 13f
@ -47,8 +45,7 @@ class BasicMapDemoActivity :
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just move the camera to Sydney and add a marker in Sydney.
*/
override fun onMapReady(googleMap: GoogleMap?) {
googleMap ?: return
override fun onMapReady(googleMap: GoogleMap) {
with(googleMap) {
moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, ZOOM_LEVEL))
addMarker(MarkerOptions().position(SYDNEY))

View File

@ -103,10 +103,8 @@ class CameraDemoActivity :
}
// [END_EXCLUDE]
override fun onMapReady(googleMap: GoogleMap?) {
override fun onMapReady(googleMap: GoogleMap) {
// return early if the map was not initialised properly
map = googleMap ?: return
with(googleMap) {
setOnCameraIdleListener(this@CameraDemoActivity)
setOnCameraMoveStartedListener(this@CameraDemoActivity)
@ -352,7 +350,7 @@ class CameraDemoActivity :
// When the camera stops moving, add its target to the current path, and draw it on the map.
checkPolylineThen {
addCameraTargetToPath()
map.addPolyline(currPolylineOptions)
map.addPolyline(currPolylineOptions!!)
}
isCanceled = true // Set to clear the map when dragging starts again.
@ -365,7 +363,7 @@ class CameraDemoActivity :
// [START_EXCLUDE silent]
checkPolylineThen {
addCameraTargetToPath()
map.addPolyline(currPolylineOptions)
map.addPolyline(currPolylineOptions!!)
}
currPolylineOptions = null

View File

@ -101,12 +101,12 @@ class CircleDemoActivity :
* This class contains information about a circle, including its markers
*/
private inner class DraggableCircle(center: LatLng, private var radiusMeters: Double) {
private val centerMarker: Marker = map.addMarker(MarkerOptions().apply {
private val centerMarker: Marker? = map.addMarker(MarkerOptions().apply {
position(center)
draggable(true)
})
private val radiusMarker: Marker = map.addMarker(
private val radiusMarker: Marker? = map.addMarker(
MarkerOptions().apply {
position(center.getPointAtDistance(radiusMeters))
icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
@ -128,10 +128,10 @@ class CircleDemoActivity :
when (marker) {
centerMarker -> {
circle.center = marker.position
radiusMarker.position = marker.position.getPointAtDistance(radiusMeters)
radiusMarker?.position = marker.position.getPointAtDistance(radiusMeters)
}
radiusMarker -> {
radiusMeters = centerMarker.position.distanceFrom(radiusMarker.position)
radiusMeters = centerMarker?.position?.distanceFrom(radiusMarker.position)!!
circle.radius = radiusMeters
}
else -> return false
@ -205,8 +205,8 @@ class CircleDemoActivity :
* When the map is ready, move the camera to put the Circle in the middle of the screen,
* create a circle in Sydney, and set the listeners for the map, circles, and SeekBars.
*/
override fun onMapReady(googleMap: GoogleMap?) {
map = googleMap ?: return
override fun onMapReady(googleMap: GoogleMap) {
map = googleMap
// we need to initialise map before creating a circle
with(map) {
moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 4.0f))

View File

@ -41,9 +41,9 @@ class EventsDemoActivity : AppCompatActivity(), OnMapClickListener,
mapFragment?.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap?) {
override fun onMapReady(googleMap: GoogleMap) {
// return early if the map was not initialised properly
map = googleMap ?: return
map = googleMap
map.setOnMapClickListener(this)
map.setOnMapLongClickListener(this)
map.setOnCameraIdleListener(this)

View File

@ -50,8 +50,8 @@ class GroundOverlayDemoActivity : AppCompatActivity(), OnSeekBarChangeListener,
mapFragment?.getMapAsync(this)
}
override fun onMapReady(map: GoogleMap?) {
map ?: return
override fun onMapReady(map: GoogleMap) {
map
// Register a listener to respond to clicks on GroundOverlays.
map.setOnGroundOverlayClickListener(this)

View File

@ -39,8 +39,8 @@ class IndoorDemoActivity : AppCompatActivity(), OnMapReadyCallback {
mapFragment?.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap?) {
map = googleMap ?: return
override fun onMapReady(googleMap: GoogleMap) {
map = googleMap
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(37.614631, -122.385153), 18f))
}

View File

@ -104,10 +104,8 @@ class LayersDemoActivity :
}
@SuppressLint("MissingPermission")
override fun onMapReady(googleMap: GoogleMap?) {
// exit early if the map was not initialised properly
map = googleMap ?: return
override fun onMapReady(googleMap: GoogleMap) {
map = googleMap
updateMapType()

View File

@ -148,10 +148,10 @@ class LiteListDemoActivity : AppCompatActivity() {
}
}
override fun onMapReady(googleMap: GoogleMap?) {
override fun onMapReady(googleMap: GoogleMap) {
MapsInitializer.initialize(applicationContext)
// If map is not initialised properly
map = googleMap ?: return
map = googleMap
setMapLocation()
}

View File

@ -14,6 +14,7 @@
package com.example.kotlindemos
import android.Manifest.permission
import android.annotation.SuppressLint
import android.content.pm.PackageManager
import android.location.Location
import android.os.Bundle
@ -50,6 +51,7 @@ class LocationSourceDemoActivity : AppCompatActivity() {
lifecycle.addObserver(locationSource)
}
@SuppressLint("MissingPermission")
private fun init(map: GoogleMap) {
map.setLocationSource(locationSource)
map.setOnMapLongClickListener(locationSource)

View File

@ -98,10 +98,10 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener, OnMap
}
}
override fun onMapsSdkInitialized(renderer: MapsInitializer.Renderer) {
override fun onMapsSdkInitialized(renderer: Renderer) {
Toast.makeText(
this,
"All demo activities will use the ${renderer.toString()} renderer.",
"All demo activities will use the $renderer renderer.",
Toast.LENGTH_LONG
).show()
}

View File

@ -45,7 +45,7 @@ class MarkerCloseInfoWindowOnRetapDemoActivity :
* the showing info window will be closed. Otherwise will show a different window.
*/
private val markerClickListener = object : GoogleMap.OnMarkerClickListener {
override fun onMarkerClick(marker: Marker?): Boolean {
override fun onMarkerClick(marker: Marker): Boolean {
if (marker == selectedMarker) {
selectedMarker = null
// Return true to indicate we have consumed the event and that we do not

View File

@ -131,7 +131,7 @@ class MarkerDemoActivity :
}
private fun render(marker: Marker, view: View) {
val badge = when (marker.title) {
val badge = when (marker.title!!) {
"Brisbane" -> R.drawable.badge_qld
"Adelaide" -> R.drawable.badge_sa
"Sydney" -> R.drawable.badge_nsw
@ -343,14 +343,14 @@ class MarkerDemoActivity :
(0 until numMarkersInRainbow).mapTo(markerRainbow) {
map.addMarker(MarkerOptions().apply{
position(LatLng(
-30 + 10 * Math.sin(it * Math.PI / (numMarkersInRainbow - 1)),
135 - 10 * Math.cos(it * Math.PI / (numMarkersInRainbow - 1))))
-30 + 10 * Math.sin(it * Math.PI / (numMarkersInRainbow - 1)),
135 - 10 * Math.cos(it * Math.PI / (numMarkersInRainbow - 1))))
title("Marker $it")
icon(BitmapDescriptorFactory.defaultMarker((it * 360 / numMarkersInRainbow)
.toFloat()))
.toFloat()))
flat(flatBox.isChecked)
rotation(rotationBar.progress.toFloat())
})
})!!
}
}

View File

@ -52,8 +52,8 @@ class MyLocationDemoActivity : AppCompatActivity(), OnMyLocationButtonClickListe
mapFragment?.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap?) {
map = googleMap ?: return
override fun onMapReady(googleMap: GoogleMap) {
map = googleMap
googleMap.setOnMyLocationButtonClickListener(this)
googleMap.setOnMyLocationClickListener(this)
enableMyLocation()

View File

@ -65,7 +65,7 @@ class OnMapAndViewReadyListener(
mapFragment.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap?) {
override fun onMapReady(googleMap: GoogleMap) {
// NOTE: The GoogleMap API specifies the listener is removed just prior to invocation.
map = googleMap ?: return
isMapReady = true

View File

@ -133,7 +133,7 @@ class PolygonDemoActivity :
return resourceIds.map { getString(it) }
}
override fun onMapReady(googleMap: GoogleMap?) {
override fun onMapReady(googleMap: GoogleMap) {
// return early if the map was not initialised properly
googleMap ?: return

View File

@ -149,7 +149,7 @@ class TagsDemoActivity : AppCompatActivity(),
image(BitmapDescriptorFactory.fromResource(R.drawable.harbour_bridge))
position(places.getValue("SYDNEY"), 700000f)
clickable(true)
}).run {
})?.run {
// add a tag to the overlay to count clicks
tag = CustomTag("Sydney ground overlay")
}
@ -157,7 +157,7 @@ class TagsDemoActivity : AppCompatActivity(),
// A marker at Hobart.
addMarker(MarkerOptions().apply {
position(places.getValue("HOBART"))
}).run {
})?.run {
// add a tag to the marker to count clicks
tag = CustomTag("Hobart marker")
}

View File

@ -67,7 +67,7 @@ class TileOverlayDemoActivity : AppCompatActivity(), OnSeekBarChangeListener, On
return url
}
}
mMoonTiles = map.addTileOverlay(TileOverlayOptions().tileProvider(tileProvider))
mMoonTiles = map.addTileOverlay(TileOverlayOptions().tileProvider(tileProvider))!!
mTransparencyBar.setOnSeekBarChangeListener(this)
}

View File

@ -44,7 +44,7 @@ class UiSettingsDemoActivity :
mapFragment.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap?) {
override fun onMapReady(googleMap: GoogleMap) {
// Return early if map is not initialised properly
map = googleMap ?: return
enableMyLocation()

View File

@ -161,10 +161,8 @@ class PolylineDemoActivity :
return resourceIds.map { getString(it) }
}
override fun onMapReady(googleMap: GoogleMap?) {
// exit early if the map was not initialised properly
googleMap ?: return
override fun onMapReady(googleMap: GoogleMap) {
googleMap
with(googleMap) {
// Override the default content description on the view, for accessibility mode.

View File

@ -29,8 +29,8 @@ internal class MapRendererOptInApplication : Application(), OnMapsSdkInitialized
override fun onMapsSdkInitialized(renderer: MapsInitializer.Renderer) {
when (renderer) {
LATEST -> Log.d("MapsDemo", "The latest version of the renderer is used.")
LEGACY -> Log.d("MapsDemo", "The legacy version of the renderer is used.")
Renderer.LATEST -> Log.d("MapsDemo", "The latest version of the renderer is used.")
Renderer.LEGACY -> Log.d("MapsDemo", "The legacy version of the renderer is used.")
}
}
}

View File

@ -144,9 +144,9 @@ class MarkerDemoActivity : AppCompatActivity(),
private val SYDNEY = LatLng(-33.87365, 151.20689)
private val BRISBANE = LatLng(-27.47093, 153.0235)
private lateinit var markerPerth: Marker
private lateinit var markerSydney: Marker
private lateinit var markerBrisbane: Marker
private var markerPerth: Marker? = null
private var markerSydney: Marker? = null
private var markerBrisbane: Marker? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -164,19 +164,19 @@ class MarkerDemoActivity : AppCompatActivity(),
.position(PERTH)
.title("Perth")
)
markerPerth.tag = 0
markerPerth?.tag = 0
markerSydney = map.addMarker(
MarkerOptions()
.position(SYDNEY)
.title("Sydney")
)
markerSydney.tag = 0
markerSydney?.tag = 0
markerBrisbane = map.addMarker(
MarkerOptions()
.position(BRISBANE)
.title("Brisbane")
)
markerBrisbane.tag = 0
markerBrisbane?.tag = 0
// Set a listener for marker click.
map.setOnMarkerClickListener(this)

View File

@ -84,14 +84,13 @@ internal class TileOverlays : OnMapReadyCallback {
}
// [END maps_android_tile_overlays_transparency]
// [END maps_android_tile_overlays_transparency]
private fun removeAndClearCache() {
// [START maps_android_tile_overlays_remove]
tileOverlay.remove()
tileOverlay?.remove()
// [END maps_android_tile_overlays_remove]
// [START maps_android_tile_overlays_clear_tile_cache]
tileOverlay.clearTileCache()
tileOverlay?.clearTileCache()
// [END maps_android_tile_overlays_clear_tile_cache]
}
}

View File

@ -97,17 +97,17 @@ internal class Heatmaps {
// [START maps_android_utils_heatmap_customize_opacity]
provider.setOpacity(0.7)
tileOverlay.clearTileCache()
tileOverlay?.clearTileCache()
// [END maps_android_utils_heatmap_customize_opacity]
// [START maps_android_utils_heatmap_customize_dataset]
val data: List<WeightedLatLng> = ArrayList()
provider.setWeightedData(data)
tileOverlay.clearTileCache()
tileOverlay?.clearTileCache()
// [END maps_android_utils_heatmap_customize_dataset]
// [START maps_android_utils_heatmap_remove]
tileOverlay.remove()
tileOverlay?.remove()
// [END maps_android_utils_heatmap_remove]
}
}