Using Ionic 6, Angular, Capacitor. Fails on a real physical device and the emulator, both. At one point in time, I was able to getCurrentPosition(), but something changed and I no longer can. Created a blank app named ‘deleteme’ and still not able to. Followed a number of tutorials, most recently this one. Either real physical device or emulator, for either one Geolocation.getCurrentPosition() is not firing.
I am choosing to allow location with the permission request.
Phone is only on Wi-Fi, not connected to a cell network.
Please assist.
home.page.ts:
import { Component } from '@angular/core';
import { Geolocation } from '@ionic-native/geolocation/ngx';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
latitude: number;
longitude: number;
constructor(private geolocation: Geolocation) {
this.getLocation();
}
async getLocation() {
console.log('[DEBUG] About to getCurrentPosition()');
const position = await this.geolocation.getCurrentPosition();
this.latitude = position.coords.latitude;
this.longitude = position.coords.longitude;
}
}
home.page.html:
<ion-card>
<ion-card-header>
<ion-card-title>Coordinates</ion-card-title>
</ion-card-header>
<ion-card-content>
<ion-item>Latitude: {{ latitude }}</ion-item>
<ion-item>Longitude: {{ longitude }}</ion-item>
</ion-card-content>
</ion-card>
app.module.ts:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { Geolocation } from '@ionic-native/geolocation/ngx';
@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
providers: [
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
Geolocation,
],
bootstrap: [AppComponent],
})
export class AppModule {}
AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
4 posts - 1 participant