Google Maps V2无效

时间:2013-10-16 08:04:15

标签: android google-maps android-maps-v2

我正在使用谷歌地图v2并收到此错误。

无法找到从map.ag.an.a

方法引用的类'maps.af.k'

我正确遵守所有规则。我创建了SHA1指纹并将其添加到ANdroid Key中,用于具有项目名称的apikey

1F:41:29:EC:E8:07:3C:A9:F8:6E:EB:D2:7B:42:41:62:3A:36:CA:F2;com.example.routes

然后在mainest文件中使用

<fragment
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/> 

我还导入了googleplayservices并正确地将其添加到项目中

我的代码是

public class MainActivity extends FragmentActivity {
GoogleMap googleMap;
@Override
public 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();
    ;

     googleMap = ((SupportMapFragment)    

  getSupportFragmentManager().findFragmentById(
                R.id.map)).getMap();
  }
}

但我仍然遇到了这个错误。

3 个答案:

答案 0 :(得分:0)

尝试扩展Activity而不是FragmentActivity:

  public class MainActivity extends Activity implements LocationListener{
LocationManager locationManager;
    private GoogleMap googleMap;
double lat=17.385044;
double long=78.486671;
 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
    LocationManager service=(LocationManager)getSystemService(LOCATION_SERVICE);
    locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
 Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
   Location location = locationManager.getLastKnownLocation(provider);
  private void initilizeMap(){
if(googleMap==null){
 googleMap=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
    //check if map is created succesfully or not
    if(googleMap==null){
Toast.makeText(getApplicationContext(),"sorry unable to create map",1).show();
    }
 } 
MarkerOptions markerOptions=new MarkerOptions().position(new LatLng(lat,long));
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher));
CameraPosition cameraPosition=new CameraPosition.Builder().target(new LatLng(17.385044,     78.486671)).zoom(12).build();
 googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
 googleMap.setMyLocationEnabled(true);
 googleMap.setOnInfoWindowClickListener(null);
 }

  @Override
  protected void onResume(){
 super.onResume();
 initilizeMap();
   }

答案 1 :(得分:0)

当您使用SupportMapFragment时,无需延长FragmentActivity只需延长Activity请参阅以下示例代码:

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

public class MainActivity extends Activity {

private GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    map =  ((MapFragment)   getFragmentManager().findFragmentById(R.id.map)).getMap();

}

@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;
}

}

注意:您必须添加google play services库项目作为项目的参考,您的包名必须是com.example.routes您必须在xml布局中添加不在清单中的地图片段。 对Manifest.xml进行以下更改 添加以下权限。

<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"/>

添加OpenGL

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

添加API密钥

<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="API_KEY"/>

示例清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.package"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

   <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"/>

<used-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" >

    <activity
        android:name="com.example.package.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="YOUR_API_KEY_HERE"/>
</application>

答案 2 :(得分:0)

我有同样的问题,我的解决方案是删除并再次添加googleplayservices。