2017-11-10 76 views
0

我是Android开发的新手。我试图创建一个非常基本的简单的应用程序来使用谷歌地图功能,但由于某种原因,地图未按预期加载。附上图片显示了它是如何加载的。简单的谷歌地图应用程序不能在模拟器工作

enter image description here

下面是我的google_maps_api.xml。我创建了一个新的Google Maps API密钥并处于活动状态。

<resources> 
    <!-- 
    TODO: Before you run your application, you need a Google Maps API key. 

    To get one, follow this link, follow the directions and press "Create" at the end: 

    https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=9F:9B:0A:4E:CA:F7:7C:D3:83:42:BE:E3:91:18:00:6C:16:D7:B2:96%3Bcom.mycompany.googlemapsdemo 

    You can also add your credentials to an existing key, using these values: 

    Package name: 
    9F:9B:0A:4E:CA:F7:7C:D3:83:42:BE:E3:91:18:00:6C:16:D7:B2:96 

    SHA-1 certificate fingerprint: 
    9F:9B:0A:4E:CA:F7:7C:D3:83:42:BE:E3:91:18:00:6C:16:D7:B2:96 

    Alternatively, follow the directions here: 
    https://developers.google.com/maps/documentation/android/start#get-key 

    Once you have your key (it starts with "AIza"), replace the "google_maps_key" 
    string in this file. 
    --> 
    <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">**mykey**</string> 
</resources> 

下面我们activity_maps.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:map="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/map" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.mycompany.googlemapsdemo.MapsActivity" /> 

和AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.mycompany.googlemapsdemo"> 
    <!-- 
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use 
     Google Maps Android API v2, but you must specify either coarse or fine 
     location permissions for the 'MyLocation' functionality. 
    --> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="com.google.android.providers.gsf.permission. 
    READ_GSERVICES" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <!-- 
      The API key for Google Maps-based APIs is defined as a string resource. 
      (See the file "res/values/google_maps_api.xml"). 
      Note that the API key is linked to the encryption key used to sign the APK. 
      You need a different API key for each encryption key, including the release key that is used to 
      sign the APK for publishing. 
      You can define the keys for the debug and release targets in src/debug/ and src/release/. 
     --> 
     <meta-data 
      android:name="com.google.android.geo.API_KEY" 
      android:value="@string/google_maps_key" /> 

     <activity 
      android:name=".MapsActivity" 
      android:label="@string/title_activity_maps"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

MapsActivity.java

package com.mycompany.googlemapsdemo; 

import android.os.Bundle; 
import android.support.v4.app.BundleCompat; 
import android.support.v4.app.FragmentActivity; 


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.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

    private GoogleMap mMap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
    } 


    /** 
    * Manipulates the map once available. 
    * This callback is triggered when the map is ready to be used. 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, 
    * we just add a marker near Sydney, Australia. 
    * If Google Play services is not installed on the device, the user will be prompted to install 
    * it inside the SupportMapFragment. This method will only be triggered once the user has 
    * installed Google Play services and returned to the app. 
    */ 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

     // Add a marker in Sydney and move the camera 
     LatLng sydney = new LatLng(-34, 151); 
     mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
    } 
} 

我没有看到在logcat中的任何错误。一个有趣的事情是,每当我尝试放大/缩小或向上/向下滚动,应用程序崩溃,但再次没有错误。我使用

的Android 3.0工作室构建 #AI-171.4408382,建立在二零一七年十月二十零日 JRE:1.8.0_152释放-915-B01 AMD64 JVM:OpenJDK的64位服务器VM通过JetBrains的SRO Windows 7的6.1

logcat的消息 enter image description here

更新3: 我不知道是否有任何与下面的消息,我在logcat的

0123看

十一月11日至14日:57:18.471 2816年至2974年/ com.example.mmuniyappa.googlemapdemonew W/DynamiteModule:不找到com.google.android.gms.googlecertificates本地模块描述符类。

11-14 11:57:18.491 2816-2974/com.example.mmuniyappa.googlemapdemonew W/System:ClassLoader引用未知路径:/data/data/com.google.android.gms/app_chimera/m/00000001/N/86

此外,当我试图放大/缩小或向上/向下滚动,仿真器崩溃与弹出消息

模拟器:进程结束,退出代码-1073741819(0000005)

+0

如果应用程序崩溃,我可以保证你有错误。确保你没有吞咽任何异常。当你说它不起作用时,很难猜测。但是在模拟器中运行时要记住的一点是,您必须提供预期的坐标给emualtor。它不会获取主机的PC地址,如果你预期这是默认情况下。根据我们的地图,您的地图可以缩小。尝试在moveCamera方法中添加缩放,例如'mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(yourLocation,13))' – Jimmy

+0

我在三星s7上安装了相同的应用程序,并且按预期工作。 –

回答

0

您的清单文件中缺少版本元数据。

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

我试着将它们添加到AndroidManifest.xml中,但没有运气 –

0

是您确保API密钥与应用程序ID匹配,在https://console.developers.google.com

+0

是的,它是正确的。但我没有限制它 –

+0

这是来自Android Studio的Map示例,或者API键或模拟器的问题有某种问题。 –

+0

地图部分可见,这意味着这与API密钥无关。这可能是一个网络问题,或者您可以尝试其他Google API系统映像的模拟器。 –

0

随着Refrence到Google Issues Tracker

它已得到修复与谷歌API 27图像(REV 3) 。它应该可以解决谷歌地图样本问题..

如果您使用API​​ 27,这是一个已知的问题,地图API将在那里工作。现在尝试使用API​​ API较低的API级别

如果任何问题仍然存在,请在Google issue tracker举报,他们将重新开放审查。

相关问题