Capacitor: Geolocation Permissions error

Created on 30 Oct 2018  路  8Comments  路  Source: ionic-team/capacitor

Capacitor geolocation is giving me the following errors...

To Native -> Geolocation requestPermissions 64714929 鈿★笍 Error calling method requestPermissions on plugin Geolocation: No method found. 鈿★笍 Ensure plugin method exists and uses @objc in its declaration, and has been defined

However I have added the correct permissions to the info.plist as stated in the guide and it was working perfectly until today.. any ideas?

Most helpful comment

I don't think that's right, I think that will use the webview implementation instead of the Capacitor plugin.

Ah, wait, you had import { Geolocation } from '@capacitor/core' and changed it to const { Geolocation } = Plugins? then yeah, that's the right way of using it.

All 8 comments

@jcesarmobile any ideas?

Can you show your code?

`import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { NavController, ToastController, InfiniteScroll, ModalController, Platform } from '@ionic/angular';

// Services
import { MapService } from '../../services/map.service';
import { FirestoreService } from '../../services/firestore.service';
import { UserService } from '../../services/user.service';
import { BloomFilterService } from '../../services/bloom-filter.service';
import { SkillService } from '../../services/skill.service';

// Models
import { Skill } from '../../models/skill';
import { User } from '../../models/user';

// RXJS
import { Observable, forkJoin, of } from 'rxjs';
import { map, take } from 'rxjs/operators';

// Animations
import { cardFade, cardListAnimation, stateFade } from '../../../animations';

// Pages
import { AddSkillPage } from '../add-skill/add-skill.page';
import { FilterPage } from '../filter/filter.page';

// Ionic Native
import { Shake } from '@ionic-native/shake/ngx';

// Capacitor
import { Plugins, Geolocation } from '@capacitor/core';
const { Storage } = Plugins;



@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
  animations: [cardFade, cardListAnimation, stateFade]
})
export class HomePage implements OnInit {

  skills: Skill[] = [];
  userSkills: string[] = [];
  categories: string[] = [];
  user: User;

  skillPaginationcursors: any = {};

  showSkillsInfiniteScroll = true;
  noSkillsFound: boolean;
  suggestionShow = true;
  loaded: boolean;
  skillsLoaded: boolean;
  searching: false;
  filtered: false;

  filterParams = {
    sortBy: { type: 'createdAt', direction: 'desc' },
    filterBy: [],
    distance: null
  };

  states = {
    map: 'inactive',
    cards: 'active',
    noResults: 'inactive',
    filter: 'inactive'
  };

  skillsObservable: Observable<any>;
  objectKeys = Object.keys;


  @ViewChild('mapCanvas') mapElement: ElementRef;
  map: any;

  cardLayout = 'large';

  infiniteScroll: any;
  refresher: any;

  googleMap: any;
  cardView = 'large';


  // Modal Variabkes
  filtersAdded: boolean;
  selectedSortBy = 'matches';
  selectedOrderBy = 'createdAt';
  selectedCategories: string[] = [];

  constructor(private mapService: MapService,
    private navCtrl: NavController,
    private firestoreService: FirestoreService,
    private userService: UserService,
    private bloomFilterService: BloomFilterService,
    private toastCtrl: ToastController,
    private skillService: SkillService,
    private modalController: ModalController,
    private shake: Shake,
    private platform: Platform) { }

  ngOnInit() {
    this.getView();
    this.initComponent();
    this.shakeListen();
  }`

async initMap() {
console.log('Initializing map');
this.map = this.mapElement.nativeElement;
let latLng;

try {
  const coordinates = await Geolocation.getCurrentPosition();
  console.log('COORDINATES FOUND - WORKING');
  latLng = new google.maps.LatLng(coordinates.coords.latitude, coordinates.coords.longitude);

  this.setItemInStorage('position', JSON.stringify(latLng));
} catch (error) {
  console.log('UNABLE TO GET GEOLOCATION');
  latLng = await this.getItemFromStorage('position');
  latLng = JSON.parse(latLng.value);
}

the wierd thing is that the console logs the 'Initializing map' bit but doesnt log any of the other console logs below it - none in the try catch block

@jcesarmobile I've managed to figure out that the geolocation works fine everywhere else, but if I'm calling it from the ngOnInit() it gives the error...

SOLVED - Instead of putting:

const { Geolocation } = Plugins

I had placed the geolocation here:

import { Geolocation } from '@capacitor...'

In case anyone else does this

I don't think that's right, I think that will use the webview implementation instead of the Capacitor plugin.

Ah, wait, you had import { Geolocation } from '@capacitor/core' and changed it to const { Geolocation } = Plugins? then yeah, that's the right way of using it.

@jcesarmobile yep thats the one!

Was this page helpful?
0 / 5 - 0 ratings