I'm working on an Android app with geolocation and it just doesn't get my location. Even though the location works, the problem is when I try to pass it to the architectView. I tried many location strategies, including the one from the documentation, but with no success. I always get this error:
I/ArchitectView: setLocation: LocationService is not initialized.
I need to mention that the samples work properly without this error.
Also, I read the solutions for the similar questions but nothing worked, so if you see something wrong with my code or have any ideea, please let me know.
Thanks a lot!
Here is my code:
public class ExploreTEST extends AppCompatActivity {
//region ArActivity variables and configs
/**
* The ArchitectView is the core of the AR functionality, it is the main
* interface to the Wikitude SDK.
* The ArchitectView has its own lifecycle which is very similar to the
* Activity lifecycle.
* To ensure that the ArchitectView is functioning properly the following
* methods have to be called:
* - onCreate(ArchitectStartupConfiguration)
* - onPostCreate()
* - onResume()
* - onPause()
* - onDestroy()
* Those methods are preferably called in the corresponding Activity lifecycle callbacks.
*/
protected ArchitectView architectView;
//endregion
private LocationProvider locationProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_explore_test);
architectView = (ArchitectView)this.findViewById( R.id.architectView );
final ArchitectStartupConfiguration config = new ArchitectStartupConfiguration();
config.setFeatures(ArchitectStartupConfiguration.Features.Geo);
config.setLicenseKey( getString(R.string.wikitude_licence_key) );
architectView.onCreate( config );
locationProvider = new LocationProvider(this, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (location!=null && ExploreTEST.this.architectView != null ) {
// check if location has altitude at certain accuracy level & call right architect method (the one with altitude information)
if ( location.hasAltitude() && location.hasAccuracy() && location.getAccuracy()<7) {
ExploreTEST.this.architectView.setLocation( location.getLatitude(), location.getLongitude(), location.getAltitude(), location.getAccuracy() );
} else {
ExploreTEST.this.architectView.setLocation( location.getLatitude(), location.getLongitude(), location.hasAccuracy() ? location.getAccuracy() : 1000 );
}
Toast.makeText(ExploreTEST.this,"Location: "+location.getLatitude()+" "+location.getLongitude()+" "+location.getAltitude()+" "+location.getAccuracy(),Toast.LENGTH_LONG).show();
}
}
@Override public void onStatusChanged(String s, int i, Bundle bundle) {}
@Override public void onProviderEnabled(String s) {}
@Override public void onProviderDisabled(String s) {}
});
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
architectView.onPostCreate();
try {
architectView.load( "ARexplore/index.html" );
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
// start location updates
locationProvider.onResume(this);
architectView.onResume();
}
@Override
protected void onPause() {
super.onPause();
architectView.onPause();
// stop location updates
locationProvider.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
architectView.onDestroy();
}
}
public class LocationProvider {
/** location listener called on each location update */
private final LocationListener locationListener;
/** system's locationManager allowing access to GPS / Network position */
private final LocationManager locationManager;
/** location updates should fire approximately every second */
private static final int LOCATION_UPDATE_MIN_TIME_GPS = 1000;
/** location updates should fire, even if last signal is same than current one (0m distance to last location is OK) */
private static final int LOCATION_UPDATE_DISTANCE_GPS = 0;
/** location updates should fire approximately every second */
private static final int LOCATION_UPDATE_MIN_TIME_NW = 1000;
/** location updates should fire, even if last signal is same than current one (0m distance to last location is OK) */
private static final int LOCATION_UPDATE_DISTANCE_NW = 0;
/** to faster access location, even use 10 minute old locations on start-up */
private static final int LOCATION_OUTDATED_WHEN_OLDER_MS = 1000 * 60 * 10;
/** is gpsProvider and networkProvider enabled in system settings */
private boolean gpsProviderEnabled, networkProviderEnabled;
public LocationProvider(@NonNull final Context context,
@NonNull final LocationListener locationListener) {
super();
this.locationListener = locationListener;
locationManager = (LocationManager)context.getSystemService( Context.LOCATION_SERVICE );
if (this.locationManager != null) {
gpsProviderEnabled = this.locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER );
networkProviderEnabled = this.locationManager.isProviderEnabled( LocationManager.NETWORK_PROVIDER );
}
}
public void onPause() {
if (this.locationListener != null && this.locationManager != null && (this.gpsProviderEnabled || this.networkProviderEnabled)) {
this.locationManager.removeUpdates(this.locationListener);
}
}
public void onResume(final Context context) {
if (this.locationManager != null && this.locationListener != null) {
// check which providers are available
this.gpsProviderEnabled = this.locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
this.networkProviderEnabled = this.locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
/** is GPS provider enabled? */
if (this.gpsProviderEnabled) {
if (context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && context.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
final Location lastKnownGPSLocation = this.locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if ( lastKnownGPSLocation != null && lastKnownGPSLocation.getTime() > System.currentTimeMillis() - LOCATION_OUTDATED_WHEN_OLDER_MS ) {
locationListener.onLocationChanged( lastKnownGPSLocation );
}
if (locationManager.getProvider(LocationManager.GPS_PROVIDER)!=null) {
this.locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, LOCATION_UPDATE_MIN_TIME_GPS, LOCATION_UPDATE_DISTANCE_GPS, this.locationListener );
}
}
/** is Network / WiFi positioning provider available? */
if ( this.networkProviderEnabled ) {
final Location lastKnownNWLocation = this.locationManager.getLastKnownLocation( LocationManager.NETWORK_PROVIDER );
if ( lastKnownNWLocation != null && lastKnownNWLocation.getTime() > System.currentTimeMillis() - LOCATION_OUTDATED_WHEN_OLDER_MS ) {
locationListener.onLocationChanged( lastKnownNWLocation );
}
if (locationManager.getProvider(LocationManager.NETWORK_PROVIDER)!=null) {
this.locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, LOCATION_UPDATE_MIN_TIME_NW, LOCATION_UPDATE_DISTANCE_NW, this.locationListener );
}
}
}
}
}
Carmen Boglis
Hi,
I'm working on an Android app with geolocation and it just doesn't get my location. Even though the location works, the problem is when I try to pass it to the architectView. I tried many location strategies, including the one from the documentation, but with no success. I always get this error:
I/ArchitectView: setLocation: LocationService is not initialized.
I need to mention that the samples work properly without this error.
Also, I read the solutions for the similar questions but nothing worked, so if you see something wrong with my code or have any ideea, please let me know.
Thanks a lot!
Here is my code: