Location Sensor Returns Zero Value when a button is click

Hello. I created an application that uses the location sensor component.
I wish to send the latitude and longitude of my phone using the location sensor when a button is clicked. Instead it returns a zero value and is quite very unhelpful because it causes a lot of errors.

Here is a snippet of the code:

That is because you haven’t a location yet. That depends on where you are, how you get your fix. The lng and lat are only available after the locationsensor has found your location. So it might take some time, sometimes up to 30 seconds or longer depending on the conditions.

How can I improve my code to not send the zero values and wait for the real location instead? and also, it gives a kilometer away incorrect data. i can’t think of any code to improve it. Any help will be so much appreciated!

image

You should use these blocks. Only executes when a location has been found.

I tried using that but it does not fit my system functionality. (or maybe I did it wrong?)
I really want to have a location data only and only when the button is clicked.

@Peter is right, take time get location.
everything depends how to the set of the locations on smartphone.
1 > Gps, wifi and mobile data
2 > Wifi and mobile data
3 > Only GPS

Will it be better if I open my wifi, and mobile data at the same time to get a location more accurate and faster?

I my test, i would say yes

Where will your app be used the most?

I suggest:
I home you need mobile data or wifi. Many times GPS don’t work if the smartphone is inside home.
If you are on street work all set.

1 Like

Mostly indoors.

Indoors you need wifi, mobile data or both