在我的Android应用程序中,我想要一个MainActivity,谷歌地图占据整个屏幕。
我的问题是谷歌地图没有显示地图,只显示左下角的谷歌徽标:
以下是应显示地图的主要活动的相关部分:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback,
NavigationView.OnNavigationItemSelectedListener {
private GoogleMap mMap;
private FragmentManager mFragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
setupLayout();
mFragmentManager = getSupportFragmentManager();
}
/**
* 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));
}
以下是包含Google地图片段的主要活动内容的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.purringcat.stray.view.activity.MainActivity"/>
这是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.purringcat.stray">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!--
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"/>
<application
android:name=".Stray"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".view.activity.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=".view.activity.LoginActivity"
android:label="@string/title_activity_login"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity android:name=".view.activity.SignUpActivity">
</activity>
<!--
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"/>
</application>
</manifest>
答案 0 :(得分:1)
您需要从谷歌控制台为您的项目启用谷歌地图API。而且您还需要从控制台下载json文件。
<强> 1。转到google console
<强> 2。点击你的项目。
第3。单击左侧菜单面板中的库部分。见下图
在google maps api中,点击 Google maps android api
比启用它。
现在转到firebase console
单击您的项目并转到项目设置并下载json文件。
在您的应用程序中添加该json文件。
答案 1 :(得分:1)
将此权限添加到menifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET" />
或检查谷歌控制台是否启用了API
或检查您的google_api_key
答案 2 :(得分:1)
我只是像你的谷歌地图一样检查过。
只需将您的代码与我的代码进行比较。
MainActivity.java
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));
}
}
和Manifest
<?xml version="1.0" encoding="utf-8"?>
<!--
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" />
<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>
注意:您必须在Google
中创建项目click here to visit the google console
在google_maps_api
中替换您的Key值<resources>
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">Your Key</string>
希望这适合你。
PS:你可以从android studio生成谷歌地图活动。
答案 3 :(得分:0)
在AndroidManifest.xml
部分的application
文件中包含以下代码:
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
请参阅文档here。
答案 4 :(得分:0)
Google地图喜欢获取不同的活动生命周期状态。因此,您应该发送它:map_view.onCreate(savedInstanceState)
和map_view.onResume()
。如果它们发生,它也喜欢onPause()
,onLowMemory()
,onSaveInstanceState(outState)
,onDestroy()
。
答案 5 :(得分:0)
1)请更新Google Play服务
2)请在网址中附加您的api密钥
例如:-“ https://maps.googleapis.com/maps/api/directions/ ” +输出+“?” +参数+“&key =” + MY_API_KEY;
答案 6 :(得分:0)
确保您已提供这些许可
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
并检查AVD的WiFi是否已打开,并且您的PC是否已连接到互联网。
我遇到了同样的问题,按照上面提到的步骤,我能够解决我的问题。
答案 7 :(得分:0)
如果活动正在进行中,即使您提供Google_map_key,地图也不可见,那么对我有用的一种解决方案是,我通过复制google_maps_api.xml中的链接并粘贴来创建了一个新的Google map API KEY。浏览器。选择一个项目或创建一个项目,它将把您登陆到准备好Google API KEY的页面上,将其复制并粘贴到说明中指定的位置。它将起作用。
答案 8 :(得分:0)
点击限制密钥,然后在API限制->创建api时不要限制密钥