The last know location could be null. So you need a further check for this, then ask for a quick update:

   if(location == null){
            String provider = locationManager.getBestProvider(criteria, true);
            if (provider != null){
                locationManager.requestLocationUpdates(provider, 0, 0, singeUpdateListener, context.getMainLooper()); 
            }else {
                Log.e("TAG", No location set");             
            }
    }

class variable:

protected LocationListener singeUpdateListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            String lati = Double.toString(location.getLatitude());
            String longi = Double.toString(location.getLongitude());
            locationManager.removeUpdates(singeUpdateListener);
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {}
        public void onProviderEnabled(String provider) {}
        public void onProviderDisabled(String provider) {}
      };
Answer from Blundell on Stack Exchange
Top answer
1 of 2
1

There is one other way to achieve it using java only.

You can use selenium+PhantomJS to hit "https://mycurrentlocation.net/". This website by default will load you current location and you can read the data on this website using Selenium API.

PhantomJS is an invisible browser, it can open any website in invisible mode and read data from the same.

Hope this helps.

2 of 2
1

There is not an API for location within Google+. You can use the Google Maps Places API to get information of interest located close to a user. Before you can make API queries to the Google Maps Places API, you will need geolocation data (latitude and longitude) to make your queries.

For getting GPS data from the user's laptop, there are a number of approaches, however, I personally prefer to use a location database and perform lookups on the user's IP. The GeoLite databases from Maxmind are really easy to use: you get the user's IP address and then query the database for the location (specificity will vary based on the database used) and the location data will include anything you need for mapping purposes. This stack overflow question on geolocation includes a link to some example code that should get you bootstrapped.

You may also want to consider the geolocation API for modern browsers if you're using a web integration on the desktop.

Once you have lat/long coordinates, you can get location data using the Places API by passing in the coordinates to Place search APIs.

If you want to get location data from Android, you should use a location provider to set up location status updates. The Android documentation does a better job explaining the steps than I can here.

To transmit the data to your computer, you can use bluetooth.

