Android - 无法显示谷歌地图

时间:2015-12-13 21:09:36

标签: java android xml api google-maps

我需要一些帮助...... 我的应用程序无法显示地图。我已经启用了API密钥并尝试使用新的密钥库多次创建它。 Screenshot for result

错误代码显示:

12-14 03:47:45.527 31641-31956/tkj5a.dhiaulhaq.pnj.project_paksyamsi E/b: Authentication failed on the server.
12-14 03:47:45.527 31641-31956/tkj5a.dhiaulhaq.pnj.project_paksyamsi E/Google Maps Android API: Authorization failure.  Please see 
12-14 03:47:45.533 31641-31956/tkj5a.dhiaulhaq.pnj.project_paksyamsi E/Google Maps Android API: In the Google Developer Console (https://console.developers.google.com)
                                                                                                Ensure that the "Google Maps Android API v2" is enabled.
                                                                                                Ensure that the following Android Key exists:
                                                                                                    API Key: AIzaSyCLdUfhN9SoszJQiGzDF1rcOuV5P2RfcJs
                                                                                                    Android Application (<cert_fingerprint>;<package_name>): 39:AD:91:AB:60:3D:34:22:1C:F3:36:4B:B6:98:6F:BB:61:5A:B5:60;tkj5a.dhiaulhaq.pnj.project_paksyamsi

我的布局:

<?xml version="1.0" encoding="utf-8"?>
<fragment
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsActivity"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"/>

的manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="tkj5a.dhiaulhaq.pnj.project_paksyamsi">
    <permission android:name="tkj5a.dhiaulhaq.pnj.project_paksyamsi.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>

    <uses-permission android:name="tkj5a.dhiaulhaq.pnj.project_paksyamsi.permission.MAPS_RECEIVE"/>
    <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="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="tkj5a.dhiaulhaq.pnj.project_paksyamsi.permission.READ_GSERVICES"/>

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MapsActivity"
            android:theme="@style/AppTheme.NoActionBar"></activity>
        <activity android:name=".CheckInActivity"
            android:theme="@style/AppTheme.NoActionBar"></activity>
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyCLdUfhN9SoszJQiGzDF1rcOuV5P2RfcJs"/>

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

</manifest>

这是MapsActivity.java:

package tkj5a.dhiaulhaq.pnj.project_paksyamsi;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.ArrayList;

public class MapsActivity extends FragmentActivity {

    private GoogleMap map;
    private DBLokasi lokasi;
    private ArrayList<DBLokasi> values;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_maps);

        SupportMapFragment mapFrag=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
        map=mapFrag.getMap();
        map.setMyLocationEnabled(true);

        Bundle b=this.getIntent().getExtras();

        if (b.containsKey("longitude")){
            final LatLng latLng=new LatLng(b.getDouble("latitude"),b.getDouble("longitude"));
            map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,16));
            map.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)));
            map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(Marker marker) {
                    Toast.makeText(MapsActivity.this,"Lokasi Saat Ini " + latLng.latitude+ "," +latLng.longitude,Toast.LENGTH_LONG).show();

                    return false;
                }
            });
        }else if (this.getIntent().getSerializableExtra("lokasi")!=null){
            lokasi=(DBLokasi)this.getIntent().getSerializableExtra("lokasi");
            if (lokasi!=null){
                LatLng latLng=new LatLng(lokasi.getLatD(),lokasi.getLngD());
                map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,16));
                map.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)).title(lokasi.getNama()));

            }

            map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(Marker marker) {
                    final Dialog dialog=new Dialog(MapsActivity.this);
                    dialog.setTitle("Checkin Data : ");
                    dialog.setContentView(R.layout.fragment_dialog_datashow);
                    TextView tvNama=(TextView)dialog.findViewById(R.id.tv_nama);
                    TextView tvKoordinat=(TextView)dialog.findViewById(R.id.tv_koordinat);
                    Button btOK=(Button)dialog.findViewById(R.id.bt_checkin_ok);

                    tvNama.setText(String.format(getResources().getString(R.string.checkin_label_name),marker.getTitle()));
                    tvKoordinat.setText(marker.getPosition().latitude+ ","+ marker.getPosition().longitude);
                    btOK.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.cancel();
                        }
                    });
                    dialog.show();
                    return false;
                }
            });
        }
        else {
            LatLng init;
            DBLokasi lokInit;
            LatLng latLng;

            values=((ArrayList<DBLokasi>)this.getIntent().getSerializableExtra("arraylokasi"));
            lokInit=values.get(0);
            init=new LatLng(lokInit.getLatD(),lokInit.getLngD());
            map.animateCamera(CameraUpdateFactory.newLatLngZoom(init,16));

            for (DBLokasi lok:values){
                latLng=new LatLng(lok.getLatD(),lok.getLngD());
                map.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)));

            }

            map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(Marker marker) {
                    final Dialog dialog=new Dialog(MapsActivity.this);
                    dialog.setTitle("Checkin Data : ");
                    dialog.setContentView(R.layout.fragment_dialog_datashow);
                    TextView tvNama=(TextView)dialog.findViewById(R.id.tv_nama);
                    TextView tvKoordinat=(TextView)dialog.findViewById(R.id.tv_koordinat);
                    Button btOK=(Button)dialog.findViewById(R.id.bt_checkin_ok);

                    tvNama.setText(String.format(getResources().getString(R.string.checkin_label_name)));
                    tvKoordinat.setText(marker.getPosition().latitude+","+marker.getPosition().longitude);
                    btOK.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.cancel();
                        }
                    });
                    dialog.show();
                    return false;
                }
            });
        }
    }
}

感谢任何有帮助的人:)

2 个答案:

答案 0 :(得分:0)

确保您的API密钥是正确的...并且可能会检查您是否拥有Google Play服务&#39;已安装SDK。

它说&#34;在谷歌开发者控制台&#34;所以我会从那里开始lol

检查: -SHA 1键 -API Key

别忘了点击&#39; Save&#39;一旦你完成了,我就知道有些人以前犯了这个错误!

答案 1 :(得分:0)

确认您收到的消息中的SHA-1密钥以及服务器上的密钥是否相同。此外,请确保在清单中以及自动生成的XML文件(发布 - &gt; res - &gt; values - &gt; google_maps_api.xml)中添加API密钥。

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

**google_maps_api.xml**
<string name="google_maps_key" translatable="false" templateMergeStrategy="preserve">
        YOUR_KEY_HERE
    </string>

您可以从已发布的错误消息中获取SHA-1密钥和包名称。两者都用';'

分隔