简体   繁体   中英

Getting current location (coordinates) in android?

I am trying to get the current location coordinates in longitude and latitude. Here is my code so far:

public class MainActivity extends AppCompatActivity {

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

    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    MyLocationListener myLocationListener = new MyLocationListener();
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocationListener);

}

}

and this class too:

public class MyLocationListener implements LocationListener {
private static final String TAG = "COORDINATES: ";

@Override
public void onLocationChanged(Location location) {
    if(location != null){
        Log.e(TAG, "Latitude: " + location.getLatitude());
        Log.e(TAG, "Longitude: " + location.getLongitude());
    }
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

}

When I run the app in emulator, I don't get any log message with coordinates. Any suggestions?

The best approach is to use the latest FusedLocationApi provided by Google Play Services library.

If you want to use the old approach, that is fine but you might not get very accurate results.

Either way, make sure you have enabled internet permission, either COARSE_LOCATION or FINE_LOCATION or both in your android manifest.

Also, if you have android 6.0, remember you must request runtime permissions or it won't work for you!

I answered a similar question yesterday and you can find here -which works;

There is also a link to a sample code for FusedLocationApi here .

I hope this helps you and good luck!

UPDATE

You can add Google Play Services to your build.gradle like this:

compile 'com.google.android.gms:play-services:9.2.'

But if you are only interested in one service like location, you can be specific:

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

NOTE

I would highly discourage you from getting user location on your UI thread because it will destroy user experience in the long run! Use a separate thread!!

You have to mimic the location in Emulator. You can do that by accessing the Android Device Manager and Select Emulator Control tab and send the locations to Emulator.

模拟器中的位置设置

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM