How can I get a use开发者_StackOverflowrs location in a KRL rule?
- What is the method?
- What is the advantage or disadvantage of using that method?
Here is a simple example
rule locations is active {
select using ".*" setting ()
pre {
whereareyou = location:region();
msg = <<
#{whereareyou}
>>;
}
notify("I think you live in", msg) with sticky = true;
}
And here is the docs. http://docs.kynetx.com/docs/Location
The issue you will find is that sometimes the ip does not really represent the real location of the user because the user could be using a proxy. Also with most ISP's the ip is registered to a location a the and ISP's hub not the direct location of where the IP is being used at any given moment.
With the advent of html 5 and location apis in the browser it may be posable in the future to get a more exact location but that has not been implemented in KRL as of yet.
HTM5 Browser location is available now, but requires some javascript to make it happen. This is a slightly old app that uses the Browser Location APIs. This could probably be updated to not use a form, but here it is for reference:
ruleset a8x47 {
meta {
name "WikiNearMe"
description <<
Shows Wikipedia content near the user.
>>
author "TubTeam"
logging off
}
dispatch {
domain "wikipedia.org"
}
global {
datasource placearticles:JSON <- "http://ws.geonames.org/findNearbyWikipediaJSON";
}
rule getlocation is active {
select when pageview "/wiki/" setting ()
pre {
form = <<
<div id="my_div">
<form id="nearmeform" onsubmit="return false" style="display:none;">
<input type="text" name="lat" id="nearmelat"/>
<input type="text" name="lon" id="nearmelon"/>
<input type="submit" value="Submit" />
</form>
<div id="nearmelinks" style="text-align:left;">
<h2>Nearby Links</h2>
</div>
</div>
>>;
}
// notify("Hello World", "This is a sample rule.");
emit <<
navigator.geolocation.getCurrentPosition(function(position){
$K("#nearmelat").val(position.coords.latitude);
$K("#nearmelon").val(position.coords.longitude);
$K("#nearmeform").submit();
//alert("lat: " + position.coords.latitude + " lon: " + position.coords.longitude);
});
>>
{
append("#siteNotice", form);
watch("#nearmeform", "submit");
}
}
rule shownearby is active {
select when web submit "#nearmeform"
foreach datasource:placearticles({"lat":page:param("lat"), "lng":page:param("lon"), "style":"full", "formatted":"true"}).pick("$..geonames") setting (item)
pre {
title = item.pick("$..title");
link = item.pick("$..wikipediaUrl");
}
append("#nearmelinks", "<a href='http://#{link}'>#{title}</a><br/>");
}
}
精彩评论