-1

I have gone through almost every article in SOF, or other websites.But everywhere I got my Location Enabled after click on Radio Button. But I need my location automatically in Google Maps immediately I open my app. Like in demo maps "Sydney" is shown, I need my Location ( no matter where I am)....

enter image description here

I am getting this (IMage) after open my app. If i click on radio button it will take me to my location.. But I want to get my location without on Click Radio button... I almost tried the link given below till now. but didnt get what i need. I am usin gSony Xperia 18API..

Arjun Singh
  • 724
  • 1
  • 8
  • 24
  • Demo app has pre defined latitude and longitude in coding say hard coded. For real time use of location you need to wait until you get location from mobile network or gps. – Shvet Jun 01 '16 at 04:13
  • For target api-22 and lower: http://stackoverflow.com/questions/30253123/blue-dot-and-circle-is-not-shown-on-mylocation-using-android-fused-location-api For target api-23 and higher: http://stackoverflow.com/questions/34582370/how-can-i-use-google-maps-and-locationmanager-to-show-current-location-on-androi – Daniel Nugent Jun 01 '16 at 04:42
  • Still facing issue or resolved ? – ρяσѕρєя K Jun 01 '16 at 08:43
  • Still Facing.. As i gone through every link given below – Arjun Singh Jun 01 '16 at 08:44

2 Answers2

0

You can use the following lines to get your current location:

LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locManager.getLastKnownLocation(locManager.getBestProvider(new Criteria(), false));

Make sure, you have the following permissions in your Manifest

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

Lastly, in the Google Maps Demo App, Sydney is hard-coded. :)

Eric B.
  • 4,622
  • 2
  • 18
  • 33
0

In real life you can never be sure to receive a location. User could have disabled location services so you first check if location services are enabled in onResume() (better than onCreate() since App can be created, sent to background, location services disabled, App back to foreground).

Then you can get LocationManager.getLastKownLocation for a provider BUT this can be null!

A better approach would be to use LocationManagers requestSingleUpdate() or requestLocationUpdates() on a background thread and update your UI once you received a location update.

Again, if the device has just booted and might not be connected to a network and might not be able to receive gps, then getLastKnownLocation() will return null!

Heres what I did to receive my location:

            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestSingleUpdate(new Criteria(), new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    Log.d(Constants.LOG_TAG, "Received single location update");
                    mainThreadHandler.post(new Runnable(){updateUI(location);});
                }

                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {
                    Log.d(Constants.LOG_TAG, "Status changed for single location update");
                }

                @Override
                public void onProviderEnabled(String provider) {
                    Log.d(Constants.LOG_TAG, "Provider enabled for single location update");
                }

                @Override
                public void onProviderDisabled(String provider) {
                    Log.d(Constants.LOG_TAG, "Provider disabled for single location update");
                }
            }, locationHandler.getLooper());
xxtesaxx
  • 6,175
  • 2
  • 31
  • 50