Ionic location get cached position


#1

Hello,
I’m using Leaflet for maps and for geolocate, but when I move to another location the app returns the old location. This is my code:

this.map = leaflet.map("map").fitWorld();
	    leaflet.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
	      //attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
	      attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>',
	      maxZoom: 18,
	      draggable: true
	    }).addTo(this.map);
	    this.map.locate({
	      setView: true,
	      maxZoom: 18,
	      timeout: 1000,
	      enableHighAccuracy: true,
	      maximumAge: 3600
	    }).on('locationfound', (e) => {
	      let markerGroup = leaflet.featureGroup();
	      this.ubi_lat = e.latitude;
	      this.ubi_long = e.longitude;
	      let marker: any = leaflet.marker([e.latitude, e.longitude], {draggable: true}).on('dragend', (ev) => {
	        var chagedPos = ev.target.getLatLng();
	        this.ubi_lat = chagedPos.lat;
	      	this.ubi_long = chagedPos.lng;
	      })
	      markerGroup.addLayer(marker);
	      this.map.setZoom(16);
	      this.map.addLayer(markerGroup);
	      }).on('locationerror', (err) => {
	        alert(err.message);
	    })

What I can do? The “maximunAge” property doesn’t work… Any help? Thanks


#2

Please refer this coding its must work,but you can import plugins in geolocation,geocoder.Then import app.module.ts file,then where you want to use this coding copy and paste it.

//get location coding this file you can paste it.
import { NativeGeocoder, NativeGeocoderReverseResult, NativeGeocoderOptions } from ‘@ionic-native/native-geocoder’;

@IonicPage()
@Component({
selector: ‘page-register’,
templateUrl: ‘register.html’,
})
export class RegisterPage {
//declare the variable
data:any = {};
public deviceInfo: deviceInterface = {};
loading: any;
public lat:number;
public lng:number;

//parameters are used to geolocation.
constructer(public nativeGeocoder: NativeGeocoder,public geolocation: Geolocation){
}
//Get a location
this.geolocation.getCurrentPosition({enableHighAccuracy: true}).then((resp) => {
this.lat= resp.coords.latitude;
this.lng= resp.coords.longitude;
console.log(this.lat);
console.log(this.lng);
this.reverseGeocoding(this.lat,this.lng);
}).catch((error) => {
console.log(‘Error getting location’, error);
});
}

//lat and lng conversion of original address.
reverseGeocoding(lat,lng){
let options: NativeGeocoderOptions = {
useLocale: false,
maxResults: 2
};
this.nativeGeocoder.reverseGeocode(lat,lng,options)
.then((result: NativeGeocoderReverseResult) =>{
this.data.addrss= result[1].subLocality +","+ result[1].locality +","+
result[1].subAdministrativeArea +","+ result[1].administrativeArea +"-"+
result[1].postalCode +","+result[1].countryName;
console.log(this.data.addrss);
alert(JSON.stringify(this.data.addrss));
}).catch((error: any) => console.log(error));
}
its work correctly,please refer this coding,try it.