Reputation: 1872
I have implemented android Google maps V2 in my application and its working fine on most of the devices(Galaxy Ace Plus, Galaxy 7inch Tab, Sony Xperia Z) i tested. But it's not working on HTC Desire. i have cross verified the Google play services also installed in that device. i goggled a lot to find a solution. below is my implementation.
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment fragment = new SupportMapFragment();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, fragment).commit();
}
}
below is activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/the_map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTilt="45"
map:cameraZoom="2" />
and here is manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.raj.googlemapsv2test"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.raj.googlemapsv2test.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.raj.googlemapsv2test.permission.MAPS_RECEIVE" />
<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" />
<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="8"
android:targetSdkVersion="17" />
<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="AIzaSyDD6bvUVkTk2UzOLpojdeBtlh5xBYDhy6E" />
<activity
android:name="com.raj.googlemapsv2test.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>
when i check the logs its showing the following error lines.
Could not find class 'maps.i.k', referenced from method maps.z.ag.a
Failed to load map. Could not contact Google servers.
Failed to load map. Could not contact Google servers.
and finally with a small doubt i updated the goggle maps app in the device and after that it's working. So I wants to show an alert to the user for update when this kind of issue is found with any of the devices. But unable to know how to check this. So Can any one suggest some way to find whether the goggle maps app and android goggle play services in the device are of the supportive version to this android Goggle Maps V2 or not.
Any solution will be appreciated.
Upvotes: 0
Views: 1321
Reputation: 81
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
supportMapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (supportMapFragment == null) {
supportMapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, supportMapFragment).commit();
}
}
at this time google play services are not initialize so get map and add what ever you want to it in onResume() or onStart()
@Override
public void onResume() {
super.onResume();
if (gMap == null) {
supportMapFragment.getMapAsync(this);
}
}
after adding this u need to implement OnMapReadyCallback. and do what ever operations u like to do with map.
Upvotes: 0
Reputation: 1844
You can add this code to your onResume method. It checks if the user has an updated version of the Google Play Services, and if he does not, a popup redirecting him to the Google Play is shown.
@Override
protected void onResume() {
super.onResume();
int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(errorCode != ConnectionResult.SUCCESS && GooglePlayServicesUtil.isUserRecoverableError(errorCode)){
Dialog d = GooglePlayServicesUtil.getErrorDialog(errorCode, this, 0, new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
//As my application does a heavy use of maps, if the user cancels the dialog, I just finish the FragmentActivity
MainFragmentActivity.this.finish();
}
});
d.show();
}
}
Hope that helps, cheers!
Upvotes: 6