Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
739 views
in Technique[技术] by (71.8m points)

post - Angular geolocation becomes empty

I'm trying to bind my geolocation with a picture and send it to my backend (in Angular). I get correctly the geolocation but when I make the POST call, the geolocation becomes empty in the body request. HOW/WHY TF?

Here is an example of my problem: https://stackblitz.com/edit/angular-nbrov5 (maybe need to open it 'live' to have geolocation working https://angular-nbrov5.stackblitz.io/ )

makeCall() {
   navigator.geolocation.getCurrentPosition(position => {
     console.log("My position is ", position);
     let postObject = {
       position: position,
       positionJSONified: JSON.parse(JSON.stringify(position)),
       latitude: position.coords.latitude,
       longitude: position.coords.longitude,
       nested: { toto: 1234, tata: { tutu: "hello" } }
     };
     this.httpClient
       .post("https://anyurl.com", postObject)
       .subscribe(console.log);
   }, console.error);
 }

I got the geolocation correctly: enter image description here

but is empty (not null) in POST: enter image description here

This isn't be a backend-server issue because this happens in the POST Call, not in the response. Looks like this is a reference issue. Therefore I tried the old JSON.parse/JSON.stringify and the spread operator but even this, the location remains empty. Hints? The geolocation content is readonly. Is this for security purposes?

question from:https://stackoverflow.com/questions/65882595/angular-geolocation-becomes-empty

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this

makeCall() {
   navigator.geolocation.getCurrentPosition(position => {
     const positionObject= { coords: position.coords, timestamp: position.timestamp };
     let postObject = {
       position: positionObject,
       positionJSONified: JSON.parse(JSON.stringify(positionObject)),
       latitude: position.coords.latitude,
       longitude: position.coords.longitude,
       nested: { toto: 1234, tata: { tutu: "hello" } }
     };
     this.httpClient
       .post("https://anyurl.com", postObject)
       .subscribe(console.log);
   }, console.error);
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...