Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么我不能使用MapView?_Android_Android Mapview - Fatal编程技术网

Android 为什么我不能使用MapView?

Android 为什么我不能使用MapView?,android,android-mapview,Android,Android Mapview,我想使用MapView,所以我得到了SHA1指纹,也得到了我的apikey,但在我导入com.google.android.maps.MapView的MainActivity.java文件中,它显示这个包不可用,所以我不知道我在哪里出错 这是我的舱单 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" pac

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

这是我的舱单

<?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>
正如我在上面导入时看到的

import com.google.android.gms.maps.CameraUpdateFactory;
而另一些则显示这些软件包不可用。我该怎么办?

您需要使用 使用库android:name=com.google.android.maps 在您的清单中,sdk应该是google api,转到您的项目属性,然后转到android并从中选择任何google api
并使用Mapactivity扩展类。

MapView始终显示在Mapactivity中。您正在使用活动。尝试使用MapActivity和google api emulator。
将它放在应用程序标签中

更改项目构建目标,它可能是标准的android平台
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;
 }
}
相反,您应该选择GoogleAPI项目

确保你的钥匙工作正常。先用谷歌的例子试试吧 你必须把所有的信件都转寄出去。
我没有看到任何关于将包含com.google.android.gms.maps和其他类的库放在类路径上的内容。

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

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

在这个项目中,谷歌地图将不可用。如果我想使用地图,我需要将我的目标更改为GoogleAPI4.2


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

您使用的是Eclipse还是其他IDE?你的目标是安卓还是谷歌api?是的,我使用的是eclipse,我的目标是你在说什么。当你想让一个项目有地图时,你必须以谷歌api而不是安卓为目标…@user1833334你使用的是谷歌api v2,对吗?问题是当我使用MapActivity时,它显示它不可用。我指的是这个我用的是ubuntu,我指的是我在做什么says@user1833334类似的过程,@anshika comment不是给你的,我在这里写的,是给user1833334的,因为他说这表明这个软件包不可用
import com.google.android.gms.maps.CameraUpdateFactory;
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;
 }
}
@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();
}