Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android google maps not loading the map

I searched through google about this bug and there was a fair amount of people with same problem, but all of their solutions didn't work for me.I would love for you to know that I am not experienced in asking question on this site so if you have any type of feedback on how to improve my question it will be more than welcome.Thanks.

the bug is that google map doesn't load the map and you'r left with a grey screen and the google logo on the bottom left corner.

this is my java file with the map:

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) {
    }

}

and this is my app gradle file:

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'
}

above is my gradle file and here is my Logcat error data:

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
like image 733
Rakan Habab Avatar asked Feb 27 '18 13:02

Rakan Habab


People also ask

Why has Google Maps stopped working on my phone?

Clear cache and data On your Android phone or tablet, open the Settings app. Tap Apps & notifications. Locate the Google Maps app from your list of downloaded apps. After you select the app, storage & cache options should be available.


1 Answers

You need to do 2 things

  1. ENABLE Google Android Maps API
  2. Add code to onMapReady and call the callback in onCreate

How to Enable Google Android Maps API

  • Login to your app in https://console.developers.google.com/
  • Click Enable APIs and Services
  • Click Google Maps Android API
  • Click ENABLE

And secondly add the following code to your 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);
//    }
}
like image 93
Dhaval Jardosh Avatar answered Oct 02 '22 17:10

Dhaval Jardosh