-1

When trying to get current location to say on Google Maps the application displays white and then crashes. The error with the line at the bottom says "call requires permission which may be rejected by user". I'm using an Android 7.0 device. Everything worked previous to adding the final line.

Here is my MapsActivity.java:

package com.example.thomas.demomaps;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
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;

public class MapsActivity extends FragmentActivity implements             
OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
     // Obtain the SupportMapFragment and get notified when the map is ready 
     to be used.
     SupportMapFragment mapFragment = (SupportMapFragment) 
     getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    }


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Bristol, UK.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Bristol for Bristol City FC and move the camera
    LatLng Bristol = new LatLng(51.439922, -2.620950);
    mMap.addMarker(new MarkerOptions().position(Bristol).title("Bristol City 
    FC, Ashton Gate Stadium"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(Bristol));


    mMap.setMyLocationEnabled(true);
   }

}

And here is my Manifest:

  package="com.example.thomas.demomaps">

  <!--
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but you must specify either coarse or fine
     location permissions for the 'MyLocation' functionality. 
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <!--
         The API key for Google Maps-based APIs is defined as a string 
   resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign 
   the APK.
         You need a different API key for each encryption key, including the 
   release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in 
   src/debug/ and src/release/. 
    -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Thanks!

mehul chauhan
  • 1,792
  • 11
  • 26
brworbriz
  • 1
  • 1
  • 1
    `ACCESS_FINE_LOCATION` is runtime permission . have asked for runtime permission ? – ADM Jan 11 '18 at 13:02

1 Answers1

1

Since android 6 (API level 23), ACCESS_FINE_LOCATION is a runtime permission and You need to request runtime permission, check the android documentation on how to do that https://developer.android.com/training/permissions/requesting.html

Kuti Gbolahan
  • 2,379
  • 1
  • 9
  • 17
  • I don't understand the example as it is for a camera, nor do I understand which part of the file to add the code for location permissions to? – brworbriz Jan 11 '18 at 14:05
  • Take a look at this answer then https://stackoverflow.com/questions/40142331/how-to-request-location-permission-at-runtime-on-android-6 – Kuti Gbolahan Jan 11 '18 at 19:25