在我的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>
您需要从 google 控制台为您的项目启用 google 地图 api。并且还需要从控制台下载json文件。
1。转到 google 控制台
2。单击您的项目。
3.单击左侧菜单面板中的库部分。见下图
在 google 地图 api 中,单击 Google 地图 android api
然后启用它。
现在转到firebase控制台
单击您的项目并转到项目设置并下载 json 文件。
在您的应用程序中添加该 json 文件。
我刚刚检查了像你一样的谷歌地图。
只需将你的代码与我的进行比较即可。
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));
}
}
和清单
<?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中创建一个项目
替换 google_maps_api 中的 Key 值
<resources>
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">Your Key</string>
希望这对您有用。
PS:您可以从 android studio 生成谷歌地图活动。
将此权限添加到menifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET" />
或检查谷歌控制台是否已启用API
或检查您的 google_api_key
在
AndroidManifest.xml
文件中的 application
部分包含以下代码:
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
请参阅文档此处。
Google 地图喜欢获取不同的活动生命周期状态。所以你应该发送:
map_view.onCreate(savedInstanceState)
和map_view.onResume()
。它还喜欢 onPause()
、onLowMemory()
、onSaveInstanceState(outState)
、onDestroy()
(如果出现)。
单击“限制密钥”,然后在“API 限制”中->“创建 api 时不限制密钥”
1) 请更新 Google Play 服务
2) 请在网址中附加您的 api 密钥
例如:-“https://maps.googleapis.com/maps/api/directions/”+输出+“?” + 参数 + "&key=" + MY_API_KEY;
确保您已授予这些权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
并检查 AVD 的 WiFi 是否打开以及您的 PC 是否已连接到互联网。
我遇到了同样的问题,按照上述步骤我能够解决我的问题。
如果活动正在打开并且地图不可见,即使您提供了 Google_map_key ,那么对我有用的一个解决方案是我通过复制 google_maps_api.xml 中的链接并粘贴到浏览器中来创建一个新的 Google 地图 API 密钥。选择一个项目或创建一个项目,它会将您带到 google API KEY 已准备好的页面,将其复制并粘贴到说明中给出的位置。它将起作用。
所有编码工作均已完成。检查正确。 那么必须用 APp 做两件事。
1. add SHA -1 Key in Firebase console.
2. Dowanload and add this json file in your code.
THis PC -> "Your project folder" -> app -> "here Put and Update json file"
3. Go to Google cloud Console.
API & Services -> Credentials -> Android key (auto created by Firebase) -> copy "API Key".
4. put this API Key in Manifest file.
☻♥ 完成保留代码。