1

I am new at Android. I have been trying from many days to make very basic google map application but unable to complete it yet... There are no errors in code, emulator running fine from terminal, Map key also fine but still I am unable to see the map. When I run my app only grid appears and map is not displayed. Here is the code, can any body please help me. i am using eclipse juno 4.2.1 api level 17 and i want to show map using google map api v2 My code is here

  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.googlemap"
  android:versionCode="1"
  android:versionName="2.0" >
  <uses-feature
  android:glEsVersion="0x00020000"
 android:required="true"/> 


  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
  <permission
      android:name="com.example.googlemap.permission.MAPS_RECEIVE"
      android:protectionLevel="signature"/>
  <uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE"/>

 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
  <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/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
   <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="[API_KEY]"/>

       <!-- <activity
        android:name="com.example.googlemap.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> -->
     <!--<uses-library android:name="com.google.android.maps" />-->
     </application>

    </manifest>

main.xml

         <?xml version="1.0" encoding="utf-8"?>
  <fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.MapFragment"/>                

MainActivity.java

   package com.example.googlemap;

  import android.app.Activity;
  import android.os.Bundle;

  public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
protected boolean isRouteDisplayed() {

         return false;

        }

  }
Gorcyn
  • 2,807
  • 1
  • 20
  • 22
Zainya Masood
  • 21
  • 1
  • 4
  • Your maps key is most probably wrong. With what certificate/signature are you generating the Key, the Development or Deployment one? Are you pressing play from inside eclipse or exporting an APK? – blindstuff Jan 23 '13 at 14:11
  • i am using ShA-1 for generating key... – Zainya Masood Jan 23 '13 at 14:35
  • @Zainya Masood....(1) In your "main.xml", API_KEY is not required because your API_KEY already exists in your manifest file. From Google Maps API v2, your API_KEY is defined as the in your manifest file. – BBonDoo Jan 23 '13 at 15:02
  • @Zainya Masood...(2) your "main.xml" is the case of the old version called as Google Maps API version 1. Please change it into the new one using "" – BBonDoo Jan 23 '13 at 15:11
  • @Zainya Masood...(3)In your MainActivity.java, please change into "public class MainActivity extends android.support.v4.app.FragmentActivity { " because you set the " – BBonDoo Jan 23 '13 at 15:22
  • @BBonDoo Thanx for your responce ...plz kindly recheck my code i make some changes in it n do tell me if there is any isse – Zainya Masood Jan 24 '13 at 09:15
  • still my app is not showing map bt just grid :( – Zainya Masood Jan 24 '13 at 09:17
  • Not yet??..so sorry...OK, I will let you feel be successful in a moment...Wait for me.. – BBonDoo Jan 24 '13 at 09:27
  • Now I saw your code and file again. With that files, you can not see Google Maps..I will write a separate text, not this comment. And try to modify your code... – BBonDoo Jan 24 '13 at 09:31

5 Answers5

1

Your Manifest file is using Maps V2 and layout and Activity are using Maps V1. You are generating Maps API key using Maps V2 api key generation procedure and setting the same to Maps V1 MapView. So It will not display as it treat a wrong API Key.

Either you generate API key in V1 Key Generation Procedure or else use google-play-services_lib project and change the MapView and other Classes.

TNR
  • 5,839
  • 3
  • 33
  • 62
  • please do tell me how to make layout and activity as using Maps V2 – Zainya Masood Jan 23 '13 at 14:33
  • and i am getting errors in logcat like "could not get connection factory client " and second one is 'error opening trace file no such file found' @TNR – Zainya Masood Jan 23 '13 at 14:54
  • @ZainyaMasood They are just warnings and you no need to worry about them – TNR Jan 23 '13 at 15:07
  • @ZainyaMasood There are no too many tutorials teaching Maps V2 as the V2 version release just only few days back. You have to wait for some more time to get more elaborated tutorials or else go through this link which can help you http://stackoverflow.com/questions/13796822/google-map-android-api-v2-application-crashes – TNR Jan 23 '13 at 15:09
  • Thanx for your responce ...plz kindly recheck my code i make some changes in it n do tell if there is any isse – Zainya Masood Jan 24 '13 at 09:15
  • @ZainyaMasood I see your minSdk is 8, so you need to use SupportMapFragment rather MapFragment. and you have to extend your Activity with FragmentActivity, not with Activity. other than this issue everything is ok in you code – TNR Jan 24 '13 at 09:30
0

First, in your manifest file, modify this part into this below.

<activity
        android:name="com.example.googlemap.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>
<uses-library android:name="com.google.android.maps" />

Why did you block this part? Please make this part activate right now.

@@@

Second,please modify your main.xml into this below; You MUST specify if the layout file name is "main.xml" or "activity_main.xml".

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.SupportMapFragment"/>

You must use the above code. Because you set [android:minSdkVersion="8"] in your manifest file. Your main.xml is only useful for testing your settings in an application targeting Android API 12 or later .

@@@

Third, please modify your MainActivity,java into this below;

public class MianActivity extends android.support.v4.app.FragmentActivity {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUpMapIfNeeded();

    }

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

    private void setUpMapIfNeeded() {
        if (mMap == null) {
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }
}

And in Eclipse, press Ctrl + Shift + O (alphabet) on your Keyboard and perform "organize imports" and then several classes will be imported automatically.

This code from #1 to #3 above is the standard basic code format to be able to see Google Maps on your real handset using new Google Maps API version 2. This code will show you a map and a marker on the map.

@@@

Four. The most important thing is to add two libraries to your project : one is Google Play service libs, the other is Android-support-v4.

HOW to add this two libraries to your project, see this document : Click here.

If you can not set two libraries, you can not see the map on your real handset.

@@@

Five, Try to run your project just on your real device(handset), not on the emulator in Eclipse. Because we can not see the map using the new Google Maps API v2 supported with Google Play service on the emulator, which is Google's official document :click here.

@@@

I hope you will see Google map on your real handset.

BBonDoo
  • 766
  • 5
  • 14
0

I also had a similar problem. This worked for me!

  1. Go to Google API Console, where the API key was generated.
  2. Click on services (appears on options in left).
  3. Hover and search for Google Maps Android API v2.
  4. Enable it (initially it was off hence only grey tiles appeared).
senshin
  • 10,022
  • 7
  • 46
  • 59
-1

There are no errors in code, emulator running fine from terminal

Great ! But Window > Show View > Other > Android > Logcat !

The application may not crash but the Map API is telling you why the map can not be displayed in the logs.

To be sure you did well:

  • To implement the new Google Map API, Google released a non-complete guide. You can find it here.
  • I say non-complete because they do not clearly explain the setup of Google Play Services, but they do here.
Gorcyn
  • 2,807
  • 1
  • 20
  • 22
-1

Make sure you have this setup:

map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

or whatever type you want....

jasonflaherty
  • 1,924
  • 7
  • 40
  • 82