I can't get Google Maps Android API v2 to work : GoogleMap cannot be resolved to a type

10,593

Solution 1

When this error happens, it is because the imports didn't work well. Let's try this, select the Project with Right click and then select Properties. now u go Android -> Library -> add . select google-play-services_lib .

obs: u have to import the google_play_services_lib from your sdk directory to your workspace!

directory : \sdk\extras\google\google_play_services.

please tell me if it worked.

Sorry for this horrible grammar(english isn't my native language).

Solution 2

Use FragmentActivity instead of Activity.

http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html

Solution 3

replace

googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

to

mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

Solution 4

import com.google.android.gms.maps.MapFragment; import com.google.android.gms.maps.GoogleMap;

Manually import the above statement your problem will be solved.

Share:
10,593
mobilGelistirici
Author by

mobilGelistirici

if($clock >= 12 && isset($lunch)) { break; }

Updated on June 04, 2022

Comments

  • mobilGelistirici
    mobilGelistirici almost 2 years

    I -think I- have done all the steps in these two links :

    http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/ http://developer.android.com/google/play-services/setup.html

    so please don't send them as answers.

    I have downloaded the Maps API, copied it into Eclipse Workspace, marked it as library, referenced it in my project. But no success. I'm still getting the errors :

    GoogleMap cannot be resolved to a type

    MapFragment cannot be resolved to a type

    Here's my MainActivity.Java:

    package com.mapsapp.mapsappv1;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.Toast;
    
    
    public class MainActivity extends Activity {
     
        
        // Google Map
        private GoogleMap googleMap;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            
     
            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 (googleMap == null) {
                googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
     
                // 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();
        }
     
    }
    

    And here is my AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.mapsapp.mapsappv1"
        android:versionCode="1"
        android:versionName="1.0" >
    
    <permission
            android:name="com.mapsapp.mapsappv1.permission.MAPS_RECEIVE"
            android:protectionLevel="signature" />
    
    <uses-permission android:name="com.mapsapp.mapsappv1.permission.MAPS_RECEIVE" />
    
        <uses-sdk
            android:minSdkVersion="12"
            android:targetSdkVersion="18" />
    
    
        <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" >
            
            <!-- this line is part of my trials.. probably unnecessary : -->
            <activity android:name="com.google.android.gms.UnusedStub" />
            
            <activity
                android:name="com.mapsapp.mapsappv1.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>
            
            
            <!-- Google Maps API Key -->
    <meta-data
         android:name="com.google.android.maps.v2.API_KEY"
         android:value="myApiKeyGeneratedWithSHA1Key-ItShouldBeTrue" />
         
        </application>
    
    </manifest>
    

    Maybe somebody had this error and knows how to solve it ?

    and I have tried restarting eclipse, cleaning the project, creating the project from scratch (its actually v10 or something :) ) apparently I'm doing something wrong..

    Any kind of help will be appreciated. But please don't paste the two links I have mentioned at the beginning.

    Edit: Progress Report

    @regeme's comment : import com.google.android.gms.maps.GoogleMap; helped getting rid of the error : GoogleMap cannot be resolved to a type

    However, MapFragment error remains.

    I have changed the code according @Imtiyaz Khalani's answer. The new code is this :

    private void initilizeMap() {
            if (mMap == null) {
                //googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
                mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
                // check if map is created successfully or not
                if (mMap == null) {
                    Toast.makeText(getApplicationContext(),
                            "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        }
    

    But it gives the error : The method getSupportFragmentManager() is undefined for the type MainActivity

    • regeme
      regeme over 10 years
      did you import import com.google.android.gms.maps.GoogleMap ?
    • mobilGelistirici
      mobilGelistirici over 10 years
      @regeme importing it solved the GoogleMap problem. Thanks! for the other part, I hope Imtiyaz Khalani 's answer solves it :)
  • mobilGelistirici
    mobilGelistirici over 10 years
    Did I understand you right? You are saying "remove all googleMap references, and declare SupportMapFragment mMap; ", right ?
  • mobilGelistirici
    mobilGelistirici over 10 years
    If I define mMap as SupportMapFragment: error: Type mismatch: cannot convert from GoogleMap to SupportMapFragment If mMap is GoogleMap, then : Type mismatch: cannot convert from GoogleMap to SupportMapFragment
  • mobibob
    mobibob over 10 years
    i was able to import and add to my project, but it does not fix the issue for me. BUT i am confident that this is a major step in the right direction.