My steps:
created AndroidManifest file:
<permission
android:name="my.package.name.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="my.package.name.permission.MAPS_RECEIVE"/>
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15"/>
<uses-permission android:name="android.permission.INTERNET"/>
<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-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:hardwareAccelerated="true">
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="MY_API_KEY_HERE" />
<activity android:name="MyActivity"
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>
created layout
After compilation I've got crash:
ERROR/AndroidRuntime(10182): FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
at android.app.Activity.onCreateView(Activity.java:4716)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
at android.app.Activity.setContentView(Activity.java:1881)
at com.example.gm2.MyActivity.onCreate(MyActivity.java:16)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
After that I've changed layout to:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
and changed MyActivity to
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.map, MapFragment.newInstance());
transaction.commit();
As result, the application was started, but I didn't see the map.
Console log:
ERROR/Google Maps Android API(10369): Authorization failure.
There are a several reasons why your google maps may not be working, the most common issue being no Google Map API key set or set incorrectly. To use the Google Maps JavaScript API, you must register your app project on the Google Cloud Platform Console and get a Google API key which you can add to your app.
You won't be charged until your usage exceeds $200 in a month. Note that the Maps Embed API, Maps SDK for Android, and Maps SDK for iOS currently have no usage limits and are at no charge (usage of the API or SDKs is not applied against your $200 monthly credit).
Steps:
android.library.reference.1=google-play-services_lib
YOUR_PROJECT/proguard-project.txt
.
-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents();
}
Now you are ready to create your own Google Map app with using Google Map APIs V2 for Android.
If you create application with min SDK = 8, please use android support library v4 + SupportMapFragment instead of MapFragment.
Also check this post: Google Map Android Api V2 Sample Code not working, if you are completely sure you did the right steps then follow the second answer, the authentication gets cached somewhere, try to uninstall the application manually (just like you do with a normal application) then "Run" again the project
Since I just wasted a lot of time getting the API to work, I will try to give a step-by-step validation for the Map API v2:
Step 1: Apply for your API key
If you are unfamiliar with the Google API console, read the very good answer of Rusfearuth above.
Step 2: Check you SHA Hash (in this case I use the debug key of eclipse):
On a Windows machine got to your user directory on a command prompt:
C:\Users\you>keytool -list -alias androiddebugkey -keystore .android\debug.keyst
ore -storepass android -keypass android
You will get something like:
androiddebugkey, 15.10.2012, PrivateKeyEntry,
Zertifikat-Fingerprint (SHA1): 66:XX:47:XX:1E:XX:FE:XX:DE:XX:EF:XX:98:XX:83:XX:9A:XX:23:A6
Then look at your package name of the map activity, e.g. com.example.mypackagename
You combine this and check that with your settings in the Google API console:
66:XX:47:XX:1E:XX:FE:XX:DE:XX:EF:XX:98:XX:83:XX:9A:XX:23:A6;com.example.mypackagename
where you get your API-key:
AZzaSyDhkhNotUseFullKey49ylKD2bw1HM
Step 3. Manifest meta data
Check if the meta-data are present and contain the right key. If you release your app, you need a different key.
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AZzaSyDhkhNotUseFullKey49ylKD2bw1HM" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
Step 4. Manifest features:
You need this entry as the map API requires some grapics support:
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
Do not worry, 99.7% of devices support this.
Step 5. Manifest library:
Add the google library.
<uses-library
android:name="com.google.android.maps"
android:required="false" /> // This is required if you want your app to start in the emulator. I set it to false also if map is not an essential part of the application.
Step 6. Manifest permissions:
Check the package name twice: com.example.yourpackage
<permission
android:name="com.example.yourpackage.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.yourpackage.permission.MAPS_RECEIVE" />
Add the following permissions:
<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" />
The following permissions are optional and not required if you just show a map. Try to not use them.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Step 7. Include the map fragment into your layout:
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
class="com.google.android.gms.maps.SupportMapFragment"
map:cameraTargetLat="47.621120"
map:cameraTargetLng="-122.349594"
map:cameraZoom="15" />
If your release to 2.x Android versions you need to add support in your Activity:
import android.support.v4.app.FragmentActivity;
For the map: entries to work include
xmlns:map="http://schemas.android.com/apk/res-auto"
in your activity layout (e.g. LinearLayout).
In my case I have to clean the project each time I change something in the layout. Seems to be a bug.
Step 8: Use Eclipse - Project - Clean.
Enjoy!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With