user2254979
user2254979

Reputation: 45

Android and Google Map v2

I have tried every example I saw on the internet about the Google Map and android but all of them did not work.

I tried on the emulator and on a real device but all give me an error about the fragment and class not found.

I did import the Google Play Service add to the project, put the Google API key but still not work.

This is the last example I used:

googlemapv2.java

package com.example;

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

public class MainActivity extends Activity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

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

manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <permission 

        android:name="com.example.permission.MAPS_RECIEVE"
        android:protectionLevel="signature"/>

     <uses-permission android:name="com.example.permission.MAPS_RECIEVE" />
     <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="android.permission.ACCESS_COARSE_LOCATION"/>
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


     <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" >

        <meta-data 
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="API key"/>
        <activity
            android:name="com.example.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>
    </application>

</manifest>

Upvotes: 1

Views: 261

Answers (1)

Emil Adz
Emil Adz

Reputation: 41099

If you receiving the fragment/class not found error then you problem is probably for lacking a good reference of the google-play-services library as MapFragment is part of this library:

Check that yours Android Dependencies folder (blue) hold the google-play-services (orange) inside you will find the com.google.android.gms.maps package (green) that has the MapFragment class (red):

enter image description here

If this is not the case you can check the first 3 steps of this blog post I wrote to get and idea on how to reference this package correctly:

Google Maps API V2

Upvotes: 1

Related Questions