Skip to content

feat: added isochrone maps #1535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions demo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ dependencies {
implementation(libs.kotlinx.coroutines.android)
implementation(libs.kotlinx.coroutines.core)
implementation(libs.material)
implementation(libs.play.services.location)

testImplementation(libs.junit)
testImplementation(libs.truth)
Expand Down
3 changes: 3 additions & 0 deletions demo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
<activity
android:name=".PolyDecodeDemoActivity"
android:exported="true" />
<activity
android:name=".IsochroneMapActivity"
android:exported="true" />
<activity
android:name=".PolySimplifyDemoActivity"
android:exported="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal fun keyHasValidFormat(apiKey: String): Boolean {
* @param context The context to retrieve the API key from.
* @return The API key if found, `null` otherwise.
*/
private fun getMapsApiKey(context: Context): String? {
fun getMapsApiKey(context: Context): String? {
try {
val bundle = context.packageManager
.getApplicationInfo(context.packageName, PackageManager.GET_META_DATA)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
* Copyright 2025 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.google.maps.android.utils.demo;

import static com.google.maps.android.utils.demo.ApiKeyValidatorKt.getMapsApiKey;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.maps.android.isochrone.IsochroneMapProvider;

public class IsochroneMapActivity extends AppCompatActivity implements OnMapReadyCallback {

private static final String TAG = "IsochroneMapActivity";

private FrameLayout rootLayout;
private MapView mapView;
private ProgressBar progressBar;

private GoogleMap map;
private FusedLocationProviderClient fusedLocationClient;

private ActivityResultLauncher<String> requestPermissionLauncher;

private IsochroneMapProvider isochroneMapProvider;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

rootLayout = new FrameLayout(this);
mapView = new MapView(this);

progressBar = new ProgressBar(this);
progressBar.setIndeterminate(true);
progressBar.setVisibility(View.GONE);

FrameLayout.LayoutParams mapParams = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);

FrameLayout.LayoutParams progressParams = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
Gravity.CENTER);

rootLayout.addView(mapView, mapParams);
rootLayout.addView(progressBar, progressParams);

setContentView(rootLayout);

mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);

fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

requestPermissionLauncher = registerForActivityResult(
new ActivityResultContracts.RequestPermission(),
isGranted -> {
if (isGranted) {
getLocationAndDraw();
} else {
Log.e(TAG, "Location permission denied");
}
});
}

@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
map = googleMap;
map.getUiSettings().setZoomControlsEnabled(true);
checkPermissionAndStart();
}

private void checkPermissionAndStart() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION);
} else {
getLocationAndDraw();
}
}

private void getLocationAndDraw() {
// Permission check to satisfy lint and prevent security exceptions
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.e(TAG, "Location permission not granted");
return;
}

IsochroneMapProvider.UiThreadExecutor uiThreadExecutor = this::runOnUiThread;

fusedLocationClient.getLastLocation().addOnSuccessListener(location -> {
if (location != null) {
LatLng origin = new LatLng(location.getLatitude(), location.getLongitude());
map.moveCamera(CameraUpdateFactory.newLatLngZoom(origin, 14f));

if (isochroneMapProvider == null) {
String apiKey = getMapsApiKey(this);
isochroneMapProvider = new IsochroneMapProvider(
map,
apiKey,
new IsochroneMapProvider.LoadingListener() {
@Override
public void onLoadingStarted() {
runOnUiThread(() -> progressBar.setVisibility(View.VISIBLE));
}

@Override
public void onLoadingFinished() {
runOnUiThread(() -> progressBar.setVisibility(View.GONE));
}
},
IsochroneMapProvider.TransportMode.BICYCLING,
uiThreadExecutor,
null // Use default fetcher
);
}

isochroneMapProvider.drawIsochrones(origin, new int[]{2, 4, 6, 9}, IsochroneMapProvider.ColorSchema.GREEN_RED);

} else {
Log.e(TAG, "Location is null");
}
});
}

// MapView lifecycle methods
@Override protected void onResume() { super.onResume(); mapView.onResume(); }
@Override protected void onStart() { super.onStart(); mapView.onStart(); }
@Override protected void onStop() { super.onStop(); mapView.onStop(); }
@Override protected void onPause() { mapView.onPause(); super.onPause(); }
@Override protected void onDestroy() { mapView.onDestroy(); super.onDestroy(); }
@Override public void onLowMemory() { super.onLowMemory(); mapView.onLowMemory(); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected void onCreate(Bundle savedInstanceState) {
addDemo("Multi Layer", MultiLayerDemoActivity.class);
addDemo("AnimationUtil sample", AnimationUtilDemoActivity.class);
addDemo("Street View Demo", StreetViewDemoActivity.class);
addDemo("Isochrone Map", IsochroneMapActivity.class);
}

private void addDemo(String demoName, Class<? extends Activity> activityClass) {
Expand Down
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mockk = "1.14.2"
lint = "31.10.1"
org-jacoco-core = "0.8.13"
material = "1.12.0"
play-services-location = "21.3.0"

[libraries]
appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
Expand Down Expand Up @@ -47,4 +48,5 @@ lint = { module = "com.android.tools.lint:lint", version.ref = "lint" }
lint-tests = { module = "com.android.tools.lint:lint-tests", version.ref = "lint" }
testutils = { module = "com.android.tools:testutils", version.ref = "lint" }
org-jacoco-core = { module = "org.jacoco:org.jacoco.core", version.ref = "org-jacoco-core" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
play-services-location = { group = "com.google.android.gms", name = "play-services-location", version.ref = "play-services-location" }
1 change: 1 addition & 0 deletions library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ dependencies {
testImplementation(libs.kxml2)
testImplementation(libs.mockk)
testImplementation (libs.kotlin.test)
testImplementation(libs.mockito.core)
implementation(libs.kotlin.stdlib.jdk8)
}

Expand Down
Loading