3

I am using following code to display my location. When I test it on a mobile device connected via a USB port, the map is displayed and location shows correctly. But when I build it as an APK and install it on a mobile device, all I get is a blank cream coloured screen. Anyone experienced a similar issue?

Manifest permissions

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

MapsActivity

public class MapsActivity extends FragmentActivity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        OnMapReadyCallback,
        LocationListener {

    public static final String TAG = MapsActivity.class.getSimpleName();
    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
    private GoogleMap mMap;
    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        setUpMapIfNeeded();

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

        // Create the LocationRequest object
        mLocationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(10 * 1000)        // 10 seconds, in milliseconds
                .setFastestInterval(1 * 1000); // 1 second, in milliseconds
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
        mGoogleApiClient.connect();
    }
    @Override
    protected void onPause() {
        super.onPause();

        if (mGoogleApiClient.isConnected()) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
            mGoogleApiClient.disconnect();
        }
    }

    private void setUpMapIfNeeded() {
        if (mMap == null) {
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Default"));
    }

    private void handleNewLocation(Location location) {
        Log.d(TAG, location.toString());

        double currentLatitude = location.getLatitude();
        double currentLongitude = location.getLongitude();

        LatLng latLng = new LatLng(currentLatitude, currentLongitude);

        MarkerOptions options = new MarkerOptions()
                .position(latLng)
                .title("Your Location" + currentLatitude + "  " + currentLongitude);
        mMap.clear();
        mMap.addMarker(options);
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    }

    @Override
    public void onConnected(Bundle bundle) {
        Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (location == null) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        }
        else {
            handleNewLocation(location);
        }
    }

    @Override
    public void onConnectionSuspended(int i) {

    }
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        if (connectionResult.hasResolution()) {
            try {
                connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
            } catch (IntentSender.SendIntentException e) {
                e.printStackTrace();
            }
        } else {
            Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
        }
    }
    @Override
    public void onLocationChanged(Location location) {
        Toast.makeText(MapsActivity.this, "You Moved", Toast.LENGTH_SHORT).show();
        handleNewLocation(location);
    }
}
kar
  • 4,791
  • 12
  • 49
  • 74
  • 1
    This might help: http://stackoverflow.com/questions/30559602/android-studio-google-map-still-blank-on-real-android-device – Daniel Nugent Jan 01 '16 at 03:23
  • No doubt a problem with your API keys when you build in release mode... how are you building it? Where are your API keys defined? – Greg Ennis Jan 01 '16 at 03:25
  • @GregEnnis I am building it as release. API key defined in my google_maps_api.xml file and also at my manifest's meta-data part. – kar Jan 01 '16 at 03:45
  • @DanielNugent My google_maps_api.xml for release was indeed empty as per the suggestion at your answer. But I have updated it now and rebuild and still obtaining same blank screen results. – kar Jan 01 '16 at 03:54
  • @kesh Be sure to uninstall completely before re testing after changing the configuration! – Daniel Nugent Jan 01 '16 at 03:55
  • I had to uninstall. The APK didn't allow me to install otherwise. Am I supposed to obtain 2 separate keys - 1 for debug 1 for release or they can be same? Cos it is same now.Edit I tried with a newly generated key and same results. – kar Jan 01 '16 at 04:01
  • You have to sigened apk for that. – Chirag Savsani Jan 01 '16 at 04:08
  • @ChiragSavsani I am generating a signed APK. – kar Jan 01 '16 at 04:11
  • @ChiragSavsani K I think I see the problem. Seems an additional step missing. I need to go https://developers.google.com/maps/documentation/android-api/signup and get a new key specific for release. Adding now. Hope this is the issue. – kar Jan 01 '16 at 04:14
  • Ok try that and let us tell if any issue. – Chirag Savsani Jan 01 '16 at 04:16
  • You can actually do it either way, one key or two keys. Just be sure that each key has the correct sha1. For using just one key, you would add both the debug and release sha1 fingerprints to the one api key. If it's still not working, check the logs for the error. – Daniel Nugent Jan 01 '16 at 04:55
  • @ChiragSavsani Working now. – kar Jan 01 '16 at 04:55
  • @DanielNugent I been using same jks file for all my builds thus I thought SHA1 fingerprints should be the same across. – kar Jan 01 '16 at 04:59
  • @keshk DId you make signed APK? – Mohit Suthar Jan 01 '16 at 05:00
  • @MohitSuthar Yes, I generated signed APK. – kar Jan 01 '16 at 05:01
  • @keshk After signed apk, did you generate SHA key from your keystore? – Mohit Suthar Jan 01 '16 at 05:04
  • @MohitSuthar By generate SHA key, not sure if you mean this - keytool -list -v -keystore your_keystore_name -alias your_alias_name via command line. I did this, obtained the SHA1 fingerprint and used it to get the API key. The map is working now. – kar Jan 01 '16 at 05:07
  • Ok. so SHA1 is main problem? – Chirag Savsani Jan 01 '16 at 06:22
  • @ChiragSavsani Seems the case. – kar Jan 01 '16 at 23:41

0 Answers0