Problem with the MapsActivity of google maps

0

I'm inserting a geolocation in Android Studio for an app:

private void miUbiacion() {
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            return;
        }
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        actualizarUbicacion(location);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,15000,0,locListener);
    } '

And I do not take the locListener since I have the imported libraries correctly

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

I do not know how to fix it, it does not let me import it or anything just comes out in red, any suggestions?

    
asked by Marcelo 10.12.2017 в 00:21
source

3 answers

0

Good afternoon! To get the location you have to follow three simple steps:

Step 1: Add the following compilation to the build.gradle in the dependencies

  

compile 'com.google.android.gms: play-services-location: 10.2.0'

Step 2: Add the location permission to the manifest.xml

  

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

Step 3: When you ask for the permissions in your function MyLocation (), you have to remove the return and add the line that asks for the permissions

  

ActivityCompat.requestPermissions (this, new   String [] {Manifest.permission.ACCESS_FINE_LOCATION,}, 1000);           }

So it would look like this:

*if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {*
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, 10000);
    *}* 

You can also add this line so that the button that takes you to your location appears

  

Map.setMyLocationEnabled (true);

    
answered by 12.12.2017 / 16:41
source
0

You need to add the Google Play Services dependency to use location services

implementation 'com.google.android.gms:play-services-location:11.4.2'
    
answered by 10.12.2017 в 05:59
0

You have a misunderstanding, locListener is not a class that you import, it is a listener that you have to create, it is the last parameter required by requestLocationUpdates() .

  

requestLocationUpdates (String provider, long minTime, float   minDistance, LocationListener listener) Register for   Location updates using the provider, and a Pending Intent.

What you can do is that your Activity extends from LocationListener , implement the required methods.

public class MainActivity extends Activity implements LocationListener {

and in this way refer to the Activity:

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,15000,0,this);
    
answered by 11.12.2017 в 23:45