I'm trying to create an application where the user clicks on a button and a TMapView appears. I just want that the current location of the user to appear to the map at the beginning and not the zero location. This only happens when he presses the device's back button and then the second time it goes to the current location. I don't quite understand why this happens. Here's the code:
procedure TForm2.SetLocation();
var
LManager : TSensorManager;
LSensors : TSensorArray;
LSensor : TCustomSensor;
LLocationSensor: TCustomLocationSensor;
begin
LManager := TSensorManager.Current;
LManager.Activate;
LSensors := LManager.GetSensorsByCategory(TSensorCategory.Location);
for LSensor in LSensors do
begin
LLocationSensor:=LSensor as TCustomLocationSensor;
LLocationSensor.Start;
MapView1.Location := TMapCoordinate.Create(LLocationSensor.Latitude,LLocationSensor.Longitude);
end;
end;
Assuming this function is called the first time the map is render.
CodePudding user response:
You should make use of OnLocationChanged
event for retrieving your device position as it will fire as soon as location is retrieved.
Of course time to successfully retrieve initial location is heavily affected by the signal strength.
It is a good practice to notify the user when you are still in the process of retrieving current location.
NOTE: Since OnLocationChanged
fires every time the location of the device is changed you might want to add yourself some control variable with which you will control whether you want to update location only once or constantly keep tracking the device movement.