显示Google地图位置

时间:2015-01-13 11:11:23

标签: android

任何人请给我一个简单的谷歌地图项目来显示位置。 我尝试了很多但是混淆了大多数项目。 这是我的项目,但它是强制关闭。请帮我解决问题。

JAVA

import android.app.Activity;
import android.os.Bundle;
import com.google.android.gms.maps.MapFragment;
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 GoogleMap extends Activity {
    static final LatLng TutorialsPoint = new LatLng(21 , 57);
    private static int MAP_TYPE_HYBRID;
       private com.google.android.gms.maps.GoogleMap googleMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_google_map);

        try { 
            if (googleMap == null) {
               googleMap = ((MapFragment) getFragmentManager().
               findFragmentById(R.id.map)).getMap();
            }
         googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
         Marker TP = googleMap.addMarker(new MarkerOptions().
         position(TutorialsPoint).title("TutorialsPoint"));

      } catch (Exception e) {
         e.printStackTrace();
      }
    }


}

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" >

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

</RelativeLayout>

清单

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" >
    </uses-permission>
    <uses-permission android:name="com.example.googlemaps.permission.MAPS_RECEIVE" />
    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />
    <permission
      android:name="com.example.googlemaps.permission.MAPS_RECEIVE"
      android:protectionLevel="signature" />


   <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

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

   <uses-feature
      android:glEsVersion="0x00020000"
      android:required="true" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/Theme.Tab_crescent" >
        <activity
            android:name=".SplashScreen"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
             <activity
            android:name=".GoogleMap"
            android:label="@string/title_activity_google_map" >
        </activity>

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

</manifest>

logcat的

01-13 11:13:27.879: E/AndroidRuntime(1334): FATAL EXCEPTION: main
01-13 11:13:27.879: E/AndroidRuntime(1334): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.paarva.CrescentBuilders/info.paarva.CrescentBuilders.GoogleMap}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
01-13 11:13:27.879: E/AndroidRuntime(1334):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at android.os.Looper.loop(Looper.java:137)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at android.app.ActivityThread.main(ActivityThread.java:5103)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at java.lang.reflect.Method.invokeNative(Native Method)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at java.lang.reflect.Method.invoke(Method.java:525)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at dalvik.system.NativeStart.main(Native Method)
01-13 11:13:27.879: E/AndroidRuntime(1334): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
01-13 11:13:27.879: E/AndroidRuntime(1334):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at android.app.Activity.setContentView(Activity.java:1895)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at info.paarva.CrescentBuilders.GoogleMap.onCreate(GoogleMap.java:18)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at android.app.Activity.performCreate(Activity.java:5133)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-13 11:13:27.879: E/AndroidRuntime(1334):     ... 11 more
01-13 11:13:27.879: E/AndroidRuntime(1334): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 6587000 but found 0.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
01-13 11:13:27.879: E/AndroidRuntime(1334):     at com.google.android.gms.common.GooglePlayServicesUtil.D(Unknown Source)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at com.google.android.gms.maps.internal.x.T(Unknown Source)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at com.google.android.gms.maps.internal.x.S(Unknown Source)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at com.google.android.gms.maps.MapFragment$b.nO(Unknown Source)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at com.google.android.gms.dynamic.a.a(Unknown Source)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at android.app.Activity.onCreateView(Activity.java:4745)
01-13 11:13:27.879: E/AndroidRuntime(1334):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
01-13 11:13:27.879: E/AndroidRuntime(1334):     ... 21 more

1 个答案:

答案 0 :(得分:1)

我建议您在<meta-data>

<application>标记中添加 Google Play服务版作为manifest.xml标记
   <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />