如何实现谷歌地图 API v2 的新版本

10

大家好,我得知谷歌地图已经废弃了其以前的API v1版本,并推出了新的谷歌地图API v2版本。我按照谷歌提供的链接尝试了一个例子,很确定自己正确地获取了API密钥,并成功编写了一些代码。但在执行代码时出现了错误,请帮我解决这个问题,以下是我的代码。

我还尝试了谷歌Play服务提供的示例代码,但仍然遇到了相同的问题。

下面是我参考谷歌提供的一些链接编写的示例代码:

MainActivity类

package com.example.apv;

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.MarkerOptions;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;

public class MainActivity extends Activity 
{
     @Override  
     protected void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.main);  

     FragmentManager fragmentManager = getFragmentManager();  
     MapFragment mapFragment = (MapFragment)fragmentManager.findFragmentById(R.id.map);  
     GoogleMap googleMap = mapFragment.getMap();  
     LatLng sfLatLng = new LatLng(37.7750, -122.4183);  
     googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);  
     googleMap.addMarker(new MarkerOptions().position(sfLatLng).title("San Francisco")
                .snippet("Population: 776733")
            .icon(BitmapDescriptorFactory.defaultMarker(
     BitmapDescriptorFactory.HUE_AZURE)));
     googleMap.getUiSettings().setCompassEnabled(true);  
     googleMap.getUiSettings().setZoomControlsEnabled(true);  
     googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sfLatLng, 10))

        }  
}  

主要的xml文件

                       <?xml version="1.0" encoding="utf-8"?>  
                <fragment xmlns:android="http://schemas.android.com/apk/res/android"  
                          android:id="@+id/map"  
                              android:layout_width="match_parent"  
                                  android:layout_height="match_parent"  
                               class="com.google.android.gms.maps.MapFragment"/>  

最后是我的清单文件

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

                             <permission  
                     android:name="com.codebybrian.mapsample.permission.MAPS_RECEIVE"  
                                     android:protectionLevel="signature"/>  


                                     <!--Required permissions-->   

            permission oid:name="com.codebybrian.mapsample.permission.MAPS_RECEIVE"/>  

              <!--Used by the API to download map tiles from Google Maps servers: -->  
                 <uses-permission android:name="android.permission.INTERNET"/>  

                   <!--Allows the API to access Google web-based services: -->  
                <uses-permission   
         android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>  

          <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  


                    <!--Optional permissions-->  
          <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>  
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>  

        <!--Version 2 of the Google Maps Android API requires OpenGL ES version 2 -->  
                <uses-feature  
                        android:glEsVersion="0x00020000"    
                          android:required="true"/>  

     application android:label="@string/app_name" android:icon="@drawable/ic_launcher">

                 <activity android:name=".MyMapActivity"  
                         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="AZzaSSsBmhi4dXoKSylGGmjkQ5Jev9UdAJBjk"/>  
                            </application>  


                          </manifest>  

我在版本为4.2、API级别为17的模拟器中运行我的应用程序时,出现了以下错误:

12-17 10:06:52.590: E/Trace(826): error opening trace file: No such file or directory (2)  
12-17 10:06:52.590: W/Trace(826): Unexpected value from nativeGetEnabledTags: 0  
12-17 10:06:52.590: W/Trace(826): Unexpected value from nativeGetEnabledTags: 0  
12-17 10:06:52.590: W/Trace(826): Unexpected value from nativeGetEnabledTags: 0  
12-17 10:06:52.680: I/ActivityThread(826): Pub   com.google.android.gms.plus;com.google.android.gms.plus.action:   com.google.android.gms.plus.provider.PlusProvider  
12-17 10:06:52.740: W/Trace(826): Unexpected value from nativeGetEnabledTags: 0   
12-17 10:06:52.740: W/Trace(826): Unexpected value from nativeGetEnabledTags: 0  
12-17 10:06:52.760: W/Trace(826): Unexpected value from nativeGetEnabledTags: 0 