🌐
Javapapers
javapapers.com › android › get-current-location-in-android
Get Current Location in Android - Javapapers
First you need to display the map fragment (https://javapapers.com/android/show-map-in-android/), then you need to tile these coordinates on top of it by using location service / activity.
🌐
YouTube
youtube.com › watch
Show Current Location on Google Map in Android Studio using Java | Part 2 - YouTube
Welcome to Android Knowledge!In this video, I have share how to show current location on google maps. It will access users current gps and accordingly will s...
Published   January 18, 2023
🌐
Stack Overflow
stackoverflow.com › questions › 28939634 › how-can-i-getcurrent-location-and-display-also-city-name
java - how can i getCurrent location and display also city name - Stack Overflow
March 9, 2015 - */ @Override protected void onPostExecute(String address) { // Display the current address in the UI addressLabel.setText(address); } @Override protected String doInBackground(Location... params) { Geocoder geocoder = new Geocoder(mContext, Locale.getDefault()); // Get the current location from the input parameter list Location loc = params[0]; // Create a list to contain the result address List<Address> addresses = null; try { addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); } catch (IOException e1) { Log.e("LocationSampleActivity", "IO Exception in getFromLocat
🌐
Google
developers.google.com › google maps platform › android › maps sdk for android › select current place and show details on a map
Select Current Place and Show Details on a Map | Maps SDK for Android | Google for Developers
The app requests location permission, retrieves the device's location, and presents a list of likely places for the user to choose from. Upon selection, a marker is added to the map at the chosen location, displaying its name and address, and the map's camera focuses on that place. Code samples are provided in both Java and Kotlin, covering initialization, permission handling, location retrieval, place selection, and UI updates.
Find elsewhere
🌐
GitHub
github.com › Pritish-git › get-Current-Location › blob › main › MainActivity.java
get-Current-Location/MainActivity.java at main · Pritish-git/get-Current-Location
Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(getApplicationContext()) · .checkLocationSettings(builder.build()); ... Toast.makeText(MainActivity.this, "GPS is already tured on", Toast.LENGTH_SHORT).show();
Author   Pritish-git
🌐
Wordpress
simpledevcode.wordpress.com › 2016 › 09 › 26 › how-to-obtain-current-location-with-android-java
How to obtain current location with Android (Java) – Bits and Pieces of Code
September 27, 2016 - Suppose in your app you would like to obtain the current location of the device. In other words, obtaining stuff like latitude, longitude, and city. Its very simple to do so with Android. ... import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.location.LocationManager; ... LocationManager localizer = (LocationManager) getSystemService(Context.LOCATION_SERVICE); List<String> providers = localizer.getProviders(true); Location bestPosition = null; //poll for the best, most accurate location for(String s:providers) { Location temp = localizer.getLastKnownLocation(s); if(temp == null) { continue; } if(bestPosition == null || temp.getAccuracy() < bestPosition.getAccuracy()) { bestPosition = temp; } } //get the lat/long double latitude = bestPosition.getLatitude(); double longitude = bestPosition.getLongitude();
🌐
Medium
medium.com › @grudransh1 › best-way-to-get-users-location-in-android-app-using-location-listener-from-java-in-android-studio-77882f8b87fd
Best way to get user’s location in android app using Location Listener from JAVA in android studio | by Rudransh Gupta | Medium
May 17, 2020 - Thus, using the combination of getLastKnownLocation and Location Listener is the best way to achieve user’s current location. ... Above is the Location Listener class that needs to be added in activity.java file, inside the parent class and ...
🌐
Stack Overflow
stackoverflow.com › questions › 39819034 › how-do-i-add-current-location-in-java-code
How do i add current location in java code - Stack Overflow
this question is too broad. you will need to use some other piece of code that already knows your location somehow.. a library that performs a lookup based on ip address, a driver that interacts w/ an actual GPS device, etc. ... I am trying to build this on mac. ... If you want to find location based on the ip address this link will help you. https://www.mkyong.com/java/java-find-location-using-ip-address/
🌐
DigitalOcean
digitalocean.com › community › tutorials › android-location-api-tracking-gps
Android Location API to track your current location | DigitalOcean
August 3, 2022 - requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener) method of the LocationManager class is used to register the current activity to be notified periodically by the named provider. onLocationChanged is invoked periodically based upon the minTime and minDistance, whichever comes first. Location class hosts the latitude and longitude. To get the current location the following code snippet is used.
🌐
W3Docs
w3docs.com › java
How to get current location in Android
To get the current location in Android, you can use the LocationManager class and the LocationProvider interface.
🌐
Blogger
javadevelopersguide.blogspot.com › 2019 › 07 › how-to-get-current-location-latitude.html
Java Developers Guide: How to get the current location latitude and longitude in java script ?
July 1, 2019 - As we know java script is a high-level programming language, it gives many libraries to implement in our program to achieve real time necessities. Here we have used Navigator and Geolocation in our program. We have used the Navigator.geolocation , its a read-only property returns a Geolocation object that gives Web content access to the location of the device. ... <!DOCTYPE html> <html> <body> <title>Get Current Location Sample</title> <h4>Click the below button to get your current location coordinates.</h4> <button onclick="getLocation()">Get My Current Location</button> <div id="displayId"><
🌐
Baeldung
baeldung.com › home › networking › geolocation by ip in java
Geolocation by IP in Java | Baeldung
January 8, 2024 - You will see the current public IP address for your connection loaded into the text box: Note that both GeoIP2 and ipify support IPv4 addresses as well as IPv6 addresses. When you submit the form, you’ll see the JSON response text, including the city, latitude, and longitude corresponding to your public IP Address, and below that, you’ll see a Google Map pointing to your location: In this tutorial, we reviewed the usage of the MaxMind GeoIP2 Java API and free MaxMind GeoLite2 City database using a JUnit test.
🌐
Stack Overflow
stackoverflow.com › questions › 16177566 › how-to-get-latitude-and-longitude-of-my-current-location-using-java
geolocation - how to get latitude and longitude of my current location using JAVA - Stack Overflow
I want to get latitude and longitude using Java. I need some JAVA API , which can be used to get current latitude and longitude. There is option in Google Geo-location API in which we can get latit...