Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » Get client location from browser
Get client location from browser [message #1739142] Wed, 27 July 2016 16:57
zone whua is currently offline zone whuaFriend
Messages: 7
Registered: July 2009
Location: Wuhan China
Junior Member
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

Previous Topic:a simple implementation of swipe slider
Next Topic:how to share the plugins lib or third part jar libaries for multiple rap applications in one tomcat?
Goto Forum:
  


Current Time: Wed Apr 24 21:30:23 GMT 2024

Powered by FUDForum. Page generated in 0.03586 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top