Hello ,
I was trying to retrieve adress using geolocation and geocoder to get all informatons about lke where i am now , municipalities near me, Zip code, …
but the geocoder do not work
my file ts :
import { Component, ViewChild, NgModule } from '@angular/core';
import { Camera, CameraOptions } from '@ionic-native/camera';
import { Geolocation } from '@ionic-native/geolocation';
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';
import { Storage } from '@ionic/storage';
import { File } from '@ionic-native/file';
import { NavController, NavParams, ToastController, LoadingController, Platform} from 'ionic-angular';
import { FormBuilder, FormGroup, Validators, NgForm, FormControl,ValidatorFn, AbstractControl } from '@angular/forms';
import { NativeGeocoder, NativeGeocoderReverseResult, NativeGeocoderOptions } from '@ionic-native/native-geocoder';
import { EspaceCitoyenPage } from '../espace-citoyen/espace-citoyen';
import { PostProvider } from '../../providers/post-provider';
import { Http, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/catch';
@Component({
selector: 'page-reclamation-automatique',
templateUrl: 'reclamation-automatique.html',
})
export class ReclamationAutomatiquePage {
geoencoderOptions: NativeGeocoderOptions = {
useLocale: true,
maxResults: 5
};
options: CameraOptions = {
quality: 100,
destinationType: this.camera.DestinationType.DATA_URL,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
cameraDirection:0
}
private Form : FormGroup;
public position: string;
loading: any;
myphoto: string= '';
items: any;
public dt: any;
cin: string="";
type_rec: string ="";
description: string ="";
gouvernorat: string ="";
commune: string="";
postal: string="";
adresse: string="";
laptitude: any;
longitude: any;
geoAccuracy: number;
geoAddress: any;
watchLocationUpdates: any;
isWatching: boolean;
constructor(public navCtrl: NavController, public storage: Storage, public toastCtrl: ToastController, private nativeGeocoder: NativeGeocoder,
private postPvdr: PostProvider, public plt: Platform, private geolocation: Geolocation, private platform: Platform,
private formBuilder: FormBuilder, public loadingCtrl: LoadingController,
private camera: Camera, private transfer: FileTransfer,
private file: File, public http: Http) {
this.Form = formBuilder.group({
type_rec: ['', Validators.compose([Validators.required])],
description : ['', Validators.compose([Validators.required])],
position : ['',]
});
this.loading = this.loadingCtrl.create({
cssClass: 'my-loading-class',
spinner: 'bubbles',
duration: 3000
});
this.platform.ready().then(() => {
//set options
var options = {
timeout: 20000 // milliseconds
}
//use the geolocation
this.geolocation.getCurrentPosition(options).then(data => {
this.laptitude = data.coords.longitude;
this.longitude = data.coords.latitude;
this.getGeoencoder(this.laptitude, this.longitude)
}).catch((err) => {
console.log("Erreur", err);
});
});
}
ionViewDidLoad()
{
console.log('ionViewDidLoad ReclamationAutomatiquePage');
}
//geocoder method to fetch address from coordinates passed as arguments
getGeoencoder(laptitude, longitude) {
this.nativeGeocoder.reverseGeocode(laptitude, longitude, this.geoencoderOptions)
.then((result: NativeGeocoderReverseResult[]) => {
this.geoAddress = this.generateAddress(result[0]);
})
.catch((error: any) => {
alert('Error getting location' + JSON.stringify(error));
});
}
//Return Comma saperated address
generateAddress(addressObj) {
let obj = [];
let address = "";
for (let key in addressObj) {
obj.push(addressObj[key]);
}
obj.reverse();
for (let val in obj) {
if (obj[val].length)
address += obj[val] + ', ';
}
return address.slice(0, -2);
}
my file html:
<ion-item>
latitude : {{ this.laptitude }}
</ion-item>
<ion-item>
Longitude : {{ this.longitude }}
</ion-item>
<ion-item>
Adresse : {{ this.geoAdress }}
</ion-item>