Jump to content

geolocalization


jahlxx

Recommended Posts

Hi all.

I have this function (is an example):

function Tlib.GetGeoInfo2(const IpAddress: string): string;
var
  get_url: string;
  lHTTP: TIdHTTP;
  sURL: string;
  xgeoloc: string;
begin
  lHTTP := TIdHTTP.Create(nil);

  sURL := 'http://api.ipinfodb.com/v3/ip-city/?key=bf37c2d7fffa57c8a0dd1260bdfe5dcaf587b250562ea1f768b5cc4eb3f55b99&ip=188.85.82.223';

  //get_url := Format(sURL, [IpAddress]);

  xgeoloc := '';

  try
    xgeoloc := lHTTP.Get(sURL);
  finally
    lHTTP.free;
  end;

  GetGeoInfo2 := xgeoloc;
end;

 

In I run this, returns HTTP/1.1 403 Forbidden, but, If I copy the url, and paste directly in a browser, works perfect.

Please, can someone give me some help?

Thanks.

 

Link to comment
Share on other sites

48 minutes ago, jahlxx said:

Hi all.

I have this function (is an example):

function Tlib.GetGeoInfo2(const IpAddress: string): string;
var
  get_url: string;
  lHTTP: TIdHTTP;
  sURL: string;
  xgeoloc: string;
begin
  lHTTP := TIdHTTP.Create(nil);

  sURL := 'http://api.ipinfodb.com/v3/ip-city/?key=bf37c2d7fffa57c8a0dd1260bdfe5dcaf587b250562ea1f768b5cc4eb3f55b99&ip=188.85.82.223';

  //get_url := Format(sURL, [IpAddress]);

  xgeoloc := '';

  try
    xgeoloc := lHTTP.Get(sURL);
  finally
    lHTTP.free;
  end;

  GetGeoInfo2 := xgeoloc;
end;

 

In I run this, returns HTTP/1.1 403 Forbidden, but, If I copy the url, and paste directly in a browser, works perfect.

Please, can someone give me some help?

Thanks.

 

Hello, try to use https://, maybe securing of API is active by http !

https://api.ipinfodb.com/v3/ip-city/?key=bf37c2d7fffa57c8a0dd1260bdfe5dcaf587b250562ea1f768b5cc4eb3f55b99&ip=188.85.82.223

Link to comment
Share on other sites

25 minutes ago, irigsoft said:

Hello, try to use https://, maybe securing of API is active by http !

https://api.ipinfodb.com/v3/ip-city/?key=bf37c2d7fffa57c8a0dd1260bdfe5dcaf587b250562ea1f768b5cc4eb3f55b99&ip=188.85.82.223

 

