-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Closed
Closed
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
demo/src/main/java/com/google/maps/android/utils/demo/IsochroneMapActivity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); } | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
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. | ||
--> | ||
|
||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:id="@+id/rootLayout" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<com.google.android.gms.maps.MapView | ||
android:id="@+id/mapView" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" /> | ||
|
||
<ProgressBar | ||
android:id="@+id/progressBar" | ||
style="?android:attr/progressBarStyleLarge" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center" | ||
android:visibility="gone" /> | ||
|
||
<LinearLayout | ||
android:id="@+id/buttonLayout" | ||
android:orientation="horizontal" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="top|end" | ||
android:layout_margin="16dp"> | ||
|
||
<Button | ||
android:id="@+id/btnBicycling" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="BICYCLING" | ||
|
||
android:textAllCaps="false" | ||
android:alpha="1" /> | ||
|
||
<Button | ||
|
||
android:id="@+id/btnDriving" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="DRIVING" | ||
|
||
android:textAllCaps="false" | ||
android:layout_marginStart="8dp" | ||
android:alpha="0.5" /> | ||
|
||
<Button | ||
|
||
android:id="@+id/btnWalking" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="WALKING" | ||
|
||
android:textAllCaps="false" | ||
android:layout_marginStart="8dp" | ||
android:alpha="0.5" /> | ||
|
||
</LinearLayout> | ||
|
||
</FrameLayout> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.