1

Is this something that is updated and need to be done with different method or something else?

I want to get my current location on Google Maps what am doing right now is as follows.

Creating GoogleMaps Activity, Download GooglePlay Services from Android SDK Manager, Giving Permissions in AndroidManifest for Internet and Location,

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>

Giving permission to use my location in OnMapReady

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

    mMap.setMapType(googleMap.MAP_TYPE_TERRAIN);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    mMap.setMyLocationEnabled(true);
}

When i try to run this code at my device LG Spirit H440 I doesn't get what i expect to get, it should give me the option to get my current location with blue dot on the maps.

I have created also API KEY in the Google API's console.

What am i doing wrong?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ecodor
  • 27
  • 8

1 Answers1

0

Try to use this code

public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;


            locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            locationListener = new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    mMap.clear();
                    LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
                    mMap.addMarker(new MarkerOptions().position(userLocation).title("Your Location"));
                    mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));
                }

                @Override
                public void onStatusChanged(String s, int i, Bundle bundle) {

                }

                @Override
                public void onProviderEnabled(String s) {

                }

                @Override
                public void onProviderDisabled(String s) {

                }
            };

if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
            } else {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
                Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                mMap.clear();
                LatLng userLocation = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
                mMap.addMarker(new MarkerOptions().position(userLocation).title("Your Location"));
                mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));
            }

And onRequestPermissionsResult:

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == 1) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
                }
            }
        }
    }

Also as you created API key I believe you included it in AndroidManifest.

Source Rob Percival / Nick Walter.

  • Thank you for your method, atleast started to ask for the permissions, but this somehow crashes the app. `Unfortunately [app_name] has stopped.` – Ecodor Jan 07 '18 at 09:20
  • Show a logcat please – galagher1901 Jan 07 '18 at 09:22
  • `java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference at com.example.stefan.example.MapsActivity.onMapReady(MapsActivity.java:73)` Line 73 is `LatLng userLocation` – Ecodor Jan 07 '18 at 09:32
  • BTW yes i did include `API KEY` in the AndroidManifest also created couple of others API KEY's for testing but still no success. – Ecodor Jan 07 '18 at 10:54