问题
Ok, so I got my SHA1 fingerprint by creating a batch file. After that, I went to the "Services" page and enabled Google Maps API v2 and v3. I also went to the "API Access" page and created a new Android Key with the SHA1;PACKAGENAME. After this, I went to my project folder and added the following code:
<permission
android:name = "com.example.test.permission.MAPS_RECEIVE"
android:protectionLevel = "signature"
/>
<uses-feature
android:glEsVersion = "0x00020000"
android:required = "true"
/>
<uses-permission android:name="com.example.test.permission.MAPS_RECEIVE" />
<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" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-library android:name="com.google.android.maps" />
<meta-data
android:name = "com.google.android.maps.v2.API_KEY"
android:value = "APIKEY"
/>
I also created an XML file and pasted the following code:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
xmlns:android = "http://schemas.android.com/apk/res/android"
android:id = "@+id/mapview_MV"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:clickable = "true"
android:apiKey = "APIKEY"
/>
After compiling and running the project on my HTC One X+, I saw nothing more than bunch of grey tiles. I also tried to use a MapView in code as shown below:
/********************************************************************
* ONCREATE *
********************************************************************/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.general_layout );
MapView mapView = new MapView( GamePage.this, "APIKEY" );
MapController mc = mapView.getController();
String coordinates[] = {"1.352566007", "103.78921587"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
GeoPoint p = new GeoPoint( (int) (lat * 1E6), (int) (lng * 1E6) );
mc.animateTo(p);
mc.setZoom(17);
FrameLayout fl = ( FrameLayout ) findViewById(R.id.general_frameHolder_FL);
fl.addView(mapView);
}
I get to see the following error:
Couldn't get connection factory client
Can anyone tell me what the hell I'm doing wrong!?
回答1:
When we are implement MapView
in Fragment
or Activity
must implement its life cycle methods docs
put mMapView.onCreate(savedInstanceState);
inside onCreateView()
or onCreate()
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
回答2:
Try enable Google maps Android API v2
Meta key must below Application Tag
<permission
android:name = "com.example.test.permission.MAPS_RECEIVE"
android:protectionLevel = "signature"
/>
<uses-feature
android:glEsVersion = "0x00020000"
android:required = "true"
/>
<uses-permission android:name="com.example.test.permission.MAPS_RECEIVE" />
<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" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-library android:name="com.google.android.maps" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- key v2 -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="Bla Bla" />
<uses-library
android:name="com.google.android.maps"
android:required="false" />
<activity
android:name="com.example.test.MainActivity"
android:label="@string/st_google_activity_main" >
</activity>
</application>
And if you use MapView, You must forward all the Activity life cycle methods.
XML layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
class
public class MainActivity extends android.support.v4.app.FragmentActivity implements OnMapClickListener , OnMapLongClickListener , OnCameraChangeListener {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_googlev2);
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.setOnMapClickListener(this);
mMap.setOnMapLongClickListener(this);
mMap.setOnCameraChangeListener(this);
}
}
回答3:
It seems to me api-key issue .. Try to give in this way
android:apiKey="0BYX7MFyg8lWRKFxzwG-D93VMxZP6wH5eObwEtw"
Check you logs ..are you getting any error log from Google Map Lib ?
回答4:
I found a solution for the problem. What I needed to do was delete the Google Play Services in the SDK Manager and reinstall it, because I missed lots of packages for some reason. Thanks to Fido, I found out that MapView isn't supported in Google Maps Android v2 API and read the whole introduction at: https://developers.google.com/maps/documentation/android/start#specifying_permissions.
After that, I also found a site where you can solve the NoClassFoundError at: http://www.user.tu-berlin.de/hennroja/tutorial_fixing_java.lang.NoClassDefFoundError%20com.google.android.gms.R$styleable.html
It took me +- 5 hours to solve this freaking issue with the new Google Maps API.
来源:https://stackoverflow.com/questions/14395809/why-does-mapview-show-grey-tiles-and-not-the-map