Home > Net >  Making use of Google Map in an Android
Making use of Google Map in an Android

Time:07-13

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

CodePudding user response:

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

CodePudding user response:

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" />
  • Related