How to Create an Android Application in Android Studio That Uses the Google Maps API V2

How can I create an Android application in Android Studio that uses the Google Maps Api v2?

Finally I managed to run GoogleMapsAPIv2 project using Android Studio.

EDIT: As mentioned by Xavier, this method is going to work for non-gradle based projects only. And UI part which was used in this tutorial will be excluded from AndroidStudio. So if you have your own project which uses Gradle build system, you need to manually modify build.gradle configuration file since Android Studio UI doesn't affect it.

EDIT2: With AndroidStudio v0.1.1 release, UI part responsible for modules dependencies has been eliminated, so for now we need to update dependencies manually through build.gradle file. UI for changing gradle dependencies is going to be released in next releases

EDIT3: For those who still tries to use this approach - please note that it is obsolete and doesn't work anymore

Here is what I did:

1) I took maps project from the Google Play Services samples and copied that to the separate directory. That is going to be our MapsApiV2 project we will be trying to run. On my Mac it was located at <sdk_location>/extras/google/google_play_services/samples
I placed it to the ~/Work/stack/

2) Copied google-play-services_lib project directory to the same place (~/Work/stack), so my working directory looks like this. Btw, lib project is located at <sdk_location>/extras/google/google_play_services/libproject:
Sample Image

3) Now let's open Android Studio. On welcome screen press Import Project and import our maps project from ~/Work/stack/maps. Now we see a lot of complaints about unknown reference to GMS library:

Sample Image

4) Now we need to add Google Play Service as a reference library. Going to View -> Open Module Settings

5) On the Modules tab, click + button and select Import Module and import your GooglePlayServices lib. I didn't change anything in the wizards, so clicked Next all the way to the end:

Sample Image

6) Now you need to reference this imported library. Open this screen again (go to View -> Module Settings). Make sure you have your maps project and Dependency tab selected. Click + to add a dependency and select Library. Choose your imported library there:

Sample Image

7) Now we can see that it is not complaining about GMS library, but still complaining about support library:

Sample Image

8) Let's fix it. I have my support library located at <sdk location>/extras/android/support/v13/android-support-v13.jar. So let's try to add it to our workspace. Go to View -> Open Module Settings and select Libraries tab. Select + -> Java and select support library:

Sample Image

9) Now it is going to ask you which project to add this lib to, so make sure you have selected your maps project:

Sample Image

10) At this point code should compile w/o problems. Just make sure you are targeting the right SDK version in Manifest.

Have fun

Android Studio integrate Google Maps API v2

Thank you all that you spend your time helping me.

I found the answer myself. My mistake is that I have put uses-permission and uses-features inside the <application> ... </application> tags.

They must be placed inside the <manifest> ... </manifest> tags.

The correct AndroidManifest.xml is:

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

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

<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=".MapActivity"
android:label="@string/title_activity_map">
<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.geo.API_KEY"
android:value="MY_API_KEY_IS_HERE" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

</application>
</manifest>

Google Maps API Level 2 - Sample Code - Android Studio

Took a closer look at the logcat and found this:

 Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml

The problem was in the manifest.

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

I put my API key at the end of com.google.android.maps.v2. instead of leaving it as

 android:name="com.google.android.maps.v2.API_KEY"

Did not read the instructions on the web page closely enough. They clearly state:

Substitute your API key for API_KEY in the value attribute.

Doh.

Google Maps Android API v2 tutorial

The only problem I currently see here is this line:

 <uses-library android:name="com.google.android.maps" />

This line belong to Google Map API v1 and shouldn't be written in Google Map API V2.

We can't help you more then this without the logcat output.

UPDATE:

If now you pasted the right manifest file then your current problem is:

1. You are developing you application for min SDK 8 so you have to use the SupportMapFragment instead of the MapFragment object in you XML layout file of the Activity. like this:

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

You can take a look at this blog post I wrote on how to add Google Map API V2 to you application:

Google Map API V2

Ensure that the Google Maps Android API v2 is enabled. I am getting this error when I try to Implement Google Maps

From error it is clear that You did not enable google map api for android. To enable,

  1. Login google developer console
  2. Select Library option from left side panel.
  3. Now you can see all API list and go to Google Maps APIs and select
    Google Maps Android API .
  4. Now you can see option to enable/disable API. Enable it.

I hope it help you.

using google map v2 or v3 in android application?

Google Maps API V2 is the native version of Google maps, while Google Maps API V3 is new API for web development, you could use the API V3 and embed it into a WebView, but if you want to achieve the best result you should use Google Maps API V2 for Android.

Here a blog guide I wrote on how to embed Google Maps API V2 in your application that you can use to get started:

Google Maps API V2

Adding Google maps api v2 to an existing project

The device must have Google play services installed for Google Maps Android v2 to run:

The API is now distributed as part of the Google Play services SDK, which you can download with the Android SDK Manager. To learn how to install the package, see Installing the Maps API SDK.

You will find these docs useful!

If your application is running v1, it's probably best to run a check to see if Google Play services is installed, and if not use the old map. I've not tested it, but check the answer here for running that check. I've also found, from here you can do this:

int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext();
if(status == ConnectionResult.SUCCESS) {
//Success! Do what you want
}

And use the following types to determine if Google Play Services is installed on the device:

public static int isGooglePlayServicesAvailable (Context context)

Verifies that Google Play services is installed and enabled on this device, and that the version installed on this device is no older than the one required by this client.

Returns
status code indicating whether there was an error. Can be one of following in ConnectionResult: SUCCESS, SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED, SERVICE_DISABLED, SERVICE_INVALID.

To add the map using a fragment you will need to do something like this:

private GoogleMap map;
private MapFragment mapFragment;
private void InitMap()
{
mapFragment = ((MapFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_map));

map = mapFragment.getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.setMyLocationEnabled(false);
}

For Google Maps Android v2 and fragments, as mentioned above, this is a great resource! Oh, and remember to use the Google Play services SDK!

Google Maps Android api v2 and current location

You can call startPerc.remove(); to delete only this marker.

From here

Google Map Android Api V2 Sample Code not working

Please check..

  1. check if the "libs" folder containing the "android-support-v4.jar" exists in your project.

    "android-support-v4.jar" is located in "/extras/android/compatibility/v4/android-support-v4.jar" under your "android-sdk" drectory.

  2. Before running your project, you must set your project Build target to "Google APIs", not Android x.x. version : Select your project and click Project > Properties > Project Build Target in Eclipse and select any "Google APIs ", and then run your project on your phone. If you use the emulator, also MUST set the AVD of the emulator to the any "Google APIs ".

  3. Once more, you don't need to create the new Google Maps API key in order to test your project, Just use the default provided API key, which is shown as "Key for browser apps (with referers) "in your Google APIs Console.

  4. Finally, the most important is to add Google Play services as an Android library project as follows:

    Select File > Import > Android > Existing Android Code Into Workspace and click Next.
    Select Browse..., enter /extras/google/google_play_services/libproject/google-play-services_lib, and click Finish.



Related Topics



Leave a reply



Submit