问题
I have used many samples of Google map V2, and i have followed all the things which are necessary for this. But still having problem, only map view is showing and my current location is not being displayed on my emulator.
I have already created my system MD 5 key and API key from API console and used the SHA 1 key in manifest. But still having problem.
So please help me.
My Source Code :
MainActivity.java
package com.googlev2.android;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().
findFragmentById(R.id.map);
mMap = fm.getMap();
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
final LatLng CIU = new LatLng(35.21843892856462, 33.41662287712097);
Marker ciu = mMap.addMarker(new MarkerOptions().position(CIU).title("My Office"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
And AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.googlev2.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<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"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<permission
android:name="com.googlev2.android.permission.MAPS_RECEIVE"
android:protectionLevel="signature">
</permission>
<uses-permission
android:name="com.googlev2.android.permission.MAPS_RECEIVE"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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="AIzaSyDciL7-T3BphxGv2q-A77vNrcyJQ_sTrgI"/> -->
<activity
android:name="com.googlev2.android.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>
<!-- <meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBDUb-4cOSTeeH3XqVQHwzXL2bT0aAM2h8"/>
-->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDiLovJFMLKEZkI5Ijtl0fKA-qApScD_Pk"/>
</application>
</manifest>
回答1:
If your manifest file is correct Try this man
activity_main.xml
<?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:name="com.google.android.gms.maps.SupportMapFragment"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTargetLat="33.41662287712097"
map:cameraTargetLng="35.21843892856462"
map:cameraZoom="4"/>
</LinearLayout>
In MainActivity.java onCreate()
public class MainActivity extends FragmentActivity {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap mMap = mapFragment.getMap();
mMap.addMarker(new MarkerOptions()
.position(new LatLng(35.21843892856462, 33.41662287712097))
.title("My Office"));
}
Here is the result on my phone

回答2:
Check whether you add Activate the Google Maps Android API v2 in your Google APIs Console more details can be view in this linkHow to Integrate Google map v2
回答3:
The keys to a working Google Maps v2 are
- Add the v4 Support library (for older devices) and
google-play-services.jar
to your project. - Import the google-play-services_lib dummy project as a dependent project. It contains the very important
res/
directory with all the localized resources. - Make sure you created a keystore and key and that you are signing your APK with it. If you are using a debug keystore you need to add that to the Google API console.
- Test on an Android device NOT the emulator. Google Play Services are not available on the emulator and you will not get a map. You get a white screen in the emulator IIRC.
- Check that your AndroidManifest.xml has all the important entries - OpenGL 2, Meta-Key with your key, the permissions, etc.
回答4:
After your meta-data that contains the api key you need to add the following lines
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
I suggest you to prove the genymotion emulator that is 20x times better than the default one. Here you can find the instructions to set it correctly:
How do you install Google frameworks (Play, Accounts, etc.) on a Genymotion virtual device?
来源:https://stackoverflow.com/questions/16535136/google-map-v2-not-working-for-me-any-sample