Humm...  Also Not work :(

Project Project1.exe raised exception class EIdOSSLUnderlyingCryptoError with message 'Error connecting with SSL.
error:1409442E:SSL routines:SSL3_READ_BYTES:tlsv1 alert protocol version'.

 

I tried all Indy Client IdHTTP1 parameters ...

Link to comment
Share on other sites

21 hours ago, Abaksoft said:

Hi. This works perfect, thanks.

 

The only problem is that the location is not OK. For example it give my location in other city near me.

For the purpose I'm working, I need more accuracy.

Is possible activate the GPS in the cell phone from a unigui mobile application? I need the exact location (or as near as possible)  of the user for some operations.

Some mobile applications, raise a message in the cell phone asking for activate the GPS, and if its not activated, the the option dosen't works.

 

Is there any way to do this?

 

Thanks.

 

 

Link to comment
Share on other sites

40 minutes ago, jahlxx said:

Is possible activate the GPS in the cell phone from a unigui mobile application? I need the exact location (or as near as possible)  of the user for some operations.

Yes, You can use javascript for this and AddJS on uniGui: https://www.google.com/search?q=javascript+get+gps+location+android&oq=get+gps+javascript&aqs=chrome.1.69i57j0i22i30l3j0i390.5918j0j7&sourceid=chrome&ie=UTF-8

1. https://stackoverflow.com/questions/26461367/get-gps-location-using-javascript

2. https://gis.stackexchange.com/questions/41374/accessing-gps-data-with-javascript-in-real-time

3. 

GetGeoLocation.txt

4. using unigui + Android Device + webview (android App)

Link to comment
Share on other sites

Here is some try to enable GPS via javascript:

//Enable GPS
//Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
//intent.putExtra("enabled", true);
//sendBroadcast(intent);

if ("geolocation" in navigator) {
  /* geolocation is available */
  var watchID = navigator.geolocation.watchPosition(
    function(position) {
      var out = document.getElementById ("out");

      out.innerHTML = "lat:" + position.coords.latitude  
            + ", long:" + position.coords.longitude
            //+ ",High: " + position.coords.accuracy
            ;
    }
);
} else {
  /* geolocation IS NOT available */
  alert ("geolocation IS NOT available");
}
 

Link to comment
Share on other sites

1 hour ago, jahlxx said:

Ok friend. I'll test.

Do you have a full example to help me? (in delphi)

Thanks.

I don't have it like a testcase.

I will explain how to use it.

Option I  with unigui and Javascript:

1. Create New uniGui project

2. use one TuniLabel with name: MyDataLabel

3. use UniTimer1Timer with :

uniSession.addJS (

//Enable GPS
//'Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");'
//+ 'intent.putExtra("enabled", true);'
//+ 'sendBroadcast(intent);' + 

'if ("geolocation" in navigator) {'
  /* geolocation is available */
+ '  var watchID = navigator.geolocation.watchPosition('
+ '    function(position) {'
+ '      var out = document.getElementById ("' + MyDataLabel.JSId + '");'

+ '      out.innerHTML = "lat:" + position.coords.latitude  '
+ '            + ", long:" + position.coords.longitude'
            //+ ",High: " + position.coords.accuracy
+ '            ;'
+ '    }'
+ ');'
+ '} else {'
  /* geolocation IS NOT available */
+ '  alert ("geolocation IS NOT available");'
+ '}'

);

for this You must manually start GPS on mobile Device

Option II unigui + Javascript + webView

1. Create New Android Project (with Android Studio like example)

2. Create uniGui app with same functionality like Option I

3. with installing AndroidApp you will ask user to enable GPS when app start: https://stackoverflow.com/questions/4721449/how-can-i-enable-or-disable-the-gps-programmatically-on-android

 

https://www.google.com/search?q=android+enable+gps+programmatically&oq=android+enable+gps&aqs=chrome.1.69i57j0i512l2j0i22i30l7.5139j0j7&sourceid=chrome&ie=UTF-8

 

Link to comment
Share on other sites

WOW. Thanks friend.

I almost got it.

With your code, raises this compile errors;

[dcc32 Error] Main.pas(70): E2003 Undeclared identifier: ' MyDataLabel'
[dcc32 Error] Main.pas(70): E2029 ')' expected but identifier 'JSId ' found

 

If I try other code based in yours:

  uniSession.addJS (
      'if ("geolocation" in navigator) {'
      + '  var watchID = navigator.geolocation.watchPosition('
      + '    function(position) {'
      + '  alert ("geolocation IS available "+ "lat:" + position.coords.latitude + ", long:" + position.coords.longitude);'
      + '    }'
      + ');'
      + '} else {'
      + '  alert ("geolocation IS NOT available");'
      + '}'
  );

This works, but have 2 problems:

1) I don't know how to return lat ans long to 2 variables (delphi variables)

2) The call to that JS is called permanently until close the browser or the browser page where is running.

Any idea?

 

Link to comment
Share on other sites

18 hours ago, jahlxx said:

the browser page where is running.

I think where the unigui session is open. I use uniTimer to keep execute (every second) javascript code !

uniTimer must use ChainMode := True, Enable := True, Sequenced := False;

18 hours ago, jahlxx said:

With your code, raises this compile errors;

try to search by Name:  ' var out = document.getElementByName ("' + MyDataLabel.JSName + '");'

