I have been trying to retrieve the Latitude and Longitude data using geolocation.getCurrentLocation, but I am facing issues with saving them. The function does not return a number and when I try to save them using lat and lng variables, I encounter this error: ERROR TypeError: Cannot set property 'lat' of null
If anyone can provide assistance on this matter, it would be greatly appreciated.
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
public constructor( private geo: GeoDataService, private tags: TagService) { }
title = 'Select a tag:';
latt =0;
lngg = 0;
zoomlv = 18;
lat = 1;
lng = 1;
iconUrl = 'http://maps.google.com/mapfiles/dir_0.png';
a : Array<any>;
polylines: Array<any>;
markers = new Array();
mapClicked($event: MouseEvent) {
console.log('Clicked the map at longitude: ' + $event.coords.lng + ', latitude: ' + $event.coords.lat);
}
ngOnInit(): void {
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function success(pos) {
var crd = pos.coords;
console.log('Your current position is:');
console.log(`Latitude : ${crd.latitude}`);
console.log(`Longitude: ${crd.longitude}`);
console.log(`More or less ${crd.accuracy} meters.`);
this.lat = crd.latitude;
this.lng = crd.longitude;
}
function error(err) {
console.warn(`ERROR(${err.code}): ${err.message}`);
}
navigator.geolocation.getCurrentPosition(success, error, options);
}
}