android谷歌地图没有加载地图

问题描述 投票:2回答:1

我通过谷歌搜索了这个错误,并且有相当多的人有同样的问题,但是他们所有的解决方案都不适合我。我很想知道我没有经验在这个网站上提问如果您对如何改进我的问题有任何形式的反馈,那将非常受欢迎。谢谢。

问题是谷歌地图没有加载地图,你左边有一个灰色的屏幕和左下角的谷歌徽标。

这是我的java文件与地图:

package location.hive;

import android.app.Dialog;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.io.IOException;
import java.util.List;


public class DetailActivity extends AppCompatActivity implements OnMapReadyCallback {

    private static final int DIALOG_REQUEST = 9001;

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_detail_with_map);

        String city = getIntent().getStringExtra("city");

        setTitle(getString(R.string.landon_hotel) + ", " + city);

        Hotel hotel = DataProvider.hotelMap.get(city);
        if (hotel == null) {
            Toast.makeText(this, getString(R.string.error_find_hotel) + ": "
                    + city, Toast.LENGTH_SHORT).show();
            return;
        }

        TextView cityText = (TextView) findViewById(R.id.cityText);
        cityText.setText(hotel.getCity());

        TextView neighborhoodText = (TextView) findViewById(R.id.neighborhoodText);
        neighborhoodText.setText(hotel.getNeighborhood());

        TextView descText = (TextView) findViewById(R.id.descriptionText);
        descText.setText(hotel.getDescription() + "\n");

        if (servicesOK() && initMap()) {

            Geocoder gc = new Geocoder(this);
            List<Address> list;
            try {
                list = gc.getFromLocationName(hotel.getAddress(), 1);
                Address address = list.get(0);
                double lat = address.getLatitude();
                double lng = address.getLongitude();
                LatLng latLong = new LatLng(lat, lng);
                CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLong, 5);
                mMap.moveCamera(update);

                MarkerOptions options = new MarkerOptions()
                        .title(getString(R.string.landon_hotel) + ", " + city)
                        .position(new LatLng(lat, lng));
                mMap.addMarker(options);

            } catch (IOException e) {
                Toast.makeText(this, getString(R.string.error_finding_hotel), Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
                Log.d(this.getLocalClassName(), e.getMessage());
            }

        }

    }

    public boolean servicesOK() {
        int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

        if (result == ConnectionResult.SUCCESS) {
            return true;
        } else if (GooglePlayServicesUtil.isUserRecoverableError(result)) {
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(result, this, DIALOG_REQUEST);
            dialog.show();
        } else {
            Toast.makeText(this, getString(R.string.error_connect_to_services), Toast.LENGTH_SHORT).show();
        }
        return false;
    }

    private boolean initMap() {
        if (mMap == null) {
            SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
            mMap = mapFrag.getMap();
        }
        return (mMap != null);
    }

    @Override
    public void onMapReady(GoogleMap map) {
    }

}

这是我的app gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "location.hive"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile 'com.google.android.gms:play-services:7.5.0'
    implementation 'com.android.support:appcompat-v7:22.2.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    testImplementation 'junit:junit:4.12'
}

上面是我的gradle文件,这是我的Logcat错误数据:

