Jump to content

Recommended Posts

  • 5 years later...
Posted

I've used the following on mobile in response to a Button click on a simple dialog form named InputBox which has a TunimEdit control named InputEdit. You should be able to figure it out from the code snippets below:

procedure TInputBox.GeoButtonClick(Sender: TObject);

var
  GetLatLong : string;
begin
  UniSession.AddJS('InputBox.OKButton.showMask("Locating...");');
  GetLatLong := 'if (navigator.geolocation) {'+
                '      navigator.geolocation.getCurrentPosition(showPosition); '+
                '  } else {                                                     '+
                '      alert("Geolocation is not supported by this browser."); '+
                '      InputBox.OKButton.hideMask(); '+
                '  }  '+
                '  function showPosition(position) { ';

  if (pos(' LATITUDE ',Uppercase(Caption))>0) then
    GetLatLong := GetLatLong + '    ajaxRequest(InputBox.InputEdit,"Latitude",["Latitude="+position.coords.latitude+""]); }'
  else 
     GetLatLong := GetLatLong + '    ajaxRequest(InputBox.InputEdit,"Longitude",["Longitude="+position.coords.longitude+""]); }';
              
  UniSession.AddJS(GetLatLong);

end;

 

procedure TInputBox.InputEditAjaxEvent(Sender: TComponent; EventName: string;
  Params: TUniStrings);
var
  Key : word;
  Latitude,Longitude : string;
begin
  if (EventName='keypress') then
    begin
      Key := StrToInt(Params.Values['key']);
      if ord(Key)=13 then //Enter-key pressed
        begin
          FCallBack(trim(InputEdit.Text));
          Close;
        end;
    end
  else
  if EventName='Latitude' then
    begin
      Latitude := Params.Values['Latitude'];
      //ShowMessage('Latitude: '+Latitude);
      InputEdit.Text := Latitude;
      UniSession.AddJS('InputBox.OKButton.hideMask();');
    end
   else
  if EventName='Longitude' then
    begin
      Longitude := Params.Values['Longitude'];
      //ShowMessage('Latitude: '+Latitude);
      InputEdit.Text := Longitude;
      UniSession.AddJS('InputBox.OKButton.hideMask();');
    end
end;

 

  • 1 year later...
Posted

That's specific to my code.  You can ignore the section (if (EventName='keypress') then begin...end).  In my code, I'm also using a dialog called "InputBox" which displays the result in an UniEdit control, all of which you can also ignore.  It's just to show how you process the AjaxEvent generated by clicking the "GeoButton".

  • 2 weeks later...
Posted

 

Use similar code as GeoButtonClick above for your TUnimButton except you can send back the longitude and latitude in a single call:

   ajaxRequest(InputBox.InputEdit,"GetCoordinates",["Latitude="+position.coords.latitude,"Longitude="+position.coords.longitude+""]); }

 

In the AjaxCallBack (e.g. InputEditAjaxEvent) where you received the lat/lon, you can assign it as follows:

if EventName='GetCoordinates' then
    begin

        Latitude := Params.Values['Latitude'];
        UniLabel1.Text := Latitude;

        Longitude := Params.Values['Longitude'];
        UniLabel2.Text := Latitude;

    end;

 

Hope this helps.

Posted

Correction:

        Latitude := Params.Values['Latitude'];
        UnimLabel1.Text := Latitude;

        Longitude := Params.Values['Longitude'];
        UnimLabel2.Text := Longitude;

Posted

InputBox is the name of a custom dialog I built that contains an edit control named "InputEdit" and a button named "GeoButton".  It is not necessary to use as part of the code snippet I provided which you can adapt  to your specific form.

Posted

I've already provided a code snippet in the first post, but apparently that isn't working for you.  I have a very old copy of UniGui but I will see if I can put together a complete mobile sample for you within the next few days if I get some time.  You have to remember that when using geolocation on a mobile device, I recall that it only works when using https and also the user must allow the access when prompted the first time.

Posted

I've confirmed that https is required in order to use Geolocation services in the browser.  Before I go through the trouble of producing a mobile example, can you confirm that you've tried your code using https and that you were prompted to allow access to geolocation services in the browser?

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...