yet I have another problem with my android app
I developed a pretty basic application that contains a single Activity and displays google maps (I used google maps API). The compilation ends with no errors, I also generate the apk without any problems but when I run my application on different devices only displays a blank page with the Google sign below this page.
[![This the screenshot of my application][1]][1]
package com.example.amine.bea_mapapp;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">My API key is here</string>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.amine.bea_mapapp">
<permission android:name="com.androidcom.example.amine.bea_mapapp.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="com.androidcom.example.amine.bea_mapapp.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<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_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.androidcom.example.amine.bea_mapapp.providers.gsf.permission.READ_GSFREVICES"/>
<uses-feature android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="My API key is here" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
First thing you need to check Google account in that device is logged in or not
Second thing check inbuilt map in your testing device is working fine or not. Because google map access some services from device existing map application and your google account.?
Seems problem with your Google API keys. What key are you using at
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="My API key is here" />
If still struggling with issue, generate a new Google Map API keys with a keystore generated from your machine and build apk with that new MAP API key. I am sure it will work in all devices then.
Related
I have been learning android app development for a while and I am suffering from this annoying bug where my app crashes as I run it on my device (Mi a1, android 9, API 28). I haven't made any changes in the original code just entered my api in the appropriate places.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemapdemo">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
google_maps_api.xml
<resources>
<!--
TODO: Before you run your application, you need a Google Maps API key.
To get one, follow this link, follow the directions and press "Create" at the end:
https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=D0:2E:31:27:4F:07:82:C0:F9:CD:5F:14:70:FF:28:CB:29:85:5A:CE%3Bcom.example.googlemapdemo
You can also add your credentials to an existing key, using these values:
Package name:
D0:2E:31:27:4F:07:82:C0:F9:CD:5F:14:70:FF:28:CB:29:85:5A:CE
SHA-1 certificate fingerprint:
D0:2E:31:27:4F:07:82:C0:F9:CD:5F:14:70:FF:28:CB:29:85:5A:CE
Alternatively, follow the directions here:
https://developers.google.com/maps/documentation/android/start#get-key
Once you have your key (it starts with "AIza"), replace the "google_maps_key"
string in this file.
-->
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaSyDHi-e0aEeDxZbspTt5m5ydO0Kv5gxdd_8</string>
MapsActivity.java
package com.example.googlemapdemo;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
Error Message :
E/AndroidRuntime: FATAL EXCEPTION: Thread-6
Process: com.example.googlemapdemo, PID: 26667
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;
at fb.b(:com.google.android.gms.dynamite_mapsdynamite#15090083#15.0.90 (100408-231259764):3)
at fa.a(:com.google.android.gms.dynamite_mapsdynamite#15090083#15.0.90 (100408-231259764):3)
at fc.a(:com.google.android.gms.dynamite_mapsdynamite#15090083#15.0.90 (100408-231259764):15)
at com.google.maps.api.android.lib6.drd.al.a(:com.google.android.gms.dynamite_mapsdynamite#15090083#15.0.90 (100408-231259764):6)
at ee.a(:com.google.android.gms.dynamite_mapsdynamite#15090083#15.0.90 (100408-231259764):23)
at ee.run(:com.google.android.gms.dynamite_mapsdynamite#15090083#15.0.90 (100408-231259764):8)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.ProtocolVersion" on path: DexPathList[[zip file "/data/user_de/0/com.google.android.gms/app_chimera/m/000000bc/MapsDynamite.apk"],nativeLibraryDirectories=[/data/user_de/0/com.google.android.gms/app_chimera/m/000000bc/MapsDynamite.apk!/lib/arm64-v8a, /system/lib64, /system/vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at ad.loadClass(:com.google.android.gms.dynamite_dynamiteloader#15090083#15.0.90 (100408-231259764):4)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at fb.b(:com.google.android.gms.dynamite_mapsdynamite#15090083#15.0.90 (100408-231259764):3)
at fa.a(:com.google.android.gms.dynamite_mapsdynamite#15090083#15.0.90 (100408-231259764):3)
at fc.a(:com.google.android.gms.dynamite_mapsdynamite#15090083#15.0.90 (100408-231259764):15)
at com.google.maps.api.android.lib6.drd.al.a(:com.google.android.gms.dynamite_mapsdynamite#15090083#15.0.90 (100408-231259764):6)
at ee.a(:com.google.android.gms.dynamite_mapsdynamite#15090083#15.0.90 (100408-231259764):23)
at ee.run(:com.google.android.gms.dynamite_mapsdynamite#15090083#15.0.90 (100408-231259764):8)
Please help.
The LogCat have the error,
classnotfoundexception: Didn't find class
"org.apache.http.ProtocolVersion"
Searching on that error show many result like this belov, try this, this might solve the error, add this to your Androidmanifest.xml
<application
....
>
...
<uses-library android:name="org.apache.http.legacy"
android:required="false"/>
....
<application/>
Did you forget to add package name:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.googlemapdemo">
<application
android:allowBackup="false"
.....
I'm writing a Android app and there's a strange behavior related to FusedLocationApi.requestLocationUpdates() that I've searched on SO and none of the other related questions solved my problem.
Apparently the requestLocationUpdates do not trigger the onLocationChanged() method all the times; I'm saying this because I'm cleaning my app's data on emulator for each run and sometimes the onLocationChanged() is triggered, sometimes not (most of under some conditions like run the app for the second time w/o cleaning the app's data or after making a user data's wipe i.e)
Below you can find my code (links from pastebin, I don't want to create any visual overload here)
Main Activity: http://pastebin.com/XiRrcR11
MapHandler: http://pastebin.com/HELnhaxy
Another problem is, oddly enough, if I use a anonymous inner class in the MapHandler in the method displayLocation(), the app works, but if I use a external class who extends LocationListener the app do not behave correctly
I know the expected question levels in SO are high but I'm starting my studies about Android development and this issue is taking my sleep away
Btw here is my AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.trackit.app.track_it_v002">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Can you guys give me a hand here?
EDIT: after some debugging I've seen the requestLocationUpdates is not returning a non null object and not triggering onLocationChanged(), anyone have a idea why? =S
I encountered this issue. The mGoogleApiClient object, which needs to be connected at the time of requestLocationUpdates. This was resolved by requestingLocationUpdates from onConnected callback.
/*
* Called first time google client is connected
*/
#Override
public void onConnected(#Nullable Bundle bundle) {
Log.v(LOG_TAG,"DetectDrivingService - onConnected");
// Create
try {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
catch(SecurityException se){
}
}
i'm trying to build a app with a Google Maps Api V2, but i only got a blank screen with zoom buttons everytime. I think everything is fine in my code. I have all the permissions necessary, i correctly got the Key. I already delete debug.keystore to eclipse generate another one, but i still got the same blank screen with zoom buttons. Can anyone see if i doing something wrong in my codes ?
My Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.meu"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<permission
android:name="com.example.meu.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.meu.permission.MAPS_RECEIVE" />
<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" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="****My key here***" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".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>
My layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
My Java code:
package com.example.meu;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.app.Activity;
import android.app.AlertDialog;
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);
try {
// Loading map
setContentView(R.layout.activity_main);
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();
}
I already read a lot of things here, but no one could help me.
Firstly: As your map is a fragment (MAPS API V.2) you should extend your MainActivity as a FragmentActivity
public class MainActivity extends FragmentActivity {
instead of
public class MainActivity extends Activity {
Secondly: When casting the fragment as a map use the SupportMapFragment class (for better cmpatability between android versions) and getSupportFragmentManager() to find your map fragment.
((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
instead of
((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
Also: as per the comments of the question "asker"(!?): If you find that your app is working in an Emulator but not on an actual device, ensure that you use your Debug.Keystore when building the App.
Note: Ensure at all times that you are using backwards compatible support libraries across your entire app (If you are targeting multiple versions of android of course but I can't understand why anyone would limit their app to only a couple of versions?) and also that all your keys, credentials, Auths etc.. are properly set up in the Google developers console. https://console.developers.google.com/
After do what Ryno Coetzee suggest me, the app work on emulator but not on device. But the app doesnt work on device because when i export to a apk file, i was using another keystore instead debug.keystore. After that, my app finally work on device and on emulator.
Thanks !
I'm new to the Android world, and I need to create an app using Google Maps Android API. I follow the instructions in:
https://developers.google.com/maps/documentation/android/start
and whenever I start the AVD I get an error:
[2013-12-05 21:04:37 - Map] Failed to install Map.apk on device 'emulator-5554!
[2013-12-05 21:04:37 - Map] (null)
[2013-12-05 21:04:38 - Map] Launch canceled!
My MainActivity code:
package com.example.map;
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);
}
}
my MainActivity XML file:
<?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"
android:name="com.google.android.gms.maps.MapFragment"/>
and my Manifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.map"
android:versionCode="1"
android:versionName="1.0" >
<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"/>
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="19" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.xxxxxxxxxHIDDENxxxxxxxx"
android:value="xxxxxxxxxHIDDENxxxxxxxx"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.map.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>
Also tried the code that it's provided here:
https://developers.google.com/maps/documentation/android/
being the MainActivity file code:
package com.example.map;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
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.map_activity);
// Get a handle to the Map Fragment
GoogleMap map = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
LatLng sydney = new LatLng(-33.867, 151.206);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
map.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
}
}
Any Ideas?
The first thing I would recommend is to not show your API key to the public; this are supposed to be private to whomever is developing the application.
Also the format of this meta tag is wrong:
<meta-data
android:name="com.google.android.maps.v2.AIzaSyB7AMKbYsVajgb6D0zpq9wUODX9gdR2DhE"
android:value="AIzaSyB7AMKbYsVajgb6D0zpq9wUODX9gdR2DhE"/>
You are copying your API key at the end of the name. I strongly suggest you create a new API key and do not use this one that you have exposed to the public. There might be other errors, but without the stack trace is hard to say.
See this page: http://developer.android.com/google/play-services/setup.html
Specifically:
To test your app when using the Google Play services SDK, you must use
either:
A compatible Android device that runs Android 2.3 or higher and includes Google Play Store.
The Android emulator with an AVD that runs the Google APIs platform based on Android 4.2.2 or higher.
So make sure the emulator you're using is Android 4.2.2 or greater. If it is below this level, Google Play Services is not available on the emulator, and you must use a physical android device to test it out.
If you have to test on a lower-end device emulator, there are workarounds to this but they aren't officially supported by Google so your mileage may vary.
See this link:
Custom emulator that supports Google Maps API
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
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).
replace
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
to
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
Use FragmentActivity instead of Activity.
http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.GoogleMap;
Manually import the above statement your problem will be solved.
If you are getting the following errors, it's means that the GoogleMap class and the MapFragment class can't be found.
As those class are part of the google-play-service library, you have some problem with the way to reference it.
You can go over the first three steps of this guide I wrote:
Google Maps API V2
and make sure you are making it right.
Your original mapFragment should work just fine after importing the two packages mentioned earlier however you manifest is missing:
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />