0

I am trying to use Google Map in an Android app for the first time. For that I found a couple of tutorials and documents on the net to get started. I already have an API key that I got from Google.

Here is the kind of code I have:

package me.myapp

import ....

class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
    private lateinit var mMap: GoogleMap
    private lateinit var binding: ActivityMapsBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMapsBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        val mapFragment = supportFragmentManager
            .findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync(this)
    }


     override fun onMapReady(googleMap: GoogleMap) {
        mMap = googleMap
        // Add a marker in Sydney and move the camera
        val sydney = LatLng(-34.0, 151.0)
        mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
    }
}

I can launch the app I made, but something is not what I expected:

  • When installed and launched on my device the app appears with the name MapsActivity instead of the usual name I give (MyApp). I don't know if this is important, but I just wonder why that is.
  • After it is launched, I see a grey display with the Google mark at the bottom left, but no other contents. I expected to see some map of Sidney appear.

I hope someone reading this will have some relevant feedback to provide. Thanks in advance.

......... addition .........

Here is how the AndroidManifest.xml file looks like, in case that may be useful:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="me.myapp">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApp"
        tools:targetApi="31">
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="${MAPS_API_KEY}" />

        <activity
            android:name=".MapsActivity"
            android:exported="true"
            android:label="@string/title_activity_maps">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

The app screenshot is:

enter image description here

Michel
  • 10,303
  • 17
  • 82
  • 179
  • Did you enable the location permission from settings for your app? – Abdullah Javed Jul 13 '22 at 07:20
  • Check your log.... – Gobu CSG Jul 13 '22 at 07:23
  • @Abdullah. No I did not and I think I did not see that mentioned in what I read. Where should I do that ? – Michel Jul 13 '22 at 07:24
  • https://stackoverflow.com/questions/57098852/how-to-ask-for-location-permission-in-android-studio – Abdullah Javed Jul 13 '22 at 07:26
  • For instant check add the permissions in the manifest of your app and enable it from app settings. Mobile Settings -> apps-> your app-> permissions-> location ->enable it – Abdullah Javed Jul 13 '22 at 07:28
  • 1
    @Abdullah. Thanks but are you sure you are not making a confusion? I only need to have a map. I do not need to the know the device current location and the GPS. – Michel Jul 13 '22 at 07:31
  • @Gobu CSG. I read this in the logs: -- In the Google Developer Console (https://console.developers.google.com) Ensure that the "Google Maps Android API v2" is enabled. -- The problem is that when I go to the URL mentioned I see no "Google Maps Android API v2". – Michel Jul 13 '22 at 08:16
  • Add this to you manifest: [Solution provided in similar issue](https://stackoverflow.com/a/29960428/16732181) – azaLiza Jul 13 '22 at 09:06
  • @Michel Key and Json file. Cross check both. Check your studio log as well. – Gobu CSG Jul 13 '22 at 11:12
  • @azaLiza. I have tried your link. It does not work for me. – Michel Jul 13 '22 at 16:04

2 Answers2

2

Please Provide your Google Map Api Key

  1. Create google api key enter link description here
  2. Enter the Key you got in the value field
<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="pasteApiKey" />
Praveen Kumar
  • 329
  • 2
  • 14
  • My API is already set up from start. – Michel Jul 13 '22 at 16:06
  • Yes. I needed to go to https://console.cloud.google.com and then enable "Maps SDK for Android" in the library section. Thanks a lot for this help! I found this detail looking at your vidéo. – Michel Jul 13 '22 at 16:33
0

To change the name of your application you need to open the Manifest file and change the string label inside the application and activity tag :

<application
   android:label="MyApp"
   <activity
      android:label="MyApp"
   </activity>
</application>

To access location you need to add these permissions in the Manifest file as well:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

And make sure to request the use of location when launching your app This decomuntation tells how to do it : Request location in android apps

azaLiza
  • 37
  • 3
  • In the the Manifest file, when I look the name is already "MyApp" so there is nothing I can change. For the rest I do not need to access location, I only want a map. – Michel Jul 13 '22 at 08:24
  • Have you checked inside of the activity tag if the string of label is MyApp as well ? Can you post a screenshot of the result when you launch your mapp please ? – azaLiza Jul 13 '22 at 08:27
  • I have updated my post to provide more information. Please take a look. – Michel Jul 13 '22 at 08:46
  • 1
    for the name change this line (android:label="@string/title_activity_maps") into (android:label="@string/app_name") – azaLiza Jul 13 '22 at 08:49
  • Yes for the name that worked. But am I supposed to do that? Beside I added a screenshot to the post. – Michel Jul 13 '22 at 09:05