谷歌地图Android V2灰色/空白屏幕应用程序

时间:2015-02-20 16:02:00

标签: android api google-maps

我正在制作一个新应用,但我遇到了一个问题。当我启动应用程序(在我的物理设备和genymotion与谷歌播放服务启用两个测试)应用程序启动正常但屏幕全是灰色,我只能看到位置按钮和放大和缩小。我见过其他人有这个问题,但没有一个答案有帮助。这是我的代码。 Mby犯了一些错误?提前致谢:>

MainActivity.java

package tk.chom.randomstudio.alcofinder;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.os.Bundle;
import com.google.android.gms.maps.GoogleMap;
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 MainActivity extends ActionBarActivity {

// Constant for defining latitude and longitude
static final LatLng myPosition = new LatLng(40 , -79);

// GoogleMap class
private GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // verify we can interact with the Google Map
    try {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().
                    findFragmentById(R.id.map)).getMap();
        }
        // Show a satellite map with roads, MapType is used here
        /* MAP_TYPE_NORMAL: Basic map with roads.
        MAP_TYPE_SATELLITE: Satellite view with roads.
        MAP_TYPE_TERRAIN: Terrain view without roads.
        */
        googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

        // Place dot on current location
        googleMap.setMyLocationEnabled(true);

        // Turns traffic layer on
        googleMap.setTrafficEnabled(true);

        // Enables indoor maps
        googleMap.setIndoorEnabled(true);

        // Turns on 3D buildings
        googleMap.setBuildingsEnabled(true);

        // Show Zoom buttons
        googleMap.getUiSettings().setZoomControlsEnabled(true);

        // Create a marker in the map at a given position with a title
        Marker marker = googleMap.addMarker(new MarkerOptions().
                position(myPosition).title("Siemano, tu jesteś"));

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tk.chom.randomstudio.alcofinder" >
<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"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission
    android:name="tk.chom.randomstudio.alcofinder.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />
<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.gms.version"
        android:value="@integer/google_play_services_version" />
    <activity
        android:name=".SplashScreen"
        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=".MainActivity"
        android:label="@string/app_name"/>

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

2 个答案:

答案 0 :(得分:1)

我认为找到了答案。问题出在奇怪的关键行为上。不得不在我的电脑上再生成几次,然后它工作正常,但是当我改变项目并想要使用相同的键时,它说它被调整或传递不正确(这是正确的顺便说一句)但是修复了一个新的问题。奇怪

答案 1 :(得分:0)

我认为您的地图API_KEY未正确设置。您需要在google maps v2 android上启用Developer api console

请尝试逐步关注this,最后您的手机上会有地图。

对于MainAcitivity`,示例代码:

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"));
    }

有关详细信息,请参阅我的github here