2016-05-17 76 views
0

我只是想在我的android应用程序中看到一张地图,并且遇到了一些麻烦。 我一直在遵循几个教程,仍然没有结果。 我从我的sdk经理下载了google play服务,并正确导入了google_play_services_lib。 因此,我使用我的软件包名称和我的SHA1创建了一个Google Maps API密钥,我使用cmd和eclipse在窗口> preferences> android> build中找到了SHA1,所以我认为它是相同的SHA1正确。MapFragment将不会加载

所以我把我的表现像这样

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

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

<permission android:name="com.example.googlemapprova.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature"/> 
<uses-permission android:name="com.example.googlemapprova.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" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-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" > 
    <meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="my api key" /> 
    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 
    <activity 
     android:name=".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> 
</application> 

在我的布局文件,我创建只是一个地图片段

<fragment 
    android:id="@+id/map" 
    class="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignTop="@+id/textView1" 
    android:layout_centerHorizontal="true" /> 

我已经改变了安卓与类名=” com.google.android.gms.maps.SupportMapFragment“因为教程

然后我的主要

package com.example.googlemapprova; 

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.widget.Toast; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 

public class MainActivity extends FragmentActivity { 

GoogleMap googleMap; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    try { 
     // Loading map 
     initilizeMap(); 
     initializeUiSettings(); 
     initializeMapLocationSettings(); 
     initializeMapTraffic(); 
     initializeMapType(); 
     initializeMapViewSettings(); 

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

@Override 
protected void onResume() { 
    super.onResume(); 
    // initilizeMap(); 
} 

private void initilizeMap() { 

    googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 

    // check if map is created successfully or not 
    if (googleMap == null) { 
     Toast.makeText(getApplicationContext(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show(); 
    } 
} 

public void initializeUiSettings() { 
    googleMap.getUiSettings().setCompassEnabled(true); 
    googleMap.getUiSettings().setRotateGesturesEnabled(false); 
    googleMap.getUiSettings().setTiltGesturesEnabled(true); 
    googleMap.getUiSettings().setZoomControlsEnabled(true); 
    googleMap.getUiSettings().setMyLocationButtonEnabled(true); 
} 

public void initializeMapLocationSettings() { 
    googleMap.setMyLocationEnabled(true); 
} 

public void initializeMapTraffic() { 
    googleMap.setTrafficEnabled(true); 
} 

public void initializeMapType() { 
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
} 

public void initializeMapViewSettings() { 
    googleMap.setIndoorEnabled(true); 
    googleMap.setBuildingsEnabled(false); 
} 

}

我得到了代码从一个教程我的主要和修改它只是一点点,这样我管理我的应用程序不会崩溃(如果我不使用此代码我的应用将崩溃)。 this is what i get 我正在使用摩托罗拉moto g3手机来试用它 所以我尝试从Play商店下载我的手机上的aLogcat,但是我什么也没有得到,也许我以错误的方式使用它。 this is the tutorial i used the most

有人可以帮我查看我的应用程序上的地图吗?我不明白为什么它不起作用 我试图让Logcat现在

+0

u能阅读和尝试https://developers.google.com/maps/documentation/android-api/start#the_maps_activity_java_file – Raghavendra

+0

尝试检查代码这太问题[17469661](HTTP:/ /stackoverflow.com/questions/17469661/google-mapfragment-not-working-in-android)和[15703245](http://stackoverflow.com/questions/15703245/google-maps-fragment-doesnt-load)如果它可以帮你。另外请确保您已正确引用了Google Play服务库项目。 – KENdi

+0

谢谢大家,我尝试使用android studio而不是eclipse,并且每件事情都很完美。在另一个堆栈溢出问题有人告诉eclipse不能正确读取元数据标签。所以现在我使用的是android studio,它在应用程序节点中运行 – Lucas

回答

0

你的地图片段被加载,但地图内容没有。

您放置API密钥的位置?

<meta-data 
    android:name="com.google.android.maps.v2.API_KEY" 
    android:value="my api key" /> 
+0

,就在活动节点 – Lucas

+0

之前,我想知道是“我的api密钥”故意隐藏您的api密钥,或者您在您的码? –

+0

在我的清单中,我的api密钥是我从google api console – Lucas