简体   繁体   中英

TMapView not showing the current location at the beginning

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM