【问题标题】:My emulator won't display a Google map我的模拟器不会显示谷歌地图
【发布时间】:2015-02-18 23:21:54
【问题描述】:

我一直在寻找一整周,但我似乎找不到任何解决方案,所以我在这里订阅:)

我试图显示一个简单的地图开始,但是当我启动我的应用程序时,只有一个空白活动(比白色深一点),左下角写着 Google。

我的模拟器在其他应用程序上运行谷歌地图非常好,所以我认为这可能是 API 密钥的问题,但我刚刚检查过,没问题。

如果有什么问题告诉我,我的 Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.slimane.pinpoint">
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- The following two permissions are not required to use
         Google Maps Android API v2, but are recommended. -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <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">

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />


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


        <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>
        <activity
            android:name=".MainActivity3"
            android:label="@string/title_activity_main_activity2"></activity>
        <activity
            android:name=".MenuOptions"
            android:label="@string/title_activity_menu_options"></activity>
        <activity
            android:name=".MenuAmis"
            android:label="@string/title_activity_menu_amis"></activity>
        <activity
            android:name=".MapEditor_StepOne"
            android:label="@string/title_activity_map_editor__step_one"></activity>
        <activity
            android:name=".MenuEvents"
            android:label="@string/title_activity_menu_events"></activity>

        <activity android:name=".MapsActivity"
            android:label=".maps"></activity>
    </application>

</manifest>

我的地图活动:

package com.example.slimane.pinpoint;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;

import com.google.android.gms.maps.Projection;
import com.google.android.gms.maps.SupportMapFragment;

import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;


public class MapEditor_StepOne extends FragmentActivity {
    private GoogleMap googleMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_editor__step_one);








    }


}

我的相关布局:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

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

</LinearLayout>

我开始掉头发了..

谢谢

【问题讨论】:

    标签: javascript android google-maps


    【解决方案1】:

    我认为您还没有设置Google Map,您的MapAcitivity 中没有任何内容。

    示例代码:

    public class MainActivity extends Activity {
    
        private static LatLng goodLatLng = new LatLng(37, -120);
        private GoogleMap googleMap;
        private EditText et_address, et_finalAddress;
        LatLng addressPos, finalAddressPos;
        Marker addressMarker;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            et_address = (EditText) findViewById(R.id.addressEditText);
            et_finalAddress = (EditText) findViewById(R.id.finalAddressEditText);
    
    
            // Initial Map
            try {
    
                if (googleMap == null) {
                    googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            // Put a dot on my current location
            googleMap.setMyLocationEnabled(true);
            googleMap.setIndoorEnabled(true);
            googleMap.setTrafficEnabled(true);
            // 3D building
            googleMap.setBuildingsEnabled(true);
            // Get zoom button
            googleMap.getUiSettings().setZoomControlsEnabled(true);
    
            Marker marker = googleMap.addMarker(new MarkerOptions()
                    .position(goodLatLng)
                    .title("Hello"));
        }
    

    更多详情请参考我的githubhere

    另外,请尝试一步步关注this,你最终会在手机上得到一张地图。

    【讨论】:

    • 好的,非常感谢,我尝试使用 .getMap(但无一例外),同样,应用程序将关闭。我怎样才能得到异常的结果?同时,我将尝试按照您的分步教程进行操作。
    • 对于exception,您需要参考您的logcat。获取方式可以参考here
    • 好的!我不知道那是 logcat,但现在我知道如何到达那里了,谢谢。我们越来越近了,问题是我怀疑的:从以下行中出现 NullPointerException:codegoogleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); code 所以,在这一点上我真的不知道该怎么做..我已经按照你的所有步骤操作,我的模拟器工作得很好,这也不是 API 密钥问题..我确定它一定是什么真的很愚蠢,但我找不到,因为我猜这个问题已经太久了..
    • 以下哪一行?
    • 你可以试试thisthis。您也可以从我的Github 下载整个项目,并在Android Studio 中运行它。
    猜你喜欢
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 2021-11-14
    相关资源
    最近更新 更多