0

我想使用 MapView,所以我得到了 SHA1 指纹,也得到了我的 apikey,但是在我的 MainActivity.java 文件中,当我导入 com.google.android.maps.MapView 它显示这个包不可用所以我不知道在哪里我犯了一个错误

这是我的清单

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

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

    <permission
        android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

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

    <uses-permission android:name="com.example.mapdemo.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" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.mapviewexample.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="my apikey" />
    </application>

</manifest>

这是我的 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"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

</RelativeLayout>

这是我的 MainActivity.java 文件

package com.example.mapviewexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
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 Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

正如你在上面看到的,当我导入这个时

import com.google.android.gms.maps.CameraUpdateFactory;

和其他它表明这些包不可用。我应该怎么办?

4

7 回答 7

1

更改您的项目构建目标,它可能是标准的 android 平台,而不是您应该选择 Google-API 项目。

于 2013-01-15T12:38:56.430 回答
0

MapView 总是显示在MapActivity. 你正在使用Activity. 尝试使用MapActivity和 google api 模拟器。 <uses-library android:name="com.google.android.maps" /> 把它放在你的应用程序标签中

于 2013-01-15T12:04:44.190 回答
0

我没有看到关于将包含 com.google.android.gms.maps 和其他类的库放在类路径上的任何内容。

于 2013-01-16T08:45:19.990 回答
0

要扩展@gaurav-jain 给出的答案,Android 项目不会自动包含谷歌地图。您需要针对 Google API 的适当版本,而不是 Android 的适当版本。

项目属性

在这里,您可以在 Eclipse 中看到一个 Android 项目。我右键单击该项目并选择属性,然后单击进入 Android 部分。可以看到目标版本是 Android 4.2。

在这个项目中,谷歌地图将不可用。如果我想使用地图,我需要将目标更改为 Google API 4.2。

如果您没有在列表中看到 Google API,那么您需要使用 SDK 管理器下载并安装所需的版本

于 2013-01-16T10:32:51.527 回答
0

您需要在清单中使用 uses-library android:name="com.google.android.maps" 并且 sdk 应该是 google-api,转到您的项目属性,然后 android 并从那里选择任何 google api 并扩展您的 Mapactivity 课程。

于 2013-01-15T12:02:53.827 回答
0
First of all **GET THE GOOGLE MAPS API KEY**

To allow you to show maps on your application, Google Maps API needs to identify you and your application (even if you are simply developing). For this you need the API Key for the Android platform.
You can get this by creating an md5 checksum of the debug certificate for you map application (in this case the tutorial).
Find your debug.keystore at
* Windows Vista: C:\Users\<user>\.android\debug.keystore
* Windows XP: C:\Documents and Settings\<user>\.android\debug.keystore
Then use the Keytool (found at C:\Program Files\Java\jdk1.6.0_20\bin) and get the md5 checksum by executing this:
keytool -list -alias androiddebugkey -keystore “C:\Documents and Settings\<user>\.android\debug.keystore” -storepass android -keypass android


after that start coding and be sure **you select the relevant version of Google APIs (in case there’s a newer version by the time you are working on this).**

**strings.xml**

    <?xml version="1.0" encoding="utf-8"?>
<resources>
 <string name="hello">Hello World, HelloGoogleMaps!</string>
 <string name="app_name">Hello,GoogleMaps</string>
 <string name="mapskey">YOUR API KEY</string>
</resources>

**main.xml**

    <?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"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:clickable="true"
 android:apiKey="YOUR API KEY"
/>

**AndroidManifest.xml**

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.HelloGoogleMaps2"
 android:versionCode="1"
 android:versionName="1.0">
 <application android:icon="@drawable/icon" android:label="@string/app_name">
 <activity android:name=".HelloGoogleMaps2" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar">
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 <uses-library android:name="com.google.android.maps" />
 </application>
 <uses-permission android:name="android.permission.INTERNET" />
</manifest>


**HelloGoogleMaps2.java**

    public class HelloGoogleMaps extends MapActivity
{
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 MapView mapView = (MapView) findViewById(R.id.mapview);
 mapView.setBuiltInZoomControls(true);

 List<Overlay> mapOverlays = mapView.getOverlays();
 Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
 HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);
 GeoPoint point = new GeoPoint(30443769,-91158458);
 OverlayItem overlayitem = new OverlayItem(point, "Laissez les bon temps rouler!", "I'm in Louisiana!");

 GeoPoint point2 = new GeoPoint(17385812,78480667);
 OverlayItem overlayitem2 = new OverlayItem(point2, "Namashkaar!", "I'm in Hyderabad, India!");

 itemizedoverlay.addOverlay(overlayitem);
 itemizedoverlay.addOverlay(overlayitem2);

 mapOverlays.add(itemizedoverlay);
 }
 @Override
 protected boolean isRouteDisplayed()
 {
 return false;
 }
}

    **HelloItemizedOverlay.java**

    public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem>
{
 private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
 private Context mContext;

 public HelloItemizedOverlay(Drawable defaultMarker, Context context)
 {
 super(boundCenterBottom(defaultMarker));
 mContext = context;
 }

 public void addOverlay(OverlayItem overlay)
 {
 mOverlays.add(overlay);
 populate();
 }
 @Override
 protected OverlayItem createItem(int i)
 {
 return mOverlays.get(i);
 }
 @Override
 public int size()
 {
 return mOverlays.size();
 }
 @Override
 protected boolean onTap(int index)
 {
 OverlayItem item = mOverlays.get(index);
 AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
 dialog.setTitle(item.getTitle());
 dialog.setMessage(item.getSnippet());
 dialog.show();
 return true;
 }
}
于 2013-01-15T12:20:52.860 回答
0
  1. 确保您的关键工作。先用google例子试试
  2. 您必须转发所有Activity 生命周期方法
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mapView.onCreate(savedInstanceState);
    try {
       MapsInitializer.initialize(mContext);
    } catch (GooglePlayServicesNotAvailableException e) {
       e.printStackTrace();
    }
 }

@Override
public void onPause() {
    super.onPause();
    mapView.onPause();
}

@Override
public void onResume() {
   super.onResume();
   mapView.onResume();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}
于 2013-01-15T12:46:37.510 回答