![]() |
|
Android Konum Alamama - Baskı Önizleme +- Delphi Can (https://www.delphican.com) +-- Forum: Delphi (https://www.delphican.com/forumdisplay.php?fid=3) +--- Forum: Mobil Platform - FireMonkey (FMX) (https://www.delphican.com/forumdisplay.php?fid=7) +--- Konu Başlığı: Android Konum Alamama (/showthread.php?tid=6889) |
Android Konum Alamama - erdogan - 14-08-2022 Merhaba, Android de konum almak için aşağıdaki örneği kullanmaya çalışıyorum fakat konum almak istediğimde uygulama direk kapanıyor proje ayarlarında tüm konumlar aktif acaba nerde hata veya eksik yapıyorum ? örnek: procedure TForm1.Button1Click(Sender: TObject); begin if Button1.Text='sensörü aç' then begin LocationSensor1.Active:=true; Button1.Text:='sensörü kapat' end else begin LocationSensor1.Active:=false; Button1.Text:='sensörü aç' end; end; procedure TForm1.LocationSensor1LocationChanged(Sender: TObject; const OldLocation, NewLocation: TLocationCoord2D); begin Boylam.Text:=NewLocation.Latitude.ToString; enlem.Text:=NewLocation.Longitude.ToString; end; bir diğer örnek
// google api: AIzaSyCsjk9cUYt_-vxQwOl7rb7GzDV3BiXTWPM
unit Form_Principal;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts,
FMX.WebBrowser, FMX.StdCtrls, FMX.Controls.Presentation, FMX.Objects,
System.Permissions, System.Sensors, System.Sensors.Components;
type
TForm1 = class(TForm)
WebBrowser: TWebBrowser;
Layout1: TLayout;
Switch: TSwitch;
Label1: TLabel;
Layout2: TLayout;
RoundRect1: TRoundRect;
lbl_endereco: TLabel;
LocationSensor: TLocationSensor;
procedure FormCreate(Sender: TObject);
procedure SwitchClick(Sender: TObject);
procedure LocationSensorLocationChanged(Sender: TObject; const OldLocation,
NewLocation: TLocationCoord2D);
procedure lbl_enderecoClick(Sender: TObject);
private
{ Private declarations }
Location: TLocationCoord2D;
FGeocoder: TGeocoder;
{$IFDEF ANDROID}
Access_Fine_Location, Access_Coarse_Location : string;
procedure DisplayRationale(Sender: TObject;
const APermissions: TArray<string>; const APostRationaleProc: TProc);
procedure LocationPermissionRequestResult
(Sender: TObject; const APermissions: TArray<string>;
const AGrantResults: TArray<TPermissionStatus>);
{$ENDIF}
procedure OnGeocodeReverseEvent(const Address: TCivicAddress);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
uses FMX.DialogService
{$IFDEF ANDROID}
,Androidapi.Helpers, Androidapi.JNI.JavaTypes, Androidapi.JNI.Os
{$ENDIF}
;
{$IFDEF ANDROID}
procedure TForm1.DisplayRationale(Sender: TObject;
const APermissions: TArray<string>; const APostRationaleProc: TProc);
var
I: Integer;
RationaleMsg: string;
begin
for I := 0 to High(APermissions) do
begin
if (APermissions[I] = Access_Coarse_Location) or (APermissions[I] = Access_Fine_Location) then
RationaleMsg := 'O app precisa de acesso ao GPS para obter sua localização'
end;
TDialogService.ShowMessage(RationaleMsg,
procedure(const AResult: TModalResult)
begin
APostRationaleProc;
end)
end;
procedure TForm1.LocationPermissionRequestResult
(Sender: TObject; const APermissions: TArray<string>;
const AGrantResults: TArray<TPermissionStatus>);
var
x : integer;
begin
if (Length(AGrantResults) = 2) and
(AGrantResults[0] = TPermissionStatus.Granted) and
(AGrantResults[1] = TPermissionStatus.Granted) then
Form1.LocationSensor.Active := true
else
begin
Switch.IsChecked := false;
TDialogService.ShowMessage
('Não é possível acessar o GPS porque o app não possui acesso')
end;
end;
{$ENDIF}
procedure TForm1.OnGeocodeReverseEvent(const Address: TCivicAddress);
var
msg : string;
begin
msg := Address.AdminArea + ', ' +
Address.CountryCode + ', ' +
Address.CountryName + ', ' +
Address.FeatureName + ', ' +
Address.Locality + ', ' +
Address.PostalCode + ', ' +
Address.SubAdminArea + ', ' +
Address.SubLocality + ', ' +
Address.SubThoroughfare + ', ' +
Address.Thoroughfare;
TDialogService.ShowMessage(msg);
end;
procedure TForm1.LocationSensorLocationChanged(Sender: TObject;
const OldLocation, NewLocation: TLocationCoord2D);
var
lt, lg, url : string;
begin
Location := NewLocation;
lt := StringReplace(Format('%2.6f', [NewLocation.Latitude]), ',', '.', [rfReplaceAll]);
lg := StringReplace(Format('%2.6f', [NewLocation.Longitude]), ',', '.', [rfReplaceAll]);
LocationSensor.Active := false;
Switch.IsChecked := false;
url := 'https://maps.google.com/maps?q=' + lt + ',' + lg;
WebBrowser.Navigate(url);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
{$IFDEF ANDROID}
Access_Coarse_Location := JStringToString(TJManifest_permission.JavaClass.ACCESS_COARSE_LOCATION);
Access_Fine_Location := JStringToString(TJManifest_permission.JavaClass.ACCESS_FINE_LOCATION);
{$ENDIF}
end;
procedure TForm1.lbl_enderecoClick(Sender: TObject);
begin
try
// Tratando a instancia TGeocoder...
if not Assigned(FGeocoder) then
begin
if Assigned(TGeocoder.Current) then
FGeocoder := TGeocoder.Current.Create;
if Assigned(FGeocoder) then
FGeocoder.OnGeocodeReverse := OnGeocodeReverseEvent;
end;
// Tratar a traducao do endereco...
if Assigned(FGeocoder) and not FGeocoder.Geocoding then
FGeocoder.GeocodeReverse(Location);
except
showmessage('Erro no serviço Geocoder');
end;
end;
procedure TForm1.SwitchClick(Sender: TObject);
begin
if Switch.IsChecked then
begin
{$IFDEF ANDROID}
PermissionsService.RequestPermissions([Access_Coarse_Location,
Access_Fine_Location],
LocationPermissionRequestResult,
DisplayRationale);
{$ENDIF}
{$IFDEF IOS}
LocationSensor.Active := true;
{$ENDIF}
end;
end;
end.
Android Konum Alamama - nguzeller - 14-08-2022 delphini içinde bu konuda bir örnek var onu denediniz mi. Cvp: Android Konum Alamama - erdogan - 14-08-2022 (14-08-2022, Saat: 08:58)nguzeller Adlı Kullanıcıdan Alıntı: delphini içinde bu konuda bir örnek var onu denediniz mi. merhaba, evet onu da denedim tüm izinler tamam android 11 de deniyorum ama direk uygulama kapanıyor (14-08-2022, Saat: 17:24)erdogan Adlı Kullanıcıdan Alıntı:(14-08-2022, Saat: 08:58)nguzeller Adlı Kullanıcıdan Alıntı: delphini içinde bu konuda bir örnek var onu denediniz mi. aşağıdaki kod ile konum geliyor fakat bir kaç saniye sonra uygulama kapanıyor unit Unit1;
interface
uses
System.SysUtils,
System.Types,
System.UITypes,
System.Classes,
System.Variants,
FMX.Types,
FMX.Controls,
FMX.Forms,
FMX.Graphics,
FMX.Dialogs,
Androidapi.JNI.Location,
Androidapi.JNIBridge,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.Os,
FMX.Layouts,
FMX.ListBox,
FMX.StdCtrls,
FMX.Controls.Presentation,
Androidapi.Helpers ;
type
TLocationListener = class;
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
CheckBox1: TCheckBox;
CheckBox2: TCheckBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
procedure Button1Click(Sender: TObject);
{ Private declarations }
private
FLocationManager : JLocationManager;
locationListener : TLocationListener;
public
destructor Destroy; override;
{ Public declarations }
procedure onLocationChanged(location: JLocation);
end;
TLocationListener = class(TJavaLocal, JLocationListener)
private
[weak]
FParent : TForm1;
public
constructor Create(AParent : TForm1);
procedure onLocationChanged(location: JLocation); cdecl;
procedure onProviderDisabled(provider: JString); cdecl;
procedure onProviderEnabled(provider: JString); cdecl;
procedure onStatusChanged(provider: JString; status: Integer; extras: JBundle); cdecl;
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
uses FMX.Helpers.Android, Androidapi.JNI.GraphicsContentViewText;
{ TLocationListener }
constructor TLocationListener.Create(AParent: TForm1);
begin
inherited Create;
FParent := AParent;
end;
procedure TLocationListener.onLocationChanged(location: JLocation);
begin
FParent.onLocationChanged(location);
end;
procedure TLocationListener.onProviderDisabled(provider: JString);
begin
end;
procedure TLocationListener.onProviderEnabled(provider: JString);
begin
end;
procedure TLocationListener.onStatusChanged(provider: JString; status: Integer;
extras: JBundle);
begin
end;
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var
LocationManagerService: JObject;
iter : JIterator;
location : JLocation;
begin
if not Assigned(FLocationManager) then
begin
LocationManagerService := SharedActivityContext.getSystemService(TJContext.JavaClass.LOCATION_SERVICE);
FLocationManager := TJLocationManager.Wrap((LocationManagerService as ILocalObject).GetObjectID);
if not Assigned(locationListener) then
locationListener := TLocationListener.Create(self);
FLocationManager.requestLocationUpdates(TJLocationManager.JavaClass.GPS_PROVIDER, 10000, 10, locationListener,
TJLooper.JavaClass.getMainLooper);
end;
iter := FLocationManager.GetAllProviders.Iterator;
ListBox1.Clear;
while iter.hasNext do
begin
ListBox1.Items.Add(JStringToString(iter.next.ToString));
end;
// CheckBox1.IsChecked := FLocationManager.isProviderEnabled(TJLocationManager.JavaClass.GPS_PROVIDER);
// CheckBox2.IsChecked := FLocationManager.isProviderEnabled(TJLocationManager.JavaClass.NETWORK_PROVIDER);
location := FLocationManager.getLastKnownLocation(TJLocationManager.JavaClass.GPS_PROVIDER);
onLocationChanged(location);
end;
destructor TForm1.Destroy;
begin
if Assigned(locationListener) then
FLocationManager.removeUpdates(locationListener);
inherited;
end;
procedure TForm1.onLocationChanged(location: JLocation);
begin
Label4.Text := location.getLatitude.ToString;
Label5.Text := location.getLongitude.ToString;
// Label6.Text := location.getAltitude.ToString;
end;
end.
Android Konum Alamama - gonulali - 19-08-2022 Merhaba hocam. bende aynı sorunu yaşıyorum. Şurada bir çözüm sunulmuş fakat portala giriş yetkisi olmadığı için göremedim. sizde erişim yetkisi var mı? https://en.delphipraxis.net/topic/5748-tlocationsensor-on-android-12/ Cvp: Android Konum Alamama - cinarbil - 19-08-2022 (19-08-2022, Saat: 08:47)gonulali Adlı Kullanıcıdan Alıntı: Merhaba hocam. bende aynı sorunu yaşıyorum. Şurada bir çözüm sunulmuş fakat portala giriş yetkisi olmadığı için göremedim. sizde erişim yetkisi var mı? https://en.delphipraxis.net/topic/5748-tlocationsensor-on-android-12/ İyi günler. ilgili makaleyi word olarak export yaptım ektedir. Cvp: Android Konum Alamama - RAD Coder - 24-08-2022 Delphi 11.1 sürümünde bu sorun düzeltilmiş. Android Konum Alamama - erdogan - 29-08-2022 Merhaba Cevaplarınız için çok teşekkür ederim, Delphi 10.4 communnity kurulu her şeyi sıfırdan kurdum windows dahil bir kaç gündür çözmeye çalışıyorum ama nafile acaba çözebilen arkadaş örnek atma imkanı var mı ? teşekkürler. Android Konum Alamama - gonulali - 31-08-2022 @erdogan hocam şunları bir dener misiniz? Androidapi.JNI.Location.pas ve System.Android.Sensors.pas'ı dosyaların yedeğini alıp proje klasörünüze kopyalayın. Bu pas dosyalarında aşağıdaki değişiklikleri yapın ve sonra projenizin içine bu dosyaları ekleyip test eder misiniz? bu yöntemle çözüme ulaşmışlar. JLocationListener = interface(IJavaInstance) ['{D1CF3FB5-3BCB-4959-98D7-BD4D8F93D839}'] procedure onLocationChanged(location: JObject); cdecl; procedure onProviderDisabled(provider: JString); cdecl; procedure onProviderEnabled(provider: JString); cdecl; procedure onStatusChanged(provider: JString; status: Integer; extras: JBundle); cdecl; end; ve "System.Android.Sensors.pas" dosyasındaki "TLocationListener" sınıfını aşağıdaki gibi değiştirin: TLocationListener = class(TJavaLocal, JLocationListener) private FLocationSensor: TUIAndroidLocationSensor; public constructor Create(ALocationSensor: TUIAndroidLocationSensor); procedure onLocationChanged(P1: JObject); overload; cdecl; procedure onLocationChanged(P1: JLocation); overload; cdecl; procedure onLocationChanged(P1: JArrayList); overload; cdecl; procedure onStatusChanged(P1: JString; P2: Integer; P3: JBundle); cdecl; procedure onProviderEnabled(P1: JString); cdecl; procedure onProviderDisabled(P1: JString); cdecl; end; Son olarak, aşağıdaki prosedürleri ekleyin. procedure TUIAndroidLocationSensor.TLocationListener.onLocationChanged(P1: JObject); begin if TJNIResolver.IsInstanceOf(P1, TJArrayList.GetClsID) then onLocationChanged(TJArrayList.Wrap(P1)) else if TJNIResolver.IsInstanceOf(P1, TJLocation.GetClsID) then onLocationChanged(TJLocation.Wrap(P1)); end; procedure TUIAndroidLocationSensor.TLocationListener.onLocationChanged(P1: JArrayList); begin onLocationChanged(TJLocation.Wrap(P1.get(P1.size-1))) end; |