【问题标题】:Google Maps V2 Not Woking谷歌地图 V2 不工作
【发布时间】:2013-10-16 08:04:14
【问题描述】:

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

找不到从方法 maps.ag.an.a 引用的类“maps.af.k”

我正确地遵守了所有规则。我已经创建了 SHA1 指纹并将其添加到带有项目名称的 apikey 的 ANdroid Key

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();
  }
}

但我仍然收到该错误。

【问题讨论】:

  • 你添加了谷歌播放服务吗?
  • 在 xml 中添加片段代码而不是清单文件,并且一旦发布您的清单文件代码
  • @user2842224 请发布您的 logcat 错误以及清单文件。
  • 您能否使用所有格式正确的信息编辑您的答案?谢谢。

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


【解决方案1】:

尝试扩展 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();
   }

【讨论】:

  • 如果我将使用 Activity 而不是 FragmentActivity ,它将不支持 Gingerbread 即支持片段管理器,我必须在 2.2 上使用它,所以我需要一个支持片段管理器的解决方案。
  • 无需使用 FragmentActivity 它将在姜饼上运行
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    当您使用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 library 项目作为对您项目的引用,您的包名称必须是 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-16
      相关资源
      最近更新 更多