Get Location without using GPS and without Google Services via official API
I wonder if it's possible to get a location without Google Services and without using GPS (and thus using NETWORK_PROVIDER) on an Android device. My current code looks like this:
public void onButtonTap(View view) {
LocationListener locationListener = new LocationListener()
public void onLocationChanged(Location location)
// we're done here
printLocation(location);
public void onStatusChanged(String provider, int status, Bundle extras)
public void onProviderEnabled(String provider)
public void onProviderDisabled(String provider)
;
String provider = LocationManager.NETWORK_PROVIDER;
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
Location location = null;
try
location = locationManager.getLastKnownLocation(provider);
catch (SecurityException se)
if(location != null)
// we're done here
printLocation(location);
if(location == null)
try
locationManager.requestSingleUpdate(provider, locationListener, null);
catch (SecurityException se)
// end of onButtonTab (formatting issues)
private void printLocation(Location location)
String latitude = String.format(Locale.US, "%.4f", location.getLatitude());
String longitude = String.format(Locale.US, "%.4f", location.getLongitude());
String slocation = "lat: " + latitude + ", lon: " + longitude;
Toast.makeText(getApplicationContext(), slocation, Toast.LENGTH_LONG).show();
So I basically query for the last location and if there's none I request an update. This works for PASSIVE_PROVIDER (if there's GPS activity) and of course for GPS_PROVIDER but I don't get a location with NETWORK_PROVIDER. The listeners method onLocationChanged never gets called. The app has the right permissions and locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) is true.
Is this functionality just not working because there's no database (owned by Google and provided by Google Services) to compare against? Or does AOSP build it's own database over time (learning from GPS Data)? I doubt the later.
Little extra question: I can't use Fused Location Provider API as I don't have Google Services installed, can I?
add a comment |
I wonder if it's possible to get a location without Google Services and without using GPS (and thus using NETWORK_PROVIDER) on an Android device. My current code looks like this:
public void onButtonTap(View view) {
LocationListener locationListener = new LocationListener()
public void onLocationChanged(Location location)
// we're done here
printLocation(location);
public void onStatusChanged(String provider, int status, Bundle extras)
public void onProviderEnabled(String provider)
public void onProviderDisabled(String provider)
;
String provider = LocationManager.NETWORK_PROVIDER;
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
Location location = null;
try
location = locationManager.getLastKnownLocation(provider);
catch (SecurityException se)
if(location != null)
// we're done here
printLocation(location);
if(location == null)
try
locationManager.requestSingleUpdate(provider, locationListener, null);
catch (SecurityException se)
// end of onButtonTab (formatting issues)
private void printLocation(Location location)
String latitude = String.format(Locale.US, "%.4f", location.getLatitude());
String longitude = String.format(Locale.US, "%.4f", location.getLongitude());
String slocation = "lat: " + latitude + ", lon: " + longitude;
Toast.makeText(getApplicationContext(), slocation, Toast.LENGTH_LONG).show();
So I basically query for the last location and if there's none I request an update. This works for PASSIVE_PROVIDER (if there's GPS activity) and of course for GPS_PROVIDER but I don't get a location with NETWORK_PROVIDER. The listeners method onLocationChanged never gets called. The app has the right permissions and locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) is true.
Is this functionality just not working because there's no database (owned by Google and provided by Google Services) to compare against? Or does AOSP build it's own database over time (learning from GPS Data)? I doubt the later.
Little extra question: I can't use Fused Location Provider API as I don't have Google Services installed, can I?
Do you have the WiFi radio enabled when you're testing this code?
– Daniel Nugent
Nov 14 '18 at 17:59
Yes Wi-Fi is enabled (connected to a network, seeing >= 3 other networks).
– J. Doe
Nov 14 '18 at 18:01
There might not be Wi-Fi location data available, butNETWORK_PROVIDERalso works based on the cell towers and might need a SIM card and access to the cellular network for that to work. Always creating a newLocationListeneron the button press seems a bit weird, but probably that's not the problem if the object(s) remains in existence. And "The fused location provider is a location API in Google Play services " Google says, so they are tied together.
– Markus Kauppinen
Nov 14 '18 at 18:08
SIM card was inserted. It seems like without Google Services I have to use GPS only.
– J. Doe
Nov 16 '18 at 9:04
add a comment |
I wonder if it's possible to get a location without Google Services and without using GPS (and thus using NETWORK_PROVIDER) on an Android device. My current code looks like this:
public void onButtonTap(View view) {
LocationListener locationListener = new LocationListener()
public void onLocationChanged(Location location)
// we're done here
printLocation(location);
public void onStatusChanged(String provider, int status, Bundle extras)
public void onProviderEnabled(String provider)
public void onProviderDisabled(String provider)
;
String provider = LocationManager.NETWORK_PROVIDER;
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
Location location = null;
try
location = locationManager.getLastKnownLocation(provider);
catch (SecurityException se)
if(location != null)
// we're done here
printLocation(location);
if(location == null)
try
locationManager.requestSingleUpdate(provider, locationListener, null);
catch (SecurityException se)
// end of onButtonTab (formatting issues)
private void printLocation(Location location)
String latitude = String.format(Locale.US, "%.4f", location.getLatitude());
String longitude = String.format(Locale.US, "%.4f", location.getLongitude());
String slocation = "lat: " + latitude + ", lon: " + longitude;
Toast.makeText(getApplicationContext(), slocation, Toast.LENGTH_LONG).show();
So I basically query for the last location and if there's none I request an update. This works for PASSIVE_PROVIDER (if there's GPS activity) and of course for GPS_PROVIDER but I don't get a location with NETWORK_PROVIDER. The listeners method onLocationChanged never gets called. The app has the right permissions and locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) is true.
Is this functionality just not working because there's no database (owned by Google and provided by Google Services) to compare against? Or does AOSP build it's own database over time (learning from GPS Data)? I doubt the later.
Little extra question: I can't use Fused Location Provider API as I don't have Google Services installed, can I?
I wonder if it's possible to get a location without Google Services and without using GPS (and thus using NETWORK_PROVIDER) on an Android device. My current code looks like this:
public void onButtonTap(View view) {
LocationListener locationListener = new LocationListener()
public void onLocationChanged(Location location)
// we're done here
printLocation(location);
public void onStatusChanged(String provider, int status, Bundle extras)
public void onProviderEnabled(String provider)
public void onProviderDisabled(String provider)
;
String provider = LocationManager.NETWORK_PROVIDER;
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
Location location = null;
try
location = locationManager.getLastKnownLocation(provider);
catch (SecurityException se)
if(location != null)
// we're done here
printLocation(location);
if(location == null)
try
locationManager.requestSingleUpdate(provider, locationListener, null);
catch (SecurityException se)
// end of onButtonTab (formatting issues)
private void printLocation(Location location)
String latitude = String.format(Locale.US, "%.4f", location.getLatitude());
String longitude = String.format(Locale.US, "%.4f", location.getLongitude());
String slocation = "lat: " + latitude + ", lon: " + longitude;
Toast.makeText(getApplicationContext(), slocation, Toast.LENGTH_LONG).show();
So I basically query for the last location and if there's none I request an update. This works for PASSIVE_PROVIDER (if there's GPS activity) and of course for GPS_PROVIDER but I don't get a location with NETWORK_PROVIDER. The listeners method onLocationChanged never gets called. The app has the right permissions and locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) is true.
Is this functionality just not working because there's no database (owned by Google and provided by Google Services) to compare against? Or does AOSP build it's own database over time (learning from GPS Data)? I doubt the later.
Little extra question: I can't use Fused Location Provider API as I don't have Google Services installed, can I?
asked Nov 14 '18 at 17:55
J. DoeJ. Doe
184
184
Do you have the WiFi radio enabled when you're testing this code?
– Daniel Nugent
Nov 14 '18 at 17:59
Yes Wi-Fi is enabled (connected to a network, seeing >= 3 other networks).
– J. Doe
Nov 14 '18 at 18:01
There might not be Wi-Fi location data available, butNETWORK_PROVIDERalso works based on the cell towers and might need a SIM card and access to the cellular network for that to work. Always creating a newLocationListeneron the button press seems a bit weird, but probably that's not the problem if the object(s) remains in existence. And "The fused location provider is a location API in Google Play services " Google says, so they are tied together.
– Markus Kauppinen
Nov 14 '18 at 18:08
SIM card was inserted. It seems like without Google Services I have to use GPS only.
– J. Doe
Nov 16 '18 at 9:04
add a comment |
Do you have the WiFi radio enabled when you're testing this code?
– Daniel Nugent
Nov 14 '18 at 17:59
Yes Wi-Fi is enabled (connected to a network, seeing >= 3 other networks).
– J. Doe
Nov 14 '18 at 18:01
There might not be Wi-Fi location data available, butNETWORK_PROVIDERalso works based on the cell towers and might need a SIM card and access to the cellular network for that to work. Always creating a newLocationListeneron the button press seems a bit weird, but probably that's not the problem if the object(s) remains in existence. And "The fused location provider is a location API in Google Play services " Google says, so they are tied together.
– Markus Kauppinen
Nov 14 '18 at 18:08
SIM card was inserted. It seems like without Google Services I have to use GPS only.
– J. Doe
Nov 16 '18 at 9:04
Do you have the WiFi radio enabled when you're testing this code?
– Daniel Nugent
Nov 14 '18 at 17:59
Do you have the WiFi radio enabled when you're testing this code?
– Daniel Nugent
Nov 14 '18 at 17:59
Yes Wi-Fi is enabled (connected to a network, seeing >= 3 other networks).
– J. Doe
Nov 14 '18 at 18:01
Yes Wi-Fi is enabled (connected to a network, seeing >= 3 other networks).
– J. Doe
Nov 14 '18 at 18:01
There might not be Wi-Fi location data available, but
NETWORK_PROVIDER also works based on the cell towers and might need a SIM card and access to the cellular network for that to work. Always creating a new LocationListener on the button press seems a bit weird, but probably that's not the problem if the object(s) remains in existence. And "The fused location provider is a location API in Google Play services " Google says, so they are tied together.– Markus Kauppinen
Nov 14 '18 at 18:08
There might not be Wi-Fi location data available, but
NETWORK_PROVIDER also works based on the cell towers and might need a SIM card and access to the cellular network for that to work. Always creating a new LocationListener on the button press seems a bit weird, but probably that's not the problem if the object(s) remains in existence. And "The fused location provider is a location API in Google Play services " Google says, so they are tied together.– Markus Kauppinen
Nov 14 '18 at 18:08
SIM card was inserted. It seems like without Google Services I have to use GPS only.
– J. Doe
Nov 16 '18 at 9:04
SIM card was inserted. It seems like without Google Services I have to use GPS only.
– J. Doe
Nov 16 '18 at 9:04
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306188%2fget-location-without-using-gps-and-without-google-services-via-official-api%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306188%2fget-location-without-using-gps-and-without-google-services-via-official-api%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you have the WiFi radio enabled when you're testing this code?
– Daniel Nugent
Nov 14 '18 at 17:59
Yes Wi-Fi is enabled (connected to a network, seeing >= 3 other networks).
– J. Doe
Nov 14 '18 at 18:01
There might not be Wi-Fi location data available, but
NETWORK_PROVIDERalso works based on the cell towers and might need a SIM card and access to the cellular network for that to work. Always creating a newLocationListeneron the button press seems a bit weird, but probably that's not the problem if the object(s) remains in existence. And "The fused location provider is a location API in Google Play services " Google says, so they are tied together.– Markus Kauppinen
Nov 14 '18 at 18:08
SIM card was inserted. It seems like without Google Services I have to use GPS only.
– J. Doe
Nov 16 '18 at 9:04