简体   繁体   English

Android:Google Maps v2 API在Android 4.0及更高版本上加载空白地图

[英]Android: Google Maps v2 API loading blank map on Android 4.0 and above

I am using Google Maps Android v2 in my Android project. 我在我的Android项目中使用Google Maps Android v2。

I have tested on 3 devices: HTC Vivid (4.0.3), Asus Nexus 7 (4.2.2), and a Galaxy S2 (2.2). 我已经在3种设备上进行了测试:HTC Vivid(4.0.3),Asus Nexus 7(4.2.2)和Galaxy S2(2.2)。 The map only shows up on the Galaxy S2 using the above information, so I know that everything SHOULD work fine. 该地图仅使用上述信息显示在Galaxy S2上,因此我知道一切都应该正常工作。

Question: What do I need to do to add compatibility, or simply fix the errors with the higher API versions? 问题:我需要做些什么来增加兼容性,或者干脆用更高版本的API修复错误?

I have done the following: 我已经完成以下工作:

  • Generated the keystores (both debug and custom) and registered both of their SHA-1 codes with package name to the Google APIs Console. 生成了密钥库(调试和自定义),并将它们的两个SHA-1代码(带有程序包名称)注册到Google API控制台。 I have tried the program with both API keys and both returned the same blank screen and error. 我已经尝试过使用两个API密钥的程序,并且都返回了相同的黑屏和错误。 Using the program out of debug mode shows that attempts were made to access the API in the report logs in the console. 在调试模式下使用程序表明在控制台的报告日志中已尝试访问API。
  • Imported the android-support-v4.jar file 导入了android-support-v4.jar文件
  • Imported the google-play-services_lib project folder, set it as a library, and configured my project to use that library. 导入了google-play-services_lib项目文件夹,将其设置为库,并将我的项目配置为使用该库。
  • Set up my "my_map.xml" layout with the following code 使用以下代码设置我的“ my_map.xml”布局

     <?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.SupportMapFragment" /> 
  • Set up my Activity Class with the following code 使用以下代码设置我的活动类

     import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; import android.os.Bundle; import android.view.Menu; public class MapActivity extends android.support.v4.app.FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_map); GoogleMap map = ((SupportMapFragment)getSupportFragmentManager() .findFragmentById(R.id.map)).getMap(); map.addMarker(new MarkerOptions().position(new LatLng(34.739849,-92.307129)).title("SwagString")); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.map, menu); return true; } } 
  • I have all required permissions 我拥有所有必需的权限
  • I have added the meta-data for the key into my AndroidManifest.xml 我已将密钥的元数据添加到我的AndroidManifest.xml中

  • Permissions: 权限:

     <permission android:name="com.uaex.droughtadvisor.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> <uses-permission android:name="com.uaex.droughtadvisor.permission.MAPS_RECEIVE" /> <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="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-feature android:glEsVersion="0x00020000" android:required="true" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 
  • Meta-Data 元数据

     <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" /> 
  • Upon running the code on the 4.0 and above devices, i get this error: 在4.0及以上版本的设备上运行代码后,出现此错误:

     Failed to load map. Error contacting Google Servers. 

I'm not sure what it was, but I got it working. 我不确定这是什么,但我可以正常使用。 Initially I was targeting 2.2, then I downloaded the other versions in my SDK manager and tried those (just 4.0.3 and 4.2.2, as those were the versions of my test devices). 最初,我的目标是2.2,然后我在SDK管理器中下载了其他版本,并尝试了这些版本(仅4.0.3和4.2.2,因为它们是测试设备的版本)。 I targeted those and installed them on their respective devices and it worked. 我针对这些对象并将它们安装在各自的设备上,并且可以正常工作。 Moving back down to 2.2, they still worked, when originally they didn't. 降回到2.2,他们仍然可以工作,而最初却没有。 No idea what happened here, but I'm fine now. 不知道这里发生了什么,但我现在很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM