java 谷歌地图不显示安卓
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43332363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google maps not showing Android
提问by kevinh1998
I want to implement google maps in my app. I added a google maps activity and created a key. I didn't change anything in the code elsewhere. I should work but it doesn't.
我想在我的应用程序中实现谷歌地图。我添加了一个谷歌地图活动并创建了一个密钥。我没有更改其他地方的代码中的任何内容。我应该工作,但没有。
MapsActivity
地图活动
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));
}
}
}
activity_maps.xml
活动地图.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.example.kevin.map.MapsActivity" />
回答by Mohammed Mukhtar
Even after you follow steps from Official documentation or you're done with steps from Android Studio, and you're still not able to see the map, follow these steps to ensure whether it is a problem with API key or not:
即使您按照官方文档中的步骤操作或完成 Android Studio 中的步骤后,您仍然无法看到地图,请按照以下步骤确保是否是 API 密钥问题:
- Filter with 'Google Maps'in your logcat.
- You will see an error message, which contains the correct package name & SHA key which should be present as it is in the API Key
- Go to Google Cloud Console -> API -> Credentails, and cross-verify both the above, correct if necessary.
- Wait for 10-15 minutes for the API Key changes to be reflected
- 在您的 logcat 中使用“Google 地图”进行过滤。
- 您将看到一条错误消息,其中包含正确的包名称和 SHA 密钥,该密钥应按 API 密钥中的原样存在
- 转到 Google Cloud Console -> API -> Credentails,并交叉验证上述两项,必要时更正。
- 等待 10-15 分钟以反映 API Key 更改
回答by abielita
In addition to other posts, you may refer with this thread. The reason may be the API key used was created with the wrong keystore. You need to make sure you use your debug keystore when you create an API key in the Google API console. Based from this link, when using GoogleMaps for Android, you need two keys - debug and release. The "debug" key is kind of a misleading term. This key is also to be used when you develop the app. So essentially, use the debug key for development, testing, debugging. When you're ready to launch the app to Market, set the android:debuggable="false" in the AndroidManifest.xml and use the Signed API key.
除了其他帖子,您还可以参考此线程。原因可能是使用的 API 密钥是用错误的密钥库创建的。在 Google API 控制台中创建 API 密钥时,您需要确保使用调试密钥库。基于此链接,当使用 GoogleMaps for Android 时,您需要两个键 - 调试和发布。“调试”键是一种误导性术语。此密钥也将在您开发应用程序时使用。所以本质上,使用调试密钥进行开发、测试、调试。当您准备好向 Market 启动应用程序时,请在 AndroidManifest.xml 中设置 android:debuggable="false" 并使用签名 API 密钥。
回答by Walter Palladino
To work with Google Maps you must:
要使用 Google 地图,您必须:
- Add logic to the Fragment or Activity.
- Add the map fragment element to the layer.
- Obtain an API Key (you probably need to associate the key to your debug version and your signed version of the app, everyone will have a different signature but both can share the same key).
- Configure the application manifest. Here, to use Google Maps you will need: Internet Access, Open GL specifications, if the map will be stored locally you sill need access to the device storage too, as the map libraries are included as part of the google play services you need to configure this too.
- 向 Fragment 或 Activity 添加逻辑。
- 将地图片段元素添加到图层。
- 获取 API 密钥(您可能需要将密钥与您的调试版本和应用程序的签名版本相关联,每个人都有不同的签名,但两者可以共享相同的密钥)。
- 配置应用程序清单。在这里,要使用 Google 地图,您将需要:Internet 访问、Open GL 规范,如果地图将存储在本地,您还需要访问设备存储空间,因为地图库包含在您需要的 google play 服务中也配置这个。
At the manifest level:
在清单级别:
<!-- Location if you will use this -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Maps API needs OpenGL ES 2.0. -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.example.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
At the application level inside the manifest:
在清单内的应用程序级别:
<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="<YOUR MAP API KEY>" />
回答by Pranav Ashok
make sure you have added the map key in the release folder (app>src>release>res>values>google_map_api.xml) too.
确保您也在发布文件夹(app>src>release>res>values>google_map_api.xml)中添加了地图键。
回答by Harsh Singhal
- Check your google API key that you have entered is correct or not and that key is enabled or not
- Did you allow Location access permission, if not then go in the mobile app setting and click on the permission option and allow location access permission and then run your code it will run
- 检查您输入的 google API 密钥是否正确以及该密钥是否已启用
- 您是否允许位置访问权限,如果没有,则进入移动应用程序设置并单击权限选项并允许位置访问权限,然后运行您的代码它将运行