* * *

Author Topic: Geo location  (Read 1316 times)

mpknap

  • New member
  • *
  • Posts: 6
Geo location
« on: April 11, 2018, 07:45:44 am »
How to get longitude and laltitude from site http:\\mycurrentlocation.net

Leledumbo

  • Hero Member
  • *****
  • Posts: 7866
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Geo location
« Reply #1 on: April 11, 2018, 08:02:02 am »
How to get longitude and laltitude from site http:\\mycurrentlocation.net
Ask the website author whether it provides an API for its services. I don't think HTML scraping would work, as the code that determines longitude/latitude is implemented at JavaScript level here: https://mycurrentlocation.net/js/mycurrentlocation.js.
I suggest another one that already has an API and pretty straightforward to call: https://ipstack.com/.

Renat.Su

  • Jr. Member
  • **
  • Posts: 58
    • Renat.Su
Re: Geo location
« Reply #2 on: April 11, 2018, 09:25:03 am »
It seems that the location of the site determines by IP. It would be more correct to simply define your IP (some external service on the Internet), and then, through a GEOIP database, to determine the coordinates and other geographic data

Renat.Su

  • Jr. Member
  • **
  • Posts: 58
    • Renat.Su
Re: Geo location
« Reply #3 on: April 11, 2018, 09:26:59 am »
Hmm... https://ipstack.com/ Also a good solution

mpknap

  • New member
  • *
  • Posts: 6
Re: Geo location
« Reply #4 on: April 12, 2018, 07:11:16 am »
hmmm...too complicated for me. I do not know how to download this data to lazarus pascal :(
I'm writing a small program for myself, I'm a beginner. Anyway, thank you for the hints :)

DonAlfredo

  • Hero Member
  • *****
  • Posts: 875
Re: Geo location
« Reply #5 on: April 12, 2018, 07:43:14 am »
You could try this:

Code: Pascal  [Select]
  1. uses fphttpclient, fpjson, jsonparser;
  2.  
  3. { TForm1 }
  4.  
  5. procedure TForm1.Button1Click(Sender: TObject);
  6. var
  7.   Http: TFPHTTPClient;
  8.   Content : string;
  9.   Json : TJSONData;
  10.   JsonObject : TJSONObject;
  11. begin
  12.   Http:=TFPHTTPClient.Create(Nil);
  13.   try
  14.      Http.IOTimeout:=1000;
  15.      Http.AllowRedirect:=true;
  16.      Content:=Http.Get('http://ip-api.com/json');
  17.      Json:=GetJSON(Content);
  18.      try
  19.        JsonObject := TJSONObject(Json);
  20.        Memo1.Lines.Append(JsonObject.Get('lat'));
  21.        Memo1.Lines.Append(JsonObject.Get('lon'));
  22.      finally
  23.        Json.Free;
  24.      end;
  25.   finally
  26.     Http.Free;
  27.   end;
  28. end;

Thaddy

  • Hero Member
  • *****
  • Posts: 5969
Re: Geo location
« Reply #6 on: April 12, 2018, 11:17:29 am »
A very good example. You can write it a bit shorter because of your type cast and inheritance.
You can also do it like this:
Code: Pascal  [Select]
  1. program testgeo;
  2. {$ifdef fpc}{$mode delphi}{$endif}
  3. {$ifdef mswindows}{$apptype console}{$endif}
  4. uses
  5.   fphttpclient, fpjson, jsonparser;
  6. var
  7.   Http: TFPHTTPClient;
  8.   JsonObject : TJSONObject;
  9. begin
  10.   Http:=TFPHTTPClient.Create(Nil);
  11.   try
  12.      Http.IOTimeout:=1000;
  13.      Http.AllowRedirect:=true;
  14.      JsonObject := TJSONObject(GetJson(Http.Get('http://ip-api.com/json')));
  15.      try
  16.        writeln(JsonObject.Get('lat'));
  17.        writeln(JsonObject.Get('lon'));
  18.      finally
  19.        JsonObject.Free;
  20.      end;
  21.   finally
  22.     Http.Free;
  23.   end;
  24. end.

You can also write:
Code: Pascal  [Select]
  1.      JsonObject := GetJson(Http.Get('http://ip-api.com/json')) as TJsonObject;
JsonData is a JsonObject. Hence this works.
But your example is probably more instructive. This is just a remark.
« Last Edit: April 12, 2018, 11:26:11 am by Thaddy »
recommends {$macro on}{$define Silly:=ObjFpc}

Thaddy

  • Hero Member
  • *****
  • Posts: 5969
Re: Geo location
« Reply #7 on: April 12, 2018, 11:37:20 am »
Or still shorter, but in the horrible code category and just for fun (don't do this at home):
Code: Pascal  [Select]
  1. program testgeo;
  2. {$ifdef fpc}{$mode delphi}{$endif}
  3. {$ifdef mswindows}{$apptype console}{$endif}
  4. uses
  5.   fphttpclient, fpjson;
  6. begin
  7.   with TFPHTTPClient.Create(Nil) do
  8.   try
  9.      IOTimeout:=1000;
  10.      AllowRedirect:=true;
  11.      with GetJson(Get('http://ip-api.com/json')) as TJsonObject do
  12.      try
  13.        writeln(Get('lat'));
  14.        writeln(Get('lon'));
  15.      finally
  16.        Free;
  17.      end;
  18.   finally
  19.     Free;
  20.   end;
  21. end.

Double with and ambiguous get  :D :D ... Ouch....Who can read that.... O:-)

Anyway: use DonAlfredo's example here http://forum.lazarus.freepascal.org/index.php/topic,40872.msg282783.html#msg282783 when you use Lazarus GUI applications or my first simplfied example. (This is horrible code and I can make it much worse by removing the try finally blocks..)
« Last Edit: April 12, 2018, 03:57:31 pm by Thaddy »
recommends {$macro on}{$define Silly:=ObjFpc}

mpknap

  • New member
  • *
  • Posts: 6
Re: Geo location
« Reply #8 on: April 12, 2018, 05:36:05 pm »
It works great, thank you everyone :)

bazinga!

  • Newbie
  • Posts: 1
Re: Geo location
« Reply #9 on: May 05, 2018, 02:29:10 pm »
How to get longitude and laltitude from site http:\\mycurrentlocation.net
Actually It is detecting your ip address and based on this it provides the longitude and laltitude. It also may show your ISP location.

ASerge

  • Hero Member
  • *****
  • Posts: 744
Re: Geo location
« Reply #10 on: May 05, 2018, 08:42:14 pm »
Code: Pascal  [Select]
  1. program testgeo;
  2. {$ifdef fpc}{$mode delphi}{$endif}
  3. {$ifdef mswindows}{$apptype console}{$endif}
  4. uses
  5.   fphttpclient, fpjson;
  6. ...
Add jsonparser to uses to make this example work.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus