Skip to content

Instantly share code, notes, and snippets.

@webfirmframework
Last active March 9, 2023 12:42
Show Gist options
  • Save webfirmframework/2a4dd279b197ebcef9de872e83430f54 to your computer and use it in GitHub Desktop.
Save webfirmframework/2a4dd279b197ebcef9de872e83430f54 to your computer and use it in GitHub Desktop.
Sample code to get geolocation at server side
package com.webfirmframework.samplecode.component;
import com.webfirmframework.wffweb.tag.html.attribute.event.mouse.OnClick;
import com.webfirmframework.wffweb.tag.html.formsandinputs.Button;
import com.webfirmframework.wffweb.tag.html.stylesandsemantics.Div;
import com.webfirmframework.wffweb.tag.htmlwff.TagContent;
public class GetGeolocationExample extends Div {
public GetGeolocationExample() {
super(null);
develop();
}
private void develop() {
// wffweb-12.x.x example
new Button(this,
new OnClick("""
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
source.position = position;
action.perform();
});
}
return false;""", event -> {
final double latitude = (double) event.data().getValue("latitude");
final double longitude = (double) event.data().getValue("longitude");
System.out.println("latitude = " + latitude);
System.out.println("longitude = " + longitude);
return null;
}, "return {latitude: source.position.coords.latitude, longitude: source.position.coords.longitude};",
null))
.give(TagContent::text, "Get geolocation");
// wffweb-3.x.x example
// new Button(this,
// new OnClick("""
// if (navigator.geolocation) {
// navigator.geolocation.getCurrentPosition(function(position) {
// source.position = position;
// action.perform();
// });
// }
// return false;""", (data, event) -> {
//
// final double latitude = (double) data.getValue("latitude");
// final double longitude = (double) data.getValue("longitude");
//
// System.out.println("latitude = " + latitude);
// System.out.println("longitude = " + longitude);
// return null;
// }, "return {latitude: source.position.coords.latitude, longitude: source.position.coords.longitude};",
// null))
// .give(TagContent::text, "Get geolocation");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment