I want to create an app that displays the route from my current location to a destination using an in-app map, rather than opening an external maps application automatically.
Using NavigationTemplate
Using MapWithContentTemplate
I’m facing an issue where the Google Map is blank when using Android Auto.
Here’s what I’ve done so far:
The Problem:
What I Need Help With:
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=";>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="androidx.car.app.NAVIGATION_TEMPLATES"/>
<uses-permission android:name="androidx.car.app.ACCESS_SURFACE" />
<uses-feature android:name="android.hardware.sensorpass"
android:required="true" />
<uses-feature
android:name="android.hardware.type.automotive"
android:required="true" />
<application
android:allowBackup="true"
android:appCategory="audio"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Testing_navigation">
<!-- Important to show in Android Auto App-->
<meta-data
android:name="com.google.android.gms.car.application"
android:resource="@xml/automotive_app_desc" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="Google_Map_Key" />
<meta-data
android:name="androidx.car.app.minCarApiLevel"
android:value="7"/>
<service android:name=".MyNavigationService"
android:exported="true">
<intent-filter>
<action android:name="androidx.car.app.CarAppService"/>
<category android:name="androidx.car.app.category.NAVIGATION"/>
</intent-filter>
</service>
</application>
</manifest>
Service:
class MyNavigationService : CarAppService() {
override fun createHostValidator(): HostValidator {
return if ((applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
HostValidator.ALLOW_ALL_HOSTS_VALIDATOR
} else {
HostValidator.Builder(baseContext)
.addAllowedHosts(R.array.hosts_allowlist_sample).build()
}
}
override fun onCreateSession(): Session {
return NavigationSession()
}
}
class NavigationSession : Session() {
override fun onCreateScreen(intent: Intent): Screen {
// Navigation Screen
return NavigationScreen(carContext)
// Hello World Screen
// return HelloWorldScreen(carContext)
}
}
Screen Template:
val row = Row.Builder().setTitle("Hello world!").build()
val paneBuilder = Pane.Builder().addRow(row)
val mapController = MapController.Builder()
.build()
// Map Test Code 1
val template = NavigationTemplate.Builder()
.setActionStrip(actionStrip)
.setDestinationTravelEstimate(travelEstimate)
.build()
// Map Test Code 2
val template = MapWithContentTemplate.Builder()
.setLoading(false)
.setContentTemplate(
PaneTemplate.Builder(paneBuilder.build())
.setTitle("Hello")
.build()
)
.setActionStrip(actionStrip)
.setMapController(mapController)
.build()
return template
I want to create an app that displays the route from my current location to a destination using an in-app map, rather than opening an external maps application automatically.
Using NavigationTemplate
Using MapWithContentTemplate
I’m facing an issue where the Google Map is blank when using Android Auto.
Here’s what I’ve done so far:
The Problem:
What I Need Help With:
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="androidx.car.app.NAVIGATION_TEMPLATES"/>
<uses-permission android:name="androidx.car.app.ACCESS_SURFACE" />
<uses-feature android:name="android.hardware.sensor.compass"
android:required="true" />
<uses-feature
android:name="android.hardware.type.automotive"
android:required="true" />
<application
android:allowBackup="true"
android:appCategory="audio"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Testing_navigation">
<!-- Important to show in Android Auto App-->
<meta-data
android:name="com.google.android.gms.car.application"
android:resource="@xml/automotive_app_desc" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="Google_Map_Key" />
<meta-data
android:name="androidx.car.app.minCarApiLevel"
android:value="7"/>
<service android:name=".MyNavigationService"
android:exported="true">
<intent-filter>
<action android:name="androidx.car.app.CarAppService"/>
<category android:name="androidx.car.app.category.NAVIGATION"/>
</intent-filter>
</service>
</application>
</manifest>
Service:
class MyNavigationService : CarAppService() {
override fun createHostValidator(): HostValidator {
return if ((applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
HostValidator.ALLOW_ALL_HOSTS_VALIDATOR
} else {
HostValidator.Builder(baseContext)
.addAllowedHosts(R.array.hosts_allowlist_sample).build()
}
}
override fun onCreateSession(): Session {
return NavigationSession()
}
}
class NavigationSession : Session() {
override fun onCreateScreen(intent: Intent): Screen {
// Navigation Screen
return NavigationScreen(carContext)
// Hello World Screen
// return HelloWorldScreen(carContext)
}
}
Screen Template:
val row = Row.Builder().setTitle("Hello world!").build()
val paneBuilder = Pane.Builder().addRow(row)
val mapController = MapController.Builder()
.build()
// Map Test Code 1
val template = NavigationTemplate.Builder()
.setActionStrip(actionStrip)
.setDestinationTravelEstimate(travelEstimate)
.build()
// Map Test Code 2
val template = MapWithContentTemplate.Builder()
.setLoading(false)
.setContentTemplate(
PaneTemplate.Builder(paneBuilder.build())
.setTitle("Hello")
.build()
)
.setActionStrip(actionStrip)
.setMapController(mapController)
.build()
return template
When using the NavigationTemplate
, MapWithContentTemplate
, (or the other deprecated navigation templates), you are responsible for drawing the map yourself. Only the PlaceListMapTemplate
(which can only be used by POI apps) handles drawing the map for you.
Since you're using Google Maps as your mapping provider and your app is a navigation app, you might be interested in using the Google Maps Android Auto SDK, which helps you draw the map yourself.