Get current location problem


#1

I’m trying to use getCurrentPosition function from geolocation plugin to get my current location but it return error this is the code

this.geolocation.getCurrentPosition().then((position) => {

  let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

  let mapOptions = {
    center: latLng,
    zoom: 15,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);

  let marker = new google.maps.Marker({
    map: this.map,
    animation: google.maps.Animation.DROP,
    position: this.map.getCenter()
  });

}, (err) => {
   console.log(err);
});

Can anyone tell me why?


#2
this.geolocation.getCurrentPosition().then((resp) => {
 // resp.coords.latitude
 // resp.coords.longitude

#3

this.geolocation.getCurrentPosition().then((position) => {

let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

I do that??
but the map not appear


#4

are you using google maps native

http://ionicframework.com/docs/native/google-maps/


#5

no, i’m using google maps sdk


#6

No, i’m using google mas sdk


#7

@Ben1nB1ack, now i’m using google maps native and it works fine but i want to get lat&long without display map. when I make #map display:none or height:0px. the plugin not woks


#8

Ok, I’m confused, first you wanted to see the map and now you don’t :wink:

maybe if you told me what your end goal is.


#9

I’m sorry about that, my end goal is in my app there is a registration page i want to display user location in it and if he/she wants to change it, when click button the map appear.
I hope that i could display the idea.


#10

I would have both installled

google-maps-native which you have for display

there is a pluging called angular2-google-maps
it works with browser and on phone.
great video. on geolocation


#11

guys i have set the ANDROID_HOME variable path when i echo the $ANDROID _HOME it shows correct path but when i am trying to build up my project ANDROID_HOME value is diffrent

bc@abc-HP-15-Notebook-PC:~$ echo $ANDROID_HOME
/home/abc/Android/Sdk

cordova build android
[11:44:35] lint finished in 3.24 s
ANDROID_HOME=/usr/lib/android-sdk
JAVA_HOME=/usr/lib/jvm/java-8-oracle
Subproject Path: CordovaLib
The Task.leftShift(Closure) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use Task.doLast(Action) instead.

at build_83r92ws7vj07urdw76kepchpw.run(/home/abc/CRM/platforms/android/build.gradle:141)

Checking the license for package Android SDK Platform 26 in /usr/lib/android-sdk/licenses
Warning: License for package Android SDK Platform 26 not accepted.

FAILURE:
Build failed with an exception.

  • What went wrong:
    A problem occurred configuring root project ‘android’.

You have not accepted the license agreements of the following SDK components:
[Android SDK Platform 26].
Before building your project, you need to accept the license agreements and complete the installation of the missing components using the Android Studio SDK Manager.
Alternatively, to learn how to transfer the license agreements from one workstation to another, go to http://d.android.com/r/studio-ui/export-licenses.html

  • Try:

#12

It is not applicable to get location by google maps without display map??


#13

l


#14

but geolocation not works with me, it all the time return error.when i use ionic serve/ ionic lab it works fine but when i deploy the app in androd device it doesn’t work.


#15

this is angular google maps, not geolocation

https://angular-maps.com/


#16

But this to be used needs lat&long which geolocation give them. doessn’t it?


#17

Sorry, i got mySelf confused, LOL

may you should do

$ ionic cordova plugin rm cordova-plugin-geolocation 
$ npm uninstall --save @ionic-native/geolocation

$ npm rebuild

$ ionic cordova plugin add cordova-plugin-geolocation --variable GEOLOCATION_USAGE_DESCRIPTION="To locate you"
$ npm install --save @ionic-native/geolocation

here is sample .ts

import { Component } from '@angular/core';
import { NavController } from "ionic-angular";
import { Geolocation } from '@ionic-native/geolocation';

@Component({
  selector: 'page-place-form',
  templateUrl: 'place-form.html',
})
export class PlaceForm {
  location: any;
  gmLocation: {lat: number, lng: number} = {lat: 0, lng: 0};

  constructor(private navCtrl: NavController, private geoloc : Geolocation) {
    this.onLocateUser();
  }

  onLocateUser() {
    this.geoloc.getCurrentPosition()
      .then(
        (location) => {
          console.log('position gotten: long:',location.coords.longitude,' lat:',location.coords.latitude);
          this.location = location;
          this.gmLocation.lat = location.coords.latitude;
          this.gmLocation.lng = location.coords.longitude;
        }
      )
      .catch(
        (error) => {
          console.log('Error getting location', error);
        }
      )

  }


}