问题
I am trying to get a basic map application to work but, I am getting only the grid screen.
Code:
1.ActivityMain.java
import android.os.Bundle;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class MainActivity extends MapActivity {
MapView mapView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.map_view);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
2.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:enabled="true"
android:apiKey="[api_key]" />
</RelativeLayout>
3.Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vevolet.maps"
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="android.permission.ACCESS_NETWORK_STATE" />
<permission
android:name="com.vevolet.maps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" >
</permission>
<uses-permission android:name="com.vevolet.maps.permission.MAPS_RECEIVE" />
<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" />
<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" >
<uses-library android:required="true" android:name="com.google.android.maps" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="[api_key]" />
<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>
</manifest>
I also tried this code with api generated using the keystore generated from the application..but it did not work.
Please tell me what the problem is. Thanks
回答1:
This is because you mixing up both API version of Google Maps. On one hand you are using MapView
which is an API V1
object on the other hand you have produced your key for API V2
and using the permissions for this version.
So because API V1
is deprecated and no longer available and because you are on half way to implement Google Maps API V2
what you should do is:
1. Remove this permission, as it's a API V1 permission:
<uses-library android:required="true" android:name="com.google.android.maps" />
2. Replace this code in your xml:
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:enabled="true"
android:apiKey="[api_key]" />
whit this one:
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
3. Remove this line from your java code:
mapView = (MapView) findViewById(R.id.map_view);
You can take a look at this guide I wrote on Google Maps API V2 to get the full idea of it's implementation:
Google Maps API V2
回答2:
Your min sdk is 8. You should use Support Fragment.
Instead of this
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:enabled="true"
android:apiKey="[api_key]" />
Use the below
<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Your activity must extend FragmentActivity
. Will be using SupportFragment
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap mMap = fm.getMap();
Make sure you have added support library
Also make sure you imported the below
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.SupportMapFragment;
Also if you have updated adt to rev 22
Right click on your project goto properties. Java Build Path. Choose Order export tab. Make sure that Android Private Libraries is selected. If you have referenced library project. do the same for the google play services library project also. Clean and Build.
You can remove this as suggested by EmilAdz
<uses-library android:required="true" android:name="com.google.android.maps" />
Make sure you have followed the steps in the below link
https://developers.google.com/maps/documentation/android/
Also make sure you have enabled maps for android in google api console under the services tag.
Test in on a device.
Edit:
You should refer to google services library project in your map project
Download the Google Play services.Goto Windows. Goto Android Sdk Manager. Choose Google play services under extras. If not installed install the package.
Copy the google-play services_lib library project to your workspace. The library project can be found under the following path.
<android-sdk-folder>/extras/google/google_play_services/libproject/google-play-services_lib library project .
Import the library project to your eclipse.
Click File > Import, select Android > Existing Android Code into Workspace, and browse the workspace import the library project. You can check if it is library project. Right click on the library project. Goto properties. Click Android on the left panel. You will see Is Library checked.
To refer to the library project
Right click on you android map project. goto properties. Choose Android. Click Add borwse and add the library project.
回答3:
I feel pretty stupid answering my own question(esp. with a bounty on it) but I'm doing it anyway.
Raghunandan
and Emil Adz
solved the first mistake I did(of mixing gmaps v1 and v2 code), thank you guys.
But then I encountered two other problems:
Problem 1: The library was failing to link with my project.
Problem 2: I was getting an error "error inflating the class fragment"
Solution:
For Problem 1: I copied the google-play-services_lib
folder inside C:\Android\android-sdk\extras\google\google_play_services\libproject
and pasted it into another drive and then imported it from the new location and referenced it to my project.
For Problem 2: I replaced the MapFragment
with SupportMapFragment
in inside <fragment>
(inside my activity xml) which is for the native API Level 11 edition of fragments.
回答4:
Which certificate did you use to generate the API key? If you launch from Eclipse, your apk is signed with a debug certificate, not your release certificate, so you need a different API key for the debug keystore.
See these instructions.
回答5:
Check out this answer--
Can't make google maps v2 to work
This occurs when the key of your google project does not match with the one you specify in the manifest. Do go through all the directed steps again, and check your key.
回答6:
I faced the same problem , it works for me when I tested in device with openGL 2.0. But when I tested in version 2.2.1 it shows only the grid screen, due to lack of openGL2.0. But I used SupportMapFragment instead MapView in XML as specified by google-maps-api-v2 documentation. Also ensure google play services get's updated before testing.
回答7:
I have few different applications that uses Google Maps and only one of them has problem similar to yours, next solution helped me:
- Go to Google API Console
- Open API Access tab
- Choose "Create New Android key"
- Use new generated API key in your AndroidManifest.xml
来源:https://stackoverflow.com/questions/16944232/app-shows-grid-screen-instead-of-google-map