To get location of client browser. a simple method is to use html5 Geolocation.
Add a js lib is simple method to get it. e.g. google map or something else. I use baidu map.
only 3 step,s we can make apps get ur phone location.
1. import js in head.html
...
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=<some ak code>"></script>
...
2. write a very simple class
public class Locator {
private static final String REMOTE_TYPE = "bizvision.locator";
private final RemoteObject remoteObject;
protected Location value;
private final OperationHandler operationHandler = new AbstractOperationHandler() {
public void handleCall(String method, JsonObject parameters) {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
Locator.this.value = gson.fromJson(parameters.toString(), Location.class);
}
};
public Locator() {
loadJavaScript();
Connection connection = RWT.getUISession().getConnection();
remoteObject = connection.createRemoteObject(REMOTE_TYPE);
remoteObject.setHandler(operationHandler);
}
private void loadJavaScript() {
JavaScriptLoader jsLoader = RWT.getClient().getService(JavaScriptLoader.class);
jsLoader.require("/apps/widgets/common/js/locatorhandler.js");
}
public Location getLocation(){
return value;
}
}
3. handler.js
(function() {
'use strict';
rap.registerTypeHandler("bizvision.locator", {
factory : function(properties) {
return new bizvision.locator(properties);
},
properties : [ "callback" ]
});
if (!window.bizvision) {
window.bizvision = {};
}
bizvision.locator = function(properties) {
bindAll(this, [ "onRender" ]);
rap.on("render", this.onRender);
};
bizvision.locator.prototype = {
onRender : function() {
rap.off("render", this.onRender);
var ro = rap.getRemoteObject(this);
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(r){
if(this.getStatus() == BMAP_STATUS_SUCCESS){
ro.call("callback", r);
}
},{enableHighAccuracy: true});
}
};
var bind = function(context, method) {
return function() {
return method.apply(context, arguments);
};
};
var bindAll = function(context, methodNames) {
for (var i = 0; i < methodNames.length; i++) {
var method = context[methodNames[i]];
context[methodNames[i]] = bind(context, method);
}
};
var async = function(context, func) {
window.setTimeout(function() {
func.apply(context);
}, 0);
};
}());
wish it will help you.
[Updated on: Wed, 27 July 2016 16:57]
Report message to a moderator