Link to comment
Share on other sites

procedure TUnimFormMeuAtendimento.ExecuteLocalizacao(Botao : String);
begin
  UniSession.AddJS(
    'if (navigator.geolocation) { ' +
    '  navigator.geolocation.getCurrentPosition( function(position) { ' +
    '    ajaxRequest(UnimFormMeuAtendimento.'+Botao+', "CurrentPosition" ,' +
    '      ["lat=" + position.coords.latitude, ' +
    '       "lng=" + position.coords.longitude, ' +
    '       "acc=" + position.coords.accuracy, ' +
    '       "alt=" + position.coords.altitude, ' +
    '       "altacc=" + position.coords.altitudeAccuracy, ' +
    '       "head=" + position.coords.heading, ' +
    '       "ts=" + position.coords.timestamp ' +
    '      ]);' +
    '    })' +
    '} else {alert("Localização não suportada!");}');
end;

 

Link to comment
Share on other sites

6 hours ago, picyka said:
procedure TUnimFormMeuAtendimento.btnLocalizacaoDestinoClick(Sender: TObject);
begin
  Self.ExecuteLocalizacao(TUnimButton(Sender).Name);
end;

 

Thank you Picyka,

How does it work ?

I tried, but no Alert. Can you send an example.

Thx


procedure TMainmForm.ExecuteLocalizacao(Botao : String);
begin
  UniSession.AddJS(
    'if (navigator.geolocation) { ' +
    '  navigator.geolocation.getCurrentPosition( function(position) { ' +
    '    ajaxRequest(UnimFormMeuAtendimento.'+Botao+', "CurrentPosition" ,' +
    '      ["lat=" + position.coords.latitude, ' +
    '       "lng=" + position.coords.longitude, ' +
    '       "acc=" + position.coords.accuracy, ' +
    '       "alt=" + position.coords.altitude, ' +
    '       "altacc=" + position.coords.altitudeAccuracy, ' +
    '       "head=" + position.coords.heading, ' +
    '       "ts=" + position.coords.timestamp ' +
    '      ]);' +
    '    })' +
    '} else {alert("Localização não suportada!");}');
end;

procedure TMainmForm.UnimButton1Click(Sender: TObject);
begin
  //Self.ExecuteLocalizacao(TUnimButton(Sender).Name);

  ExecuteLocalizacao('Paris');
end;

 

Link to comment
Share on other sites

25 minutes ago, irigsoft said:

great, can you share some code?

Of course.

Done with touch components.

I 've followed these steps:

1.- In a unimform, put 2 unimedits (xlat, xlon), and a unimbutton (disabled).

2. In the onshow event of the form, this code:

  xlat.text := 'x';
  xlon.text := 'x';

 uniSession.addJS (
'if ("geolocation" in navigator) {'
+ '  var watchID = navigator.geolocation.watchPosition('
+ '    function(position) {'
+ xlat.JSName+'.setValue(position.coords.latitude);'
+ xlon.JSName+'.setValue(position.coords.longitude);'
+ '    }'
+ ');'
+ '} else {'
+ xlat.JSName+'.setValue("x");'
+ xlon.JSName+'.setValue("x");'
+ '}'
);

4. This ask the user to allow the app to access the location. (tested in android and an iOS cell phones) (I think that as Sherzod said in a post before, this depends of the browser, not the device).

5. If the user accept, then after a few seconds, latitude and longitude appear in the unimedits (xlat and xlon).

If not, the values remains set to 'x'.

6. Give some code, to the xlat (or xlon) edit onchange event. What value is not 'x', then the button is enabled, and do what you want in the onclick event. When enabled, meand that device could be geolocated.

Value 'x' means that geolocation is not done, due to the user denied the permission, and/or technical issues that didn't the browser to be able to locate the device.  In my case, is mandatory to be geolocated. If not, the service will be unavailable for the user.

That's all.

 

  • Thanks 1
Link to comment
Share on other sites

  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...