andersonrsbezerra Posted February 7, 2018 Posted February 7, 2018 Como capturar latitude e longitude atual no unigui mobile? Quote
DFong Posted November 22, 2023 Posted November 22, 2023 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; Quote
DFong Posted January 2 Posted January 2 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". Quote
PolywickStudio Posted January 10 Posted January 10 I have question... I have a button (TUnimButton) and 2 labels (TUnimLabel). How do I re-purpose the above Ajax code so it emits the lat/lon to the label? Quote
DFong Posted January 10 Posted January 10 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. Quote
DFong Posted January 10 Posted January 10 Correction: Latitude := Params.Values['Latitude']; UnimLabel1.Text := Latitude; Longitude := Params.Values['Longitude']; UnimLabel2.Text := Longitude; Quote
DFong Posted January 11 Posted January 11 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. Quote
PolywickStudio Posted January 12 Posted January 12 Can you provide the sources, or a snipplet of it? None of the geolocation code works so far. I have no idea what's going on... Quote
DFong Posted January 12 Posted January 12 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. Quote
DFong Posted January 12 Posted January 12 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? Quote
DFong Posted January 12 Posted January 12 This works for me using ISAPI. Make sure it's run under https and that you allow access to Geolocation services when promMobile_Geolocation.zippted by the browser. Quote
DFong Posted January 12 Posted January 12 Mobile_Geolocation.zip This works for me running using ISAPI. Make sure you run under https and allow access to Geolocation services when prompted by the browser. Please let me know if this works for you. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.