import { Injectable } from '@angular/core' ; import { HttpClient } from '@angular/common/http' ; import { Geolocation } from '@ionic-native/geolocation/ngx' ; import { Diagnostic } from '@ionic-native/diagnostic/ngx' ; import { Platform } from '@ionic/angular' ; import coordtransform from 'coordtransform' ; @ Injectable ( { providedIn : 'root' } ) export class GeolocationService { amapKey = '' ; amapUrl = 'https://restapi.amap.com/v3/' ; currentPosition = [ 103.9787507031 , 30.6336840719 ] ; get latitude ( ) { return this . currentPosition [ 1 ] ; } set latitude ( latitude ) { this . currentPosition [ 1 ] = latitude ; } get longitude ( ) { return this . currentPosition [ 0 ] ; } set longitude ( longitude ) { this . currentPosition [ 0 ] = longitude ; } address ; addressString ; constructor ( private geolocation , private diagnostic , private http , private platform ) { } async getUserPosition ( ) { if ( this . platform . is ( 'cordova' ) && this . platform . is ( 'android' ) ) { if ( await this . diagnostic . isNetworkLocationEnabled ( ) && await this . diagnostic . isGpsLocationEnabled ( ) ) { return this . geolocation . getCurrentPosition ( { enableHighAccuracy : true } ) . then ( resp => { console . log ( resp ) ; this . currentPosition = coordtransform . wgs84togcj02 ( resp . coords . longitude , resp . coords . latitude ) ; console . log ( this . currentPosition ) ; return true ; } ) . catch ( ( error ) => { console . log ( 'Error getting location' , error ) ; console . log ( error . message ) ; return false ; } ) ; } } else { return this . http . get ( this . amapUrl + "ip" + "?key=" + this . amapKey + '&output=JSON' ) . toPromise ( ) . then ( response => { console . log ( response ) ; let data = JSON . parse ( JSON . stringify ( response ) ) ; if ( data . status == 1 ) { if ( data . rectangle . length > 0 ) { let pArray = data . rectangle . split ( ";" ) ; let p0 = pArray [ 0 ] . split ( "," ) ; let p1 = pArray [ 1 ] . split ( "," ) ; this . longitude = ( Number ( p1 [ 0 ] ) - Number ( p0 [ 0 ] ) ) / 2 + Number ( p0 [ 0 ] ) ; this . latitude = ( Number ( p1 [ 1 ] ) - Number ( p0 [ 1 ] ) ) / 2 + Number ( p0 [ 1 ] ) ; this . address = data . province + data . city ; return true ; } else { this . longitude = 108.919338 ; this . latitude = 34.539994 ; return true ; } } else return false ; } ) . catch ( this . handleError ) ; } } getAddress ( position ) { return this . http . get ( this . amapUrl + "geocode/regeo" + "?key=" + this . amapKey + '&output=JSON' + '&location=' + position [ 0 ] + ',' + position [ 1 ] ) . toPromise ( ) . then ( response => { console . log ( response ) ; let data = JSON . parse ( JSON . stringify ( response ) ) ; if ( data . status == 1 ) { this . address = data . regeocode . formatted_address ; return true ; } else return false ; } ) . catch ( this . handleError ) ; } private handleError ( error ) { console . error ( 'An error occurred' , error ) ; return false ; } }