Skip to content

Instantly share code, notes, and snippets.

@enappd
Last active September 7, 2021 08:16
Show Gist options
  • Save enappd/93d00d0707634203047b9222af791c6b to your computer and use it in GitHub Desktop.
Save enappd/93d00d0707634203047b9222af791c6b to your computer and use it in GitHub Desktop.
Ionic Capacitor Geolocation - Geocoding and Map display
import { Component, NgZone } from '@angular/core';
import { Plugins } from '@capacitor/core';
const { Geolocation, Toast } = Plugins;
import { NativeGeocoder, NativeGeocoderResult, NativeGeocoderOptions } from '@ionic-native/native-geocoder/ngx';
@Component({
selector: 'app-tab2',
templateUrl: 'tab2.page.html',
styleUrls: ['tab2.page.scss']
})
export class Tab2Page {
public lat: any; public lng: any;
showingCurrent: boolean = true;
address: string;
constructor(private nativeGeocoder: NativeGeocoder, private ngZone: NgZone) { }
ngOnInit() {
this.setCurrentPosition();
}
async setCurrentPosition() {
const coordinates = await Geolocation.getCurrentPosition();
this.ngZone.run(() => {
this.lat = coordinates.coords.latitude;
this.lng = coordinates.coords.longitude;
})
this.showingCurrent = true;
}
async geocode() {
if (this.address != "") {
let options: NativeGeocoderOptions = {
useLocale: true,
maxResults: 5
};
this.nativeGeocoder.forwardGeocode(this.address, options)
.then((result: NativeGeocoderResult[]) => {
this.ngZone.run(() => {
this.lat = parseFloat(result[0].latitude);
this.lng = parseFloat(result[0].longitude);
})
this.showingCurrent = false;
})
.catch((error: any) => console.log(error));
}
else {
await Toast.show({
text: 'Please add address to Geocode'
});
}
}
}
@Bhagirath123
Copy link

it is working only device not in browser because it is use "ionic-native/native-geocoder/ngx" this plugin is cordova plugin and cordova run only on device not in browser

@ahmeddev0
Copy link

i use this plugin but also not working on device

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment