我按照教程做了所有事情,地图显示在教程中,但即使我做了完全相同的事情,它也不会显示在我的内容中。
MainActivity.Java
package com.example.ankit.mrestro;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.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("android-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("android-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:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.ankit.mrestro">
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="23"/>
<permission android:name="com.example.ankit.mrestro.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<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.gsg.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"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:replace="android:icon, android:theme, android:label">
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="<<APIKEY>>"/>
<activity
android:name=".Login"
android:label="@string/title_activity_login"
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=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".Register"
android:label="@string/title_activity_register"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".Main2Activity"
android:label="@string/title_activity_main2"
android: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
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
activity_mapview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</LinearLayout>
我注意到我没有创建单独的按钮来显示地图。这可能是问题吗?如果是这样,我如何为Google地图分配按钮?我知道如何创建按钮并为其分配活动,但我该如何为地图执行此操作?请帮忙。
答案 0 :(得分:0)
你在哪里得到你的地图? 您应该在Activity中实现OnMapReadyCallback,然后在onCreate中执行以下操作:
if (/* play services are OK */) {
mMapView.onCreate(savedInstanceState);
mMapView.getMapAsync(this);
}
然后覆盖onMapReady调用(类似的东西):
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
UiSettings uiSettings = mGoogleMap.getUiSettings();
uiSettings.setZoomControlsEnabled(true);
if (mCameraPosition != null) {
mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(mCameraPosition));
}
addMarkers();
}
同样非常重要:绝不向任何人展示您的API密钥。它违反了Google规则,对您而言也不安全。