I am using Google API V2 in an Android application.
I want to get my current location and add a marker on it.
I am using my friend device. when I run the code, it shows me my-friend's house location. I am really shocked why.
could anyone please help me getting my current location..
Thats because you're first loading the LastKnownLocation, because it's your friends phone it will show your friend last known location (his house).
The code looks fine, I think you have to wait untill it finds the real location.
(make sure you got all permissions, and gps is on)
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
When you call locationManager.getLastKnownLocation(provider) your are retrieving the last location of the GPS that can be like you say when your friend was at home.
If you want to have the location where you are at, you should call requestSingleUpdate for just one time update or requestLocationUpdates for a continuous update.
An example for the one time update with a button listener:
btnupdate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
locationManager.requestSingleUpdate(criteria, new LocationListener() {
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}, getMainLooper());
}
});
This implements a listener for the LocationManager, that gives you the posibility to do some actions depending of the Location status.
In your case, you have implemented the listener on the activity, so just try calling:
locationManager.requestSingleUpdate(criteria, this);
This request just single update to show your present location
I am coming with an answer to my question. I don't know if it will work with all people facing the same problem. I just tried to restart the mobile device. After it restarts, it gets the correct current location. So it was a hardware problem though I thought it is something in the code..
Related
I developed an Android App with Qt. Some time ago I got an E-Mail "Approval for background location" from Play Store. I'm not familiar with Java and I don't understand the problem.
My App uses GPS only if the app is visible (in foreground), but not if it is invisible (background).
My Manifest.xml looks like this:
<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="28"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.android.vending.BILLING"/>
Now I found on Play Store
...the ACCESS_BACKGROUND_LOCATION permission only affects an app's access to location when it runs in the background.
For me this means because I don't use ACCESS_BACKGROUND_LOCATION my App only uses GPS if it is visible (foreground). So what is the problem? Thanks...
I had a very similar problem. I double checked my AndroidManifest.xml, there was no ACCESS_BACKGROUND_LOCATION in it.
I use the QGeoPositionInfoSource from C++ to get periodic location updates. I never called the stopUpdates() function, that had to be fixed. It must be called when the app goes to background, e.g.: onPause.
Here is a very useful video with the clue, and some other useful reading material:
https://youtu.be/xTVeFJZQ28c?t=394
https://developer.android.com/training/location/permissions
https://youtu.be/xTVeFJZQ28c?t=394
https://developer.android.com/about/versions/oreo/background-location-limits
There is a signal QGuiApplication::applicationStateChanged which can be utilized for this, an example code:
if (const auto *const gui =
qobject_cast<const QGuiApplication *const>(qApp)) {
connect(gui, &QGuiApplication::applicationStateChanged, this,
[&](Qt::ApplicationState state) {
switch (state) {
case Qt::ApplicationState::ApplicationActive:
source->startUpdates();
break;
default:
source->stopUpdates();
}
});
}
The source is a pointer for the QGeoPositionInfoSource.
I am trying to get the user's location in my Activity using Google Location API. I have a button, on which if user taps, the location of the user should be retrieved and sent to the app backend.
As per Google's documentation, the
LocationServices.FusedLocationApi
.getLastLocation(apiClient)
method has to be called in onConnected method. However, the method throws error if I am not checking for permission granted by the user.
My problem is I am asking for permission in onClick method of my button.
I tried putting
LocationServices.FusedLocationApi
.getLastLocation(apiClient)
inside the onClick method but it returns a null object.
Is there a proper way to ask location permission from user on tap of a button and not in onCreate method of the activity and still be able to get the location of the user?
I assume you are calling :
mGoogleApiClient.connect();
in onStart(). Which calls the onConnected() method when the activity starts. You can call it on Button Click after asking run-time permission for Location. Then you will be able to getLastLocation() in onConnected() and you won't face permission error.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)){
mGoogleApiClient.connect();
}
else{
// ask run-time permission
}
}
}
});
Hope this helps.
I'm currently testing gps locations in Android(with SDK Tools rev 21.0.1. I tried the standard procedure:
Create an emulator with GPS enabled(tried multiple version, from 2.1 to 4.2)
Start the emulator
telnet to localhost:5554
Type geo fix long lat
In theory, this should set the gps to .
But the following signs indicate the location is not properly set:
1. When I go to maps.google.com or open Google map, it always complains that cannot decide my location and they always ask me to switch on wifi.
2. The GPS never seems to be activated -- no icons on the status bar.
Also, I tried DDMS(which is deprecated and of course I tried Monitor as well). Nothing seems to happen.
I went previous links pointed here:
Location not being updated on new geo fix in android emulator
Android - Unable to get the gps location on the emulator
GPS on emulator doesn't get the geo fix - Android
But all those links do not seem to help. There are some bug report on android project page:
http://code.google.com/p/android/issues/detail?id=13046
but it was a pretty old issue and a final solution wasn't reached.
I'm wondering if anyone else has experienced similar issue and please offer some help. Thanks.
hope You will see icon and it will work.working on mine. APi Level 10
package com.example.locationsddf;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
LocationManager lm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv = (TextView) findViewById(R.id.tv);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onLocationChanged(Location arg0) {
tv.setText("Latitude: "+arg0.getLatitude()+" \nLongitude: "+arg0.getLongitude());
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locationsddf"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.locationsddf.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
When icon is notified then you can just simply get the location by lm.getLastKnowLocationit will work
Sometimes there are issues with AVD itself. Try switching to another CPU architecture. I had similar issues and that was resolved by switching between ARM and Intel. Sometimes AVD with Google services enabled also helped
Also play around with "Allow Mock Locations" in Developers options
As an alternative, you could try this client:
https://github.com/RomanTheLegend/Pokemon_NoGo/tree/master/Sources/PokemonNoGo
I wrote it to play Pokemons, but it works with any GPS-based app. All it does is accept coordinates from desktop client and mocks main system GPS provider - LocationManager.GPS_PROVIDER
Last but not least: in AVD you can replay .kml files. There are many services which could help you generate one. Or just install "Lockito" and generate fake coordinates from Android
I am using NETWORK_PROVIDER to get latitude and longitude of the place.
I'v already check the setting in the "location & security" and enable "use wireless networks". But "isProviderEnabled(LocationManager.NETWORK_PROVIDER)" always return false.
Can anyone help me? Thank you in advance!
Here is my code :
LocationManager locManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean isEnableGPS=locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isEnableNTW=locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Log.d(TAG, isEnableGPS+", "+isEnableNTW);
permission in the AndroidMainfest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Make sure that network based location is enabled in the phone's settings. You can detect this situation and prompt the user to enable it if you really need it.
I believe this is the line of code to launch this activity.
activity.startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
ACTION_LOCATION_SOURCE_SETTINGS
This article explains it http://emobiledude.com/fix-waiting-for-location-in-google-maps-after-android-4-2-update/
I try to use the DDMS location mockup with a very simple Log.i() call in the functions of all LocationListener attributes. When I start the program I get the Log.i() message for onStatusChanged() in my LogCat, so I think that I registered the listener correctly. But when I put a location into the VM using the DDMS menus in Eclipse I get nothing. No change in LogCat, Console, or inside the VM. As if nothing happened at all.
How can I even start to find the problem?
Here is the code for my simple GPS Event logger:
public class gpsActivity extends Activity {
private static final String TAG="gpsActivity";
private static TextView label;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
label = (TextView) findViewById(R.id.label);
LocationManager manager = (LocationManager) this
.getSystemService(LOCATION_SERVICE);
LocationListener listener = new LocationListener() {
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
Log.i(TAG, "onStatusChanged: "+arg0);
}
#Override
public void onProviderEnabled(String arg0) {
Log.i(TAG, "onProviderEnabled: "+arg0);
}
#Override
public void onProviderDisabled(String arg0) {
Log.i(TAG, "onProviderDisabled: "+arg0);
}
#Override
public void onLocationChanged(Location location) {
Log.i(TAG, "onLocationChanged");
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2, 0,
listener);
setContentView(R.layout.main);
}
}
and here the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage.example.gps"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".gpsActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Also as another side note, I am using a German Windows XP and found out that there are problems with GPS packages, because in a German system the number format for 1/2 is not 0.5 but 0,5 which leads to errors in the GPS parser grammar. But also changing my systems number format to English(US) didn't change that I don't get any message, exception or result at all.
Be sure that your device/emmulator is selected in the device menu of DDMS.
EDIT:
Oops. I should have seen these earlier:
You are using NETWORK_PROVIDER. In order to receive mock locations from DDMS (and I think also the command line through geo), you must register the GPS_PROVIDER. Your code works by changing the last line of the onCreate method:
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2, 0,
listener);
NETWORK_PROVIDER is signals through the phone network (e.g 3G) or WIFI
There where 3 problems involved. Because finding all of them took more then 12 hours of pure research time please excuse that I won't even try to find the according sources again.
The problems involved where:
You should use Google API <version> as target and not Android <version> because there are nesssesary things not activated in the latter.
You should send the updates via telnet and not DDMS because DDMS creates some uncertain, undocumented and not logged results, especially if you are using a non-US version of Windows.
The time zone in your emulator is automatically chosen, but maybe to the wrong locale (as mine is Germany, for example). You can switch it off inside a running emulator in the Android settings and also choose your correct locale. This only seems to appear in older versions of Android.
Good luck to everybody who tries his way to this kind of errors!