首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MapFragment无法加载

MapFragment无法加载
EN

Stack Overflow用户
提问于 2016-05-17 17:31:10
回答 1查看 623关注 0票数 0

我只是试图在我的android应用程序中查看地图,但我遇到了一些麻烦。我已经遵循了几个教程,但仍然没有结果。我从我的软件开发工具包管理器下载了google play服务,并正确地导入了google_play_services_lib。因此,我在Google API控制台上使用我的packagename和我的SHA1创建了一个Google Maps Api密钥,这是我在window>preferences>android>build中使用cmd和eclipse找到的,我在两种方式下都得到了相同的SHA1,所以我认为它是正确的。

所以我这样设置我的清单

代码语言:javascript
复制
<?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>

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

代码语言:javascript
复制
    <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" />

因为一个教程,我把android:name改成了class="com.google.android.gms.maps.SupportMapFragment“

然后我的main

代码语言:javascript
复制
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);
}

}

我从教程中获得了main中的代码,并对其进行了一些修改,这样我的应用程序就不会崩溃(如果我不使用这些代码,我的应用程序就会崩溃)。this is what i get我用的是摩托罗拉moto g3手机,所以我试着在手机上从play商店下载aLogcat,但什么也没用到,可能是我用错了地方。this is the tutorial i used the most

有人能帮我在我的应用程序上查看地图吗?我不明白为什么它不工作,我现在正在试着得到Logcat

EN

回答 1

Stack Overflow用户

发布于 2016-05-17 17:36:35

您的地图片段已加载,但地图内容未加载。

你把API钥匙放在哪里了?

代码语言:javascript
复制
<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="my api key" />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37272293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档