Geolocation native plugin not working in Android


#1

Hello all,

Basically i wanted to get the user location’s latitude-longitude by using Ionic 2 Geolocation plugin, Plugin is working in IOS and Web browser, but when i tested this in Android (Ionic View or After installing apk,) this plugin not works. please suggest me any solution.

Thanks.


#2

It works for me. Can you give us more detail, like the error you have, your code, Android version, …


#3

Hi @Alex_gsy

Can u please check my code, is there any error or not. this code is not working on Android version.

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';
import { Geolocation } from '@ionic-native/geolocation';
import { HomePage } from '../pages/home/home';
@Component({
  templateUrl: 'app.html',
  providers : [Geolocation]
})
export class MyApp {
  rootPage = HomePage;
  constructor(platform: Platform, private geolocation: Geolocation) {
    platform.ready().then(() => {
      StatusBar.styleDefault();
      Splashscreen.hide();
		this.geolocation.getCurrentPosition({
			enableHighAccuracy: true,
			maximumAge: 0
		}).then((resp) => {
			alert(resp.coords.latitude);
			alert(resp.coords.longitude);
		}, (err) => {
			alert(err);
		});
	});
  }
}

Thanks


#4

Plugin is not giving any error, but if i added the timeout then error comes
.


#5

Same issue for me. Is this not resolved yet?


#6

Remove geolocation as a provider in your template, instead add it to your providers array in app.module.ts.

Keep the rest of your code the same, as in continue to import it in your components imports.


#7

Hi, @manishsood

Could you try like below code


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

declare var google;

@Component({
  selector: 'page-home',
  templateUrl: 'home.html',
  providers:[[Geolocation]]
})
export class HomePage {


    public lat:any;a
    public lng:any;
    public latLng :any=[];

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

  constructor(public navCtrl: NavController,private  geolocation: Geolocation) {
    let watch = this.geolocation.watchPosition();
      watch.subscribe((position) => {
        this.latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        console.log("change position")
        });
  }
  ionViewDidLoad(){
   this.loadMap();
 }
  loadMap(){
    this.geolocation.getCurrentPosition()
    .then((position) => {
        this.latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        console.log('sadsad',this.latLng);
        let mapOptions = {
          center: this.latLng,
          zoom:18,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        console.log('asdasdsad',mapOptions);
        this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);
        this.addMarker();
        });
  }
  addMarker(){
    let marker = new google.maps.Marker({
      map: this.map,
      animation: google.maps.Animation.DROP,
      position: this.map.getCenter()
    });
    let content = "<h4>Add Your Marker Name</h4>";
    this.addInfoWindow(marker, content);
  }
  addInfoWindow(marker, content){
    let infoWindow = new google.maps.InfoWindow({
      content: content
    });
    google.maps.event.addListener(marker, 'click', () => {
      infoWindow.open(this.map, marker);
    });

  }

}

view file

<ion-content >
  <div #map id="map"></div>
</ion-content>

Hope this will Resolve your issue

Thanks