java Android:谷歌地图无法显示
我按照一个教程做了所有的事情,地图会显示在教程中,但即使我做了完全相同的事情,它也不会显示在我的地图中
主要活动。Java
package com.example.ankit.mrestro;
import 安卓.app.Dialog;
import 安卓.content.Intent;
import 安卓.net.Uri;
import 安卓.os.Bundle;
import 安卓.support.design.widget.FloatingActionButton;
import 安卓.support.design.widget.Snackbar;
import 安卓.support.v4.app.FragmentActivity;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.support.v7.widget.Toolbar;
import 安卓.view.View;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.Toast;
import com.google.安卓.gms.appindexing.Action;
import com.google.安卓.gms.appindexing.AppIndex;
import com.google.安卓.gms.common.ConnectionResult;
import com.google.安卓.gms.common.GooglePlayServicesUtil;
import com.google.安卓.gms.common.api.GoogleApiClient;
import com.google.安卓.gms.maps.GoogleMap;
import com.google.安卓.gms.maps.MapView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final int GPS_ERRORDIALOGUE_REQUEST = 9001;
GoogleMap mMap;
MapView mMapView;
Button bLogout;
EditText etFName, etLName, etAge, etEmail, etUserName;
UserLocalStorage userLocalStorage;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (servicesOK()){
Toast.makeText(this, "Ready to map", Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_mapview);
mMapView = (MapView) findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
}
setContentView(R.layout.activity_main);
etFName = (EditText) findViewById(R.id.etFName);
etLName = (EditText) findViewById(R.id.etLName);
etAge = (EditText) findViewById(R.id.etAge);
etEmail = (EditText) findViewById(R.id.etEmail);
etUserName = (EditText) findViewById(R.id.etUserName);
bLogout = (Button) findViewById(R.id.bLogout);
bLogout.setOnClickListener(this);
userLocalStorage = new UserLocalStorage(this);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
protected void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
if (authenticate() == true) {
DisplaysUserDetails();
}
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("安卓-app://com.example.ankit.mrestro/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
private boolean authenticate() {
return userLocalStorage.getUserLoggedIn();
}
private void DisplaysUserDetails() {
User user = userLocalStorage.GetLoggedInUser();
etUserName.setText(user.UserName);
etFName.setText(user.FirstName);
etLName.setText(user.LastName);
etAge.setText(user.Age + "");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bLogout:
userLocalStorage.ClearUserData();
userLocalStorage.SetUserLoggedIn(false);
startActivity(new Intent(this, Login.class));
break;
}
}
public boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOGUE_REQUEST);
dialog.show();
}
else {
Toast.makeText(this, "Can't connect to google play services", Toast.LENGTH_SHORT).show();
}
return false;
}
@Override
protected void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction2 = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("安卓-app://com.example.ankit.mrestro/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction2);
client.disconnect();
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
package="com.example.ankit.mrestro">
<uses-sdk
安卓:minSdkVersion="15"
安卓:targetSdkVersion="23"/>
<permission 安卓:name="com.example.ankit.mrestro.permission.MAPS_RECEIVE"
安卓:protectionLevel="signature"/>
<uses-permission 安卓:name="安卓.permission.INTERNET"/>
<uses-permission 安卓:name="安卓.permission.ACCESS_NETWORK_STATE"/>
<uses-permission 安卓:name="安卓.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission 安卓:name="com.google.安卓.providers.gsg.permission.READ_GSERVICES"/>
<uses-permission 安卓:name="安卓.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission 安卓:name="安卓.permission.ACCESS_FINE_LOCATION"/>
<uses-feature 安卓:glEsVersion="0x00020000"
安卓:required="true"/>
<application
安卓:allowBackup="true"
安卓:icon="@mipmap/ic_launcher"
安卓:label="@string/app_name"
安卓:supportsRtl="true"
安卓:theme="@style/AppTheme"
tools:replace="安卓:icon, 安卓:theme, 安卓:label">
<meta-data
安卓:name="com.google.安卓.maps.v2.API_KEY"
安卓:value="<<APIKEY>>"/>
<activity
安卓:name=".Login"
安卓:label="@string/title_activity_login"
安卓:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
安卓:name=".MainActivity"
安卓:label="@string/app_name"
安卓:theme="@style/AppTheme.NoActionBar" />
<activity
安卓:name=".Register"
安卓:label="@string/title_activity_register"
安卓:theme="@style/AppTheme.NoActionBar" />
<activity
安卓:name=".Main2Activity"
安卓:label="@string/title_activity_main2"
安卓:theme="@style/AppTheme.NoActionBar"></activity>
<!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
安卓:name="com.google.安卓.gms.version"
安卓:value="@integer/google_play_services_version" />
</application>
</manifest>
活动\u地图视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:orientation="vertical" 安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<com.google.安卓.gms.maps.MapView
安卓:id="@+id/map"
安卓:layout_height="match_parent"
安卓:layout_width="match_parent"/>
</LinearLayout>
我注意到我没有创建一个单独的按钮来显示地图。这可能是问题所在吗?如果是,我如何为谷歌地图分配按钮?我知道如何创建按钮并为其分配活动,但如何为地图创建按钮?请帮忙
# 1 楼答案
你在哪里拿到地图? 您应该在活动中实现OnMapReadyCallback,然后在onCreate中执行以下操作:
然后覆盖onMapReady调用(类似的内容):
你可以查看我的代码: https://github.com/dmytroKarataiev/EarthquakeSurvival/blob/master/app/src/main/java/com/adkdevelopment/earthquakesurvival/MapviewFragment.java
同样非常重要:不要向任何人展示你的API密钥。这违反了谷歌的规定,对你来说也不安全