I want to get my location in device or browserve so to do this I used Geolocation and Native Geocoder in ionic, you guyscan follow step by step as follows:
Plugin geolocation
Plugin nativeGeocoder install
Step 4. in file home.page.html
Tags: how to get Address in ionic, get address in ionic, ionic 4 geolocation, how to get name address from latitude, get latitude in ionic,ionic 4, ionic 3, get mylocation in ionic 4, get location ionic example,geocoder geocode address,google maps api get latlng from
Step 1. Create a new ionic Application
Install ionic and Cordova CLI$ npm install -g ionic cordovaCreate a new tabs Application
$ ionic start LocationHome tabsUse CMD go to project
$cd LocationHome
Step 2. Install the Cordova and Ionic Native plugins
GeolocationPlugin geolocation
$ ionic cordova plugin add cordova-plugin-geolocationIonic native geolocation
$ npm install @ionic-native/geolocationNative Geocoder
Plugin nativeGeocoder install
$ ionic cordova plugin add cordova-plugin-nativegeocoderinstall native Geocoder
$ npm install @ionic-native/native-geocoderStep 3. Add code in app.module.ts, you shound import plugin Geolocation and plugin NativeGeocoder
import { Geolocation } from '@ionic-native/geolocation/ngx';At providers: [] you add Geolocation and NativeGeocoder below
import { NativeGeocoder } from '@ionic-native/native-geocoder/ngx';
providers: [All code in app.module.ts :
Geolocation,
NativeGeocoder,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
import { NgModule, ErrorHandler } from '@angular/core';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { AboutPage } from '../pages/about/about';
import { ContactPage } from '../pages/contact/contact';
import { HomePage } from '../pages/home/home';
import { TabsPage } from '../pages/tabs/tabs';
import { Geolocation } from '@ionic-native/geolocation/ngx';
import { NativeGeocoder } from '@ionic-native/native-geocoder/ngx';
@NgModule({
declarations: [
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
schemas: [
CUSTOM_ELEMENTS_SCHEMA
],
entryComponents: [
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage
],
providers: [
Geolocation,
NativeGeocoder,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
Step 4. in file home.page.html
<ion-header>Step 5. in file home.page.ts
<ion-navbar>
<ion-title>Home</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<h1>Get Location</h1>
<div *ngIf="geoLatitude">
<p>Latitude : {{geoLatitude}}</p>
<p>Longitude : {{geoLongitude}}</p>
<p>Accuracy : {{geoAccuracy}}</p>
</div>
</ion-content>
import { Component } from '@angular/core';Demo online:
import { NavController } from 'ionic-angular';
import { Geolocation } from '@ionic-native/geolocation/ngx';
import { NativeGeocoder,NativeGeocoderOptions } from '@ionic-native/native-geocoder/ngx';
import { NativeGeocoderResult } from '@ionic-native/native-geocoder/ngx';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
geoLatitude: number;
geoLongitude: number;
geoAccuracy:number;
geoAddress: string;
watchLocationUpdates:any;
loading:any;
isWatching:boolean;
constructor(public navCtrl: NavController,
private geolocation: Geolocation,
private nativeGeocoder: NativeGeocoder
) {
this.getGeolocation()
}
getGeolocation(){
this.geolocation.getCurrentPosition().then((resp) => {
this.geoLatitude = resp.coords.latitude;
this.geoLongitude = resp.coords.longitude;
this.geoAccuracy = resp.coords.accuracy;
}).catch((error) => {
});
}
}
Tags: how to get Address in ionic, get address in ionic, ionic 4 geolocation, how to get name address from latitude, get latitude in ionic,ionic 4, ionic 3, get mylocation in ionic 4, get location ionic example,geocoder geocode address,google maps api get latlng from
No comments:
Post a Comment