combinedSignalIconId=0x7f020649/com.android.systemui:drawable/stat_sys_wifi_signal_4 mobileLabel=mobily wifiLabel="HUAWEI-B315-BF2B" emergencyOnly=false combinedLabel="HUAWEI-B315-BF2B" mAirplaneMode=false mDataActivity=3 mPhoneSignalIconId=0x7f020575/com.android.systemui:drawable/stat_sys_signal_2_auto_rotate mQSPhoneSignalIconId=0x7f02015b/com.android.systemui:drawable/ic_qs_signal_2 mDataDirectionIconId=0x0/(null) mDataSignalIconId=0x7f020575/com.android.systemui:drawable/stat_sys_signal_2_auto_rotate mDataTypeIconId=0x7f0203ef/com.android.systemui:drawable/stat_sys_data_connected_3g mQSDataTypeIconId=0x7f02015d/com.android.systemui:drawable/ic_qs_signal_3g mNoSimIconId=0x0/(null) mWifiIconId=0x7f020649/com.android.systemui:drawable/stat_sys_wifi_signal_4 mQSWifiIconId=0x7f02032b/com.android.systemui:drawable/qs_tile_wifi_signal_4 mWifiActivityIconId=0x7f0205fc/com.android.systemui:drawable/stat_sys_signal_inout mBluetoothTetherIconId=0x7f020625/com.android.systemui:drawable/stat_sys_tether_bluetooth
    02-27 16:29:29.695 1387-1387/? D/StatusBar.NetworkController: refreshNwBoosterIndicator - setNWBoosterIndicators(false)
    02-27 16:29:29.705 1387-1387/? D/StatusBar.NetworkController: refreshNwBoosterIndicator - setNWBoosterIndicators(false)
    02-27 16:29:29.705 1387-1387/? D/StatusBar.NetworkController: refreshNwBoosterIndicator - setNWBoosterIndicators(false)
    02-27 16:29:29.715 1387-1387/? D/StatusBar.NetworkController: refreshNwBoosterIndicator - setNWBoosterIndicators(false)
    02-27 16:29:30.575 3059-3164/? D/com.facebook.appevents.AppEventsLogger: Got unexpected exception: java.io.WriteAbortedException: Read an exception; java.io.NotSerializableException: org.json.JSONObject
    02-27 16:29:30.715 1387-1387/? D/StatusBar.NetworkController: refreshViews connected={ wifi } level=2 combinedSignalIconId=0x7f020649/com.android.systemui:drawable/stat_sys_wifi_signal_4 mobileLabel=mobily wifiLabel="HUAWEI-B315-BF2B" emergencyOnly=false combinedLabel="HUAWEI-B315-BF2B" mAirplaneMode=false mDataActivity=3 mPhoneSignalIconId=0x7f020575/com.android.systemui:drawable/stat_sys_signal_2_auto_rotate mQSPhoneSignalIconId=0x7f02015b/com.android.systemui:drawable/ic_qs_signal_2 mDataDirectionIconId=0x0/(null) mDataSignalIconId=0x7f020575/com.android.systemui:drawable/stat_sys_signal_2_auto_rotate mDataTypeIconId=0x7f0203ef/com.android.systemui:drawable/stat_sys_data_connected_3g mQSDataTypeIconId=0x7f02015d/com.android.systemui:drawable/ic_qs_signal_3g mNoSimIconId=0x0/(null) mWifiIconId=0x7f020649/com.android.systemui:drawable/stat_sys_wifi_signal_4 mQSWifiIconId=0x7f02032b/com.android.systemui:drawable/qs_tile_wifi_signal_4 mWifiActivityIconId=0x7f02060a/com.android.systemui:drawable/stat_sys_signal_out mBluetoothTetherIconId=0x7f020625/com.android.systemui:drawable/stat_sys_tether_bluetooth
    02-27 16:29:30.715 1387-1387/? D/StatusBar.NetworkController: refreshNwBoosterIndicator - setNWBoosterIndicators(false)
    02-27 16:29:30.715 1387-1387/? D/StatusBar.NetworkController: refreshNwBoosterIndicator - setNWBoosterIndicators(false)
    02-27 16:29:30.725 1387-1387/? D/StatusBar.NetworkController: refreshNwBoosterIndicator - setNWBoosterIndicators(false)
    02-27 16:29:30.725 1387-1387/? D/StatusBar.NetworkController: refreshNwBoosterIndicator - setNWBoosterIndicators(false)
    02-27 16:29:31.625 1387-1387/? D/StatusBar.NetworkController: onSignalStrengthsChanged signalStrength=SignalStrength: 6 99 -120 -160 -120 -1 -1 99 2147483647 2147483647 2147483647 -1 2147483647 0x2 gsm|lte level=2
    02-27 16:29:31.625 1387-1387/? D/StatusBar.NetworkController: updateTelephonySignalStrength: hasService=true ss=SignalStrength: 6 99 -120 -160 -120 -1 -1 99 2147483647 2147483647 2147483647 -1 2147483647 0x2 gsm|lte
    02-27 16:29:31.625 1387-1387/? D/StatusBar.NetworkController: updateTelephonySignalStrength: iconLevel=2
    02-27 16:29:31.625 1387-1387/? D/StatusBar.NetworkController: updateTelephonySignalStrength, No signal level. mPhoneSignalIconId = com.android.systemui:drawable/stat_sys_signal_2_auto_rotate mDataSignalIconId = com.android.systemui:drawable/stat_sys_signal_2_auto_rotate mQSPhoneSignalIconId = com.android.systemui:drawable/ic_qs_signal_2 mContentDescriptionPhoneSignal = Phone two bars.
    02-27 16:29:31.735 1387-1387/? D/StatusBar.NetworkController: refreshViews connected={ wifi } level=2 combinedSignalIconId=0x7f020649/com.android.systemui:drawable/stat_sys_wifi_signal_4 mobileLabel=mobily wifiLabel="HUAWEI-B315-BF2B" emergencyOnly=false combinedLabel="HUAWEI-B315-BF2B" mAirplaneMode=false mDataActivity=3 mPhoneSignalIconId=0x7f020575/com.android.systemui:drawable/stat_sys_signal_2_auto_rotate mQSPhoneSignalIconId=0x7f02015b/com.android.systemui:drawable/ic_qs_signal_2 mDataDirectionIconId=0x0/(null) mDataSignalIconId=0x7f020575/com.android.systemui:drawable/stat_sys_signal_2_auto_rotate mDataTypeIconId=0x7f0203ef/com.android.systemui:drawable/stat_sys_data_connected_3g mQSDataTypeIconId=0x7f02015d/com.android.systemui:drawable/ic_qs_signal_3g mNoSimIconId=0x0/(null) mWifiIconId=0x7f020649/com.android.systemui:drawable/stat_sys_wifi_signal_4 mQSWifiIconId=0x7f02032b/com.android.systemui:drawable/qs_tile_wifi_signal_4 mWifiActivityIconId=0x7f0205ff/com.android.systemui:drawable/stat_sys_signal_no_inout mBluetoothTetherIconId=0x7f020625/com.android.systemui:drawable/stat_sys_tether_bluetooth
    02-27 16:29:31.735 1387-1387/? D/StatusBar.NetworkController: refreshNwBoosterIndicator - setNWBoosterIndicators(false)
    02-27 16:29:31.745 1387-1387/? D/StatusBar.NetworkController: refreshNwBoosterIndicator - setNWBoosterIndicators(false)
    02-27 16:29:31.745 1387-1387/? D/StatusBar.NetworkController: refreshNwBoosterIndicator - setNWBoosterIndicators(false)
    02-27 16:29:31.755 1387-1387/? D/StatusBar.NetworkController: refreshNwBoosterIndicator - setNWBoosterIndicators(false)
    02-27 16:29:32.165 904-1727/? D/BatteryService: !@BatteryListener : batteryPropertiesChanged!
    02-27 16:29:32.175 904-1727/? D/BatteryService: level:100, scale:100, status:2, health:2, present:true, voltage: 4387, temperature: 321, technology: Li-ion, AC powered:false, USB powered:true, POGO powered:false, Wireless powered:false, icon:17303564, invalid charger:0, maxChargingCurrent:0
    02-27 16:29:32.175 904-1727/? D/BatteryService: online:4, current avg:450, charge type:1, power sharing:false, high voltage charger:false, capacity:280000, batterySWSelfDischarging:false, current_now:450
    02-27 16:29:32.175 904-904/? D/BatteryService: Sending ACTION_BATTERY_CHANGED.
    02-27 16:29:32.185 904-904/? I/MotionRecognitionService: Plugged
    02-27 16:29:32.185 904-904/? D/MotionRecognitionService:   cableConnection= 1
    02-27 16:29:32.185 904-904/? D/MotionRecognitionService: setPowerConnected | current backoffstate  = 1024 , state =1024
    02-27 16:29:32.185 904-904/? D/MotionRecognitionService: skip setTransmitPower. 
    02-27 16:29:32.195 1387-1387/? D/KeyguardUpdateMonitor: received broadcast android.intent.action.BATTERY_CHANGED
    02-27 16:29:32.195 1387-1387/? I/PERF: received broadcast android.intent.action.BATTERY_CHANGED
    02-27 16:29:32.195 1387-1387/? D/KeyguardUpdateMonitor: handleBatteryUpdate
    02-27 16:29:32.205 1800-1800/? D/CommonServiceConfiguration: getStringValueSetting
    02-27 16:29:32.215 1387-1387/? D/BatteryMeterView: ACTION_BATTERY_CHANGED : level:100 status:2 health:2
    02-27 16:29:32.215 1387-1387/? D/BatteryMeterView: ACTION_BATTERY_CHANGED : level:100 status:2 health:2
    02-27 16:29:32.215 1387-1387/? D/BatteryMeterView: ACTION_BATTERY_CHANGED : level:100 status:2 health:2
    02-27 16:29:32.235 1800-1800/? D/BatteryMonitor: new battery level: 100
android google-maps
1个回答
1
投票

你需要做两件事

  1. 启用Google Android Maps API
  2. 将代码添加到onMapReady并在onCreate中调用回调

如何启用Google Android Maps API

  • https://console.developers.google.com/登录您的应用
  • 点击Enable APIs and Services
  • 点击Google Maps Android API
  • 点击ENABLE

然后将以下代码添加到DetailActivity.java中,

public class DetailActivity extends AppCompatActivity implements OnMapReadyCallback {

    private static final int DIALOG_REQUEST = 9001;

    public GoogleMap mMap;

    public void onMapReady(GoogleMap map) {

        mMap = map;

        if (servicesOK() /*&& initMap()*/) {

            //Geocoder gc = new Geocoder(this);
            //List<Address> list;
            try {
                //list = gc.getFromLocationName(hotel.getAddress(), 1);
                //Address address = list.get(0);

                double lat = 0.6856979;
                double lng = 16.8795434;
                LatLng latLong = new LatLng(lat, lng);
//                CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLong, 5);
                Log.i("BEGINNING","Check this");
                mMap.moveCamera(CameraUpdateFactory.newLatLng(latLong));
                Log.i("Finishing","Check this");

                /*MarkerOptions options = new MarkerOptions()
                        .title(getString(R.string.landon_hotel) + ", " + city)
                        .position(new LatLng(lat, lng));
                mMap.addMarker(options);*/
                //onMapReady(mMap);

            } /*catch (IOException e) {
                Toast.makeText(this, getString(R.string.error_finding_hotel), Toast.LENGTH_SHORT).show();
            }*/ catch (Exception e) {
                Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
                Log.d("Check this->", e.getMessage());
            }



        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_with_map);
        /*
        String city = getIntent().getStringExtra("city");
        setTitle(getString(R.string.landon_hotel) + ", " + city);
        Hotel hotel = DataProvider.hotelMap.get(city);
        if (hotel == null) {
            Toast.makeText(this, getString(R.string.error_find_hotel) + ": "
                    + city, Toast.LENGTH_SHORT).show();
            return;
        }
        TextView cityText = (TextView) findViewById(R.id.cityText);
        cityText.setText(hotel.getCity());
        TextView neighborhoodText = (TextView) findViewById(R.id.neighborhoodText);
        neighborhoodText.setText(hotel.getNeighborhood());
        TextView descText = (TextView) findViewById(R.id.descriptionText);
        descText.setText(hotel.getDescription() + "\n");
        */


        SupportMapFragment mapFrag =
                (SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map);
//            mMap = mapFrag.getMap();
        mapFrag.getMapAsync(this);

    }

    public boolean servicesOK() {
        int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

        if (result == ConnectionResult.SUCCESS) {
            return true;
        } else if (GooglePlayServicesUtil.isUserRecoverableError(result)) {
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(result, this, DIALOG_REQUEST);
            dialog.show();
        } else {
            Toast.makeText(this, getString(R.string.error_connect_to_services), Toast.LENGTH_LONG).show();
        }
        return false;
    }

//    private boolean initMap() {
//        if (mMap == null) {
//
//        }
//        return (mMap != null);
//    }
}
© www.soinside.com 2019 - 2024. All rights reserved.