后来我发现这些版本无法在模拟器中执行,因此我尝试使用两个设备来执行,一个是安卓版本为2.3.7的Sony Xperia U,另一个是安卓版本为4.1.1的Samsung Galaxy Tab,以下是我的输出结果。

   12-17 16:24:48.965: I/dalvikvm(9088): Could not find method     com.example.apv.MainActivity.getFragmentManager, referenced from method com.example.apv.MainActivity.onCreate
  12-17 16:24:48.965: W/dalvikvm(9088): VFY: unable to resolve virtual method 3460:   Lcom/example/apv/MainActivity;.getFragmentManager ()Landroid/app/FragmentManager;
   12-17 16:24:48.965: D/dalvikvm(9088): VFY: replacing opcode 0x6e at 0x0009
   12-17 16:24:48.965: D/dalvikvm(9088): VFY: dead code 0x000c-0065 in Lcom/example /apv/MainActivity;.onCreate (Landroid/os/Bundle;)V
        12-17 16:24:49.066: D/AndroidRuntime(9088): Shutting down VM
    12-17 16:24:49.066: W/dalvikvm(9088): threadid=1: thread exiting with uncaught exception (group=0x4001d578)
      12-17 16:24:49.086: E/AndroidRuntime(9088): FATAL EXCEPTION: main
      12-17 16:24:49.086: E/AndroidRuntime(9088): java.lang.RuntimeException: Unable  to start activity ComponentInfo{com.example.apv/com.example.apv.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
         12-17 16:24:49.086: E/AndroidRuntime(9088):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
              12-17 16:24:49.086: E/AndroidRuntime(9088):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
           12-17 16:24:49.086: E/AndroidRuntime(9088):  at android.app.ActivityThread.access$1500(ActivityThread.java:121)
           12-17 16:24:49.086: E/AndroidRuntime(9088):  at  android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
           12-17 16:24:49.086: E/AndroidRuntime(9088):  at android.os.Handler.dispatchMessage(Handler.java:99)
           12-17 16:24:49.086: E/AndroidRuntime(9088):  at android.os.Looper.loop(Looper.java:138)
          12-17 16:24:49.086: E/AndroidRuntime(9088):   at android.app.ActivityThread.main(ActivityThread.java:3701)
          12-17 16:24:49.086: E/AndroidRuntime(9088):   at java.lang.reflect.Method.invokeNative(Native Method)
         12-17 16:24:49.086: E/AndroidRuntime(9088):    at java.lang.reflect.Method.invoke(Method.java:507)
            12-17 16:24:49.086: E/AndroidRuntime(9088):     at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
           12-17 16:24:49.086: E/AndroidRuntime(9088):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
             12-17 16:24:49.086: E/AndroidRuntime(9088):    at  dalvik.system.NativeStart.main(Native Method)
            12-17 16:24:49.086: E/AndroidRuntime(9088): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
             12-17 16:24:49.086: E/AndroidRuntime(9088):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
              12-17 16:24:49.086: E/AndroidRuntime(9088):   at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
              12-17 16:24:49.086: E/AndroidRuntime(9088):   at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
               12-17 16:24:49.086: E/AndroidRuntime(9088):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
                 12-17 16:24:49.086: E/AndroidRuntime(9088):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
               12-17 16:24:49.086: E/AndroidRuntime(9088):  at android.app.Activity.setContentView(Activity.java:1657)
              12-17 16:24:49.086: E/AndroidRuntime(9088):   at com.example.apv.MainActivity.onCreate(MainActivity.java:20)
               12-17 16:24:49.086: E/AndroidRuntime(9088):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
                 12-17 16:24:49.086: E/AndroidRuntime(9088):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
                   12-17 16:24:49.086: E/AndroidRuntime(9088):  ... 11 more
             12-17 16:24:49.086: E/AndroidRuntime(9088): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.apv-1.apk]
              12-17 16:24:49.086: E/AndroidRuntime(9088):   at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
               12-17 16:24:49.086: E/AndroidRuntime(9088):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
                12-17 16:24:49.086: E/AndroidRuntime(9088):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                  12-17 16:24:49.086: E/AndroidRuntime(9088):   at android.view.LayoutInflater.createView(LayoutInflater.java:471)
                  12-17 16:24:49.086: E/AndroidRuntime(9088):   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
                 12-17 16:24:49.086: E/AndroidRuntime(9088):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
                12-17 16:24:49.086: E/AndroidRuntime(9088):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
                   12-17 16:24:49.086: E/AndroidRuntime(9088):  ... 19 more

请问有人能告诉我如何完成这个任务,并给我一些新版本API v2 Google Maps的教程链接和示例链接吗?谢谢


请正确缩进您的代码以使其更易读。请查看 https://github.com/apacha/maps_demo 以获取一个简约的地图示例。如需更好的示例,请参见 Android SDK\extras\google\google_play_services\samples\maps - Alexander Pacha
6个回答

1

0

我已经完成了那件事,但当我开始运行它时,上面是我的错误。 - bapatla

0

您提供的代码存在错误。您的活动名称为MainActivity,但在清单文件中,您声明的唯一活动的名称为MyMapActivity。此外,您的清单文件没有打开应用标签。

纠正这两个错误后,我运行了您的代码并得到了您提到的异常。我通过添加google-play-services库项目来解决了这个问题。我认为您忘记添加它或者添加不正确。你可以在Google地图v2的开发者文档中找到详细信息。

要检查这一点,请右键单击您的项目,然后转到属性。然后从左侧选择Android。在此页面的底部,您将找到库项目表。确保google-play-services库项目在列表中,并带有绿色标记。如果没有,请通过点击右侧的“添加。”按钮来添加它。

有时,Eclipse无法添加不在您项目的同一工作区中的库项目。如果是您的情况,请将google-play-services和您的项目放在同一工作区中,然后再试一次。


0

看看这个YouTube教程视频。它是为初学者准备的,将帮助您从头开始创建一个简单的Google Maps V2应用程序 youtube链接。至于在Eclipse模拟器上运行Google Maps V2,可以参考这个答案 Running Google Maps v2 on emulator


0

在声明清单文件中,确保将主活动命名为您声明的名称:

Caused by: java.lang.ClassNotFoundException: com.example.apv.MyMapActivity

可能是一个包命名问题吗?请发布你整个的MyMapActivity代码,而不仅仅是onCreate方法。


地图访问权限必须以您的包名称开头。请将<permission android:name="com.codebybrian.mapsample.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>替换为<permission android:name="com.example.apv.permission.MAPS_RECEIVE" android:protectionLevel="signature"/> - Adrián Rodríguez

-5
如果你想要使用片段,那么你的活动即你的MainActivity应该扩展FragmentActivity

不正确。Google 地图的文档明确说明了相反的情况。 - Cameron Lowell Palmer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接