1

I am trying to implement a simple map and I have already added all the user permissions in my manifest file and enabled Google API V2.Still it is showing a blank map only.

list_doctor.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >



    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

list_doctor.java

public class listDoctor extends Activity implements OnMapReadyCallback{
    private MapFragment fragment;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listdoctor);
        try {
            // Loading map
            initilizeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * function to load map. If map is not created it will create it for you
     */
    private void initilizeMap() {
        if (fragment == null) {
            fragment = ((MapFragment) getFragmentManager().findFragmentById(R.id.map));
            fragment.getMapAsync(this);
            // check if map is created successfully or not
            if (fragment == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }

    @Override
       public void onMapReady(GoogleMap map) {
        LatLng sydney = new LatLng(-33.867, 151.206);

        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

        map.addMarker(new MarkerOptions()
                .title("Sydney")
                .snippet("The most populous city in Australia.")
                .position(sydney));
    }

}

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.winthrop"
    android:versionCode="1"
    android:versionName="1.0" >
    <permission
        android:name="com.winthrop.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.winthrop.permission.MAPS_RECEIVE" />

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:name="com.winthrop.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppBaseTheme">
        </activity>

              <!-- Goolge API Key -->
        <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="692634518367-dgd8tgncr13tcvhpj5uuj9bmm66d52pq" />

    </application>

</manifest>

logcat Error

03-23 11:06:31.054: E/Google Maps Android API(15456): Authorization failure.  Please see https://developers.google.com/maps/documentation/android/start for how to correctly set up the map.
Bugs Happen
  • 2,169
  • 4
  • 33
  • 59
Nidhi
  • 777
  • 7
  • 17
  • for generating API key, use the package name mentioned in your manifest. – Bugs Happen Mar 23 '15 at 06:32
  • you might also wanna check which key (debug/release) you are using. [This SO answer](http://stackoverflow.com/a/17732453/1708390) might help you in generating debug/release key. – Bugs Happen Mar 23 '15 at 06:44

2 Answers2

2

You have missed something related to API_KEY generating and using. Please re-check if you have generated API_KEY properly for your package name com.winthrop. You can refer steps at : here

Kushal
  • 8,100
  • 9
  • 63
  • 82
0

Try this

Create SHA1 & API Key

  1. Open Command prompt.
  2. C:\Program Files\Java\jdk1.6.0_37\bin> keytool -list -v -keystore "your path" \.android\debug.keystore"
  3. Get SHA1: "XXXX..."
  4. Go to https://console.developers.google.com/
  5. Create project and create Android key
  6. Paste your SHA1 key end with semi colon and paste your project package name (ex 11:23....;com.example.googlemapversion2)
  7. Get API key.

Create xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

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

</RelativeLayout>

your activity class

package com.example.googlemapversion2;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends FragmentActivity {

// Google Map
private GoogleMap googleMap;
double latitude;
double longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
        // Loading map
        initilizeMap();

        Log.i("Sucess", "Map");

    } catch (Exception e) {
        e.printStackTrace();
    }

}


private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        MarkerOptions marker=new MarkerOptions().position(new LatLng(latitude, longitude)).title("Map View");
        googleMap.addMarker(marker); 
        googleMap.setMyLocationEnabled(true);
        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

@Override
protected void onResume() {
    super.onResume();
    initilizeMap();
}

}

your manifest xml file

<permission
    android:name="com.example.googlemapversion2.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

 <uses-permission android:name="com.example.googlemapversion2.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.googlemapversion2.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="your api key paste here" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    </application>
Jai Rajesh
  • 939
  • 5
  • 18