0

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?

CJR
  • 3,174
  • 6
  • 34
  • 78
  • emulator don't work like phone. they don't have GPS or network though u can mimic the geo coordinates with some tools and settings.[follow this link](http://stackoverflow.com/questions/2279647/how-to-emulate-gps-location-in-the-android-emulator) – Pavneet_Singh Jul 22 '16 at 17:48

2 Answers2

2

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!!

Community
  • 1
  • 1
Eenvincible
  • 5,641
  • 2
  • 27
  • 46
  • Thanks for your answer, I just have a quick question. When you say Google Play Services, is it the one called "Google Play Game Services" in the google developer console? (sorry novice at this) – CJR Jul 22 '16 at 17:45
  • `FuesdLocationApi` also use `gps` for accurate location and network for `coarse_location` so with gps, the location almost same as `FusedLocation` with `high_accuracy` flag which internally use gps – Pavneet_Singh Jul 22 '16 at 17:55
  • @Eenvincible under dependencies? "build.grade(Module: app)" – CJR Jul 22 '16 at 18:11
  • Yes, dependencies, sorry I forgot to add that! If you need further help, let me know I can chat – Eenvincible Jul 22 '16 at 18:11
  • @Eenvincible Hi can I start a chat with you? I am trying to implement your solution, but I can't seem to make it work. – CJR Aug 05 '16 at 16:14
  • Yeah just start a chat – Eenvincible Aug 05 '16 at 18:02
  • @Eenvincible I cant start a chat, doesnt have enough points. Please can you start one? – CJR Aug 09 '16 at 17:07
  • I think chats are automatically recommended by the system once comments are too many – Eenvincible Aug 09 '16 at 19:36
0

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.

Location settings in Emulator

Naveen
  • 830
  • 9
  • 19