2

I am using Google map in my project, map is opening but not loading. I am new on android studio,I do not know how set map key in android studio,
Please help me.

manifest file is

     <?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="com.truiton.supportmapfragment.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!--
 The following two permissions are not required to use
 Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >


    <activity
        android:name=".activities.SplashScreen"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".activities.MainActivity" />
    <activity
        android:name=".activities.ForCustomer"
        android:label="@string/title_activity_for_customer"
        android:theme="@style/AppTheme.NoActionBar" >
    </activity>
    <activity
        android:name=".activities.CustomerSignIn"
        android:label="@string/title_activity_customer_sign_in"
        android:theme="@style/AppTheme.NoActionBar" >
    </activity>
    <activity
        android:name=".activities.Home"
        android:label="@string/title_activity_home"
        android:theme="@style/AppTheme.NoActionBar" >
    </activity>
    <activity
        android:name=".activities.RegisterCustomer"
        android:label="@string/title_activity_register_customer"
        android:theme="@style/AppTheme.NoActionBar" >
    </activity>
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="I_REMOVED"/>
</application>

xml file using like this

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="#1B8AB7">

        <ImageView
            android:id="@+id/menu"
            android:layout_width="40dp"
            android:layout_height="25dp"
            android:src="@drawable/menu"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"/>

        <RelativeLayout

            android:id="@+id/realtivelayout_search_bar"

            android:layout_width="250dp"
       android:layout_centerHorizontal="true"
            android:layout_height="30dp"
            android:layout_centerVertical="true"

            android:background="@drawable/saerchbarbackground"

            >

            <ImageView
                android:id="@+id/btn_searchbar"
                android:layout_width="35dp"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:src="@drawable/search_btn"
                android:layout_marginRight="5dp"/>
            <EditText
                android:layout_marginLeft="5dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:id="@+id/edittext_searchbar"
                android:layout_toLeftOf="@+id/btn_searchbar"
                android:hint=" search for location"
                android:singleLine="true"
                android:textColor="#000000"
                android:background="#00000000"
                />

        </RelativeLayout>

    </RelativeLayout>

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/header"

        />

</RelativeLayout>

I Used map api like you said, but map not loading yet.

Dalvinder Singh
  • 1,073
  • 1
  • 12
  • 19

4 Answers4

7

You need to set your map key in your AndroidManifest.xml file. Between <application> </application> tag.

      <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="Your key" />

Refer Google map Integration in Android for better understanding. Take care to add all required permissions for map integration in manifest file.

To create google api key, you will require sha1 key. If you have not yet created it. That you can follow SHA1 key in Android studio

  <!-- map -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

Add this before your <application> tag in manifest.

Community
  • 1
  • 1
Beena
  • 2,334
  • 24
  • 50
5

Try this

<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/google_maps_api_key"/>

build.gradle

android {

buildTypes {
    debug {
        resValue "string", "google_maps_api_key", "[YOUR DEV KEY]"
    }
    release {
        resValue "string", "google_maps_api_key", "[YOUR PROD KEY]"
    }
}}
Quang Doan
  • 632
  • 4
  • 12
2

Here are the Get a Google Maps API key and Signup and API Keys documentations that you can follow in case you are lost in adding API_KEY for your app.

Here's a summary of what's on the links:

Get a Google Maps API key

  1. Copy the link provided in the google_maps_api.xml file and paste it into your browser. The link takes you to the Google Developers Console and supplies information via URL parameters, thus reducing the manual input required from you.

  2. Follow the instructions to create a new project on the console or select an existing project.

  3. Create an Android API key for your console project.

  4. Copy the resulting API key, go back to Android Studio, and paste the API key into the <string> element in the google_maps_api.xml file.

Create an API project in the Google Developers Console

  1. Go to the Google Developers Console.
  2. Select a project, or create a new one.
  3. Open the API Library in the Google Developers Console. If prompted, select a project or create a new one. Select the Enabled APIs link in the API section to see a list of all your enabled APIs. Make sure that the API is on the list of enabled APIs. If you have not enabled it, select the API from the list of APIs, then select the Enable API button for the API. The only API you need is the Google Maps Android API, although you can choose to enable other APIs for the same project too.

Get an Android API key

  1. Go to the Google Developers Console.
  2. In the sidebar on the left, select Credentials.
  3. If your project doesn't already have an Android API key, create one now by selecting Add credentials > API key > Android key.
  4. In the resulting dialog, enter your app's SHA-1 fingerprint and package name. For example:

BB:0D:AC:74:D3:21:E1:43:67:71:9B:62:91:AF:A1:66:6E:44:5D:75 com.example.android.mapexample

  1. Your new Android API key appears in the list of API keys for your project. An API key is a string of characters, something like this:

AIzaSyBdVl-cTICSwYKrZ95SuvNw7dbMuDt1KG0

Add the API key to your application

  1. In AndroidManifest.xml, add the following element as a child of the <application> element, by inserting it just before the closing </application> tag:

    <meta-data android:name="com.google.android.geo.API_KEY" android:value="YOUR_API_KEY"/>

Substitute your API key for YOUR_API_KEY in the value attribute. This element sets the key com.google.android.geo.API_KEY to the value of your API key.

  1. Save AndroidManifest.xml and re-build your application.
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
gerardnimo
  • 1,444
  • 8
  • 10
0

check following code. add map fragment in xml file.It is working code.

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

In activity

  public class MapView extends AppCompatActivity implements OnMapReadyCallback{
  Googlemap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map_view);//your layout view
    SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap mMap) {
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
Prathap Badavath
  • 1,621
  • 2
  • 20
